Use deferred accept in reuseport.Listener. This should reduce the number of context switches for busy servers accepting a lot of new connections per second. Will test it, since google says TCP_DEFER_ACCEPT option is useless :)

This commit is contained in:
Aliaksandr Valialkin
2015-11-27 17:59:38 +02:00
parent 02a86866ef
commit 3eba841bb6
3 changed files with 21 additions and 2 deletions
+5
View File
@@ -72,6 +72,11 @@ func Listen(network, addr string) (l net.Listener, err error) {
return nil, &ErrNoReusePort{err}
}
if err = setTCPDeferAccept(fd); err != nil {
syscall.Close(fd)
return nil, err
}
if err = syscall.Bind(fd, sockaddr); err != nil {
syscall.Close(fd)
return nil, err
+6 -2
View File
@@ -2,6 +2,10 @@
package reuseport
import "syscall"
var reusePort = syscall.SO_REUSEPORT
func setTCPDeferAccept(fd int) error {
// BSD supports similar SO_ACCEPTFILTER option, but I have no access
// to BSD at the moment for proper implementation.
return nil
}
+10
View File
@@ -1,3 +1,13 @@
// +build linux
package reuseport
import (
"syscall"
)
var reusePort = 0x0F
func setTCPDeferAccept(fd int) error {
return syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.TCP_DEFER_ACCEPT, 2)
}