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

server: add generic listen function #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

server: add generic listen function #54

wants to merge 1 commit into from

Conversation

joy4eg
Copy link

@joy4eg joy4eg commented Oct 17, 2018

Hey,

I guess it is nice to have a generic listen function because go1.11+ now has ListenConfig methods and it could be useful to setup some sockets options. For example:

func controlSetOpts(network, address string, c syscall.RawConn) (err error) {
	// Taken from /usr/include/asm-generic/socket.h
	const reuseport = 15
	return c.Control(func(fd uintptr) {
		// Try to set socket options ..
		if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
			log.Printf("cannot set SO_REUSEADDR on fd [%d]: %s", fd, err)
		}
		if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, reuseport, 1); err != nil {
			log.Printf("cannot set SO_REUSEPORT on fd [%d]: %s", fd, err)
		}
	})
}
...

lc := &net.ListenConfig{Control: controlSetOpts}
if conn, err := lc.ListenPacket(context.Background(), "udp", "127.0.0.1:7788"); err != nil {
	return err
} else {
	err = syslog.ListenOn(conn)
}

Any comments are welcome!

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

Successfully merging this pull request may close these issues.

1 participant