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

check for reuseport support by opening a socket, not listening. #40

Merged
merged 2 commits into from
Jan 29, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions available_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
"syscall"
"time"

sockaddrnet "github.com/libp2p/go-sockaddr/net"
)

var (
Expand All @@ -15,7 +17,7 @@ var (
)

// Available returns whether or not SO_REUSEPORT is available in the OS.
// It does so by attepting to open a tcp listener, setting the option, and
// It does so by attepting to open a tcp socket, setting the option, and
// checking ENOPROTOOPT on error. After checking, the decision is cached
// for the rest of the process run.
func available() bool {
Expand All @@ -24,14 +26,13 @@ func available() bool {
}

func checkReusePort() {
// there may be fluke reasons to fail to add a listener.
// there may be fluke reasons to fail to open a socket.
// so we give it 5 shots. if not, give up and call it not avail.
for i := 0; i < 5; i++ {
// try to listen at tcp port 0.
l, err := listenStream("tcp", "127.0.0.1:0")
// try to setup a TCP socket.
fd, err := socket(sockaddrnet.AF_INET, sockaddrnet.SOCK_STREAM, sockaddrnet.IPPROTO_TCP)
if err == nil {
l.Close() // Go back to the Shadow!
// no error? available.
unix.Close(fd)
hasReusePort = true
return
}
Expand Down