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

Conversation

andrewheberle
Copy link
Contributor

This PR adds a "stub" Listen function for Windows only that always returns (*reuseport.ErrNoReusePort) so the same code can be used on Windows and non-Windows platforms.

So in this case the following code will work on Windows as well as Linux/BSD platforms

// create server
srv := fasthttp.Server{
	Handler:      requestHandler,
	ReadTimeout:  5 * time.Second,
	WriteTimeout: 10 * time.Second,
	IdleTimeout:  120 * time.Second,
}

// try using SO_REUSEPORT
nl, err := reuseport.Listen(addrFamily, listenAddress)
if err != nil {
	if _, noreuseport := err.(*reuseport.ErrNoReusePort); noreuseport {
		// fall back to plain net.Listen
		nl, err = net.Listen(addrFamily, listenAddress)
		if err != nil {
			fmt.Printf("Error: %s\n", err)
			os.Exit(1)
		}
	} else {
		fmt.Printf("Error: %s\n", err)
		os.Exit(1)
	}
}

// run server
if err := srv.Serve(nl); err != nil {
	fmt.Printf("Error: %s\n", err)
}

reuseport/reuseport_test.go Outdated Show resolved Hide resolved
reuseport/reuseport_windows.go Outdated Show resolved Hide resolved
andrewheberle and others added 2 commits August 19, 2019 21:12
Co-Authored-By: Erik Dubbelboer <erik@dubbelboer.com>
Copy link
Collaborator

@erikdubbelboer erikdubbelboer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you 100% sure this is required. This pull request here says it's not needed in Go 1.12 anymore: valyala/tcplisten#2

reuseport/reuseport_windows.go Outdated Show resolved Hide resolved
@andrewheberle
Copy link
Contributor Author

Are you 100% sure this is required. This pull request here says it's not needed in Go 1.12 anymore: valyala/tcplisten#2

I saw that however I don't know what that comment is based on as the Go 1.12 release notes don't mention any new support/changes in "net" that could make this work now and my Windows Go 1.12 install fails the same way as noted in that PR.

Copy link
Collaborator

@erikdubbelboer erikdubbelboer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, 2 last modifications and then this can be merged.

reuseport/reuseport.go Show resolved Hide resolved
reuseport/reuseport_windows_test.go Show resolved Hide resolved
@erikdubbelboer erikdubbelboer merged commit 8ce231e into valyala:master Aug 30, 2019
@erikdubbelboer
Copy link
Collaborator

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants