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

SO_REUSEPORT should supersede SO_REUSEADDR #81

Open
rahulghangas opened this issue Nov 10, 2020 · 2 comments
Open

SO_REUSEPORT should supersede SO_REUSEADDR #81

rahulghangas opened this issue Nov 10, 2020 · 2 comments
Assignees

Comments

@rahulghangas
Copy link

If my understanding is correct, SO_REUSEPORT will be the desired behaviour 99.9% of the time. In control_unix.go, for the following code

                err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
		if err != nil {
			return
		}

		err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
		if err != nil {
			return
		}

instead of setting (or trying to) both options, it should be a try/fail-safe scenario. We should try to set SO_REUSEPORT and if that fails, try to set SO_REUSEADDR. The current approach seems a bit redundant. Imo, it should look something like this,

		if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err == nil { 
			return
		}
                unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
@aarshkshah1992 aarshkshah1992 self-assigned this Nov 10, 2020
@aarshkshah1992
Copy link

@rahulghangas

It depends on the underlying OS:

https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ

The TLDR is set both to ensure everything works as expected. A relevant snippet from that answer is:

There is not much more to say about SO_REUSEPORT other than that it was added later than SO_REUSEADDR, that's why you will not find it in many socket implementations of other systems, which "forked" the BSD code before this option was added, and that there was no way to bind two sockets to exactly the same socket address in BSD prior to this option.

@rahulghangas
Copy link
Author

Hey, thanks for the reply.

I do understand that SO_REUSEPORT doesn't exist on some systems, which is why I suggested trying to set it and then setting SO_REUSEADDR if it errors out.

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

No branches or pull requests

2 participants