Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stub/wrapper for reuseport.Listen on Windows platforms #638

Merged
merged 16 commits into from
Aug 30, 2019
Merged
13 changes: 1 addition & 12 deletions reuseport/reuseport.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build linux darwin dragonfly freebsd netbsd openbsd rumprun
// +build !windows

// Package reuseport provides TCP net.Listener with SO_REUSEPORT support.
//
Expand All @@ -9,23 +9,12 @@
package reuseport
andrewheberle marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"
"net"
"strings"

"github.com/valyala/tcplisten"
)

// ErrNoReusePort is returned if the OS doesn't support SO_REUSEPORT.
type ErrNoReusePort struct {
err error
}

// Error implements error interface.
func (e *ErrNoReusePort) Error() string {
return fmt.Sprintf("The OS doesn't support SO_REUSEPORT: %s", e.err)
}

// Listen returns TCP listener with SO_REUSEPORT option set.
//
// The returned listener tries enabling the following TCP options, which usually
Expand Down
15 changes: 15 additions & 0 deletions reuseport/reuseport_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package reuseport

import (
"fmt"
)

// ErrNoReusePort is returned if the OS doesn't support SO_REUSEPORT.
type ErrNoReusePort struct {
err error
}

// Error implements error interface.
func (e *ErrNoReusePort) Error() string {
return fmt.Sprintf("The OS doesn't support SO_REUSEPORT: %s", e.err)
}
2 changes: 2 additions & 0 deletions reuseport/reuseport_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !windows

package reuseport

import (
Expand Down
11 changes: 11 additions & 0 deletions reuseport/reuseport_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package reuseport

import (
"fmt"
"net"
)

// Listen always returns ErrNoReusePort on Windows
func Listen(network, addr string) (net.Listener, error) {
return nil, &ErrNoReusePort{fmt.Errorf("Not supported on Windows")}
}
18 changes: 18 additions & 0 deletions reuseport/reuseport_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// +build windows

package reuseport
andrewheberle marked this conversation as resolved.
Show resolved Hide resolved

import (
"testing"
)

func TestListen(t *testing.T) {
_, err := Listen("tcp6", "[::1]:10082")
if err == nil {
t.Fatalf("unexpected non-error creating listener")
}

if _, errnoreuseport := err.(*ErrNoReusePort); !errnoreuseport {
t.Fatalf("unexpected error creating listener: %s", err)
}
}