diff options
author | Aliaksandr Valialkin <valyala@gmail.com> | 2015-11-27 17:59:38 +0200 |
---|---|---|
committer | Aliaksandr Valialkin <valyala@gmail.com> | 2015-11-27 17:59:38 +0200 |
commit | 3eba841bb697fe952d962728d09e558e15e8f5be (patch) | |
tree | c65c5e322a44e3bf32eabebfd4531bb684059052 /reuseport | |
parent | Increased maximum in-memory file size from 1MB to 16MB (diff) | |
download | fasthttp-3eba841bb697fe952d962728d09e558e15e8f5be.tar.gz fasthttp-3eba841bb697fe952d962728d09e558e15e8f5be.tar.bz2 fasthttp-3eba841bb697fe952d962728d09e558e15e8f5be.zip |
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 :)
Diffstat (limited to 'reuseport')
-rw-r--r-- | reuseport/reuseport.go | 5 | ||||
-rw-r--r-- | reuseport/reuseport_bsd.go | 8 | ||||
-rw-r--r-- | reuseport/reuseport_linux.go | 10 |
3 files changed, 21 insertions, 2 deletions
diff --git a/reuseport/reuseport.go b/reuseport/reuseport.go index ee33f7d..69d861a 100644 --- a/reuseport/reuseport.go +++ b/reuseport/reuseport.go @@ -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 diff --git a/reuseport/reuseport_bsd.go b/reuseport/reuseport_bsd.go index 89b44cb..f9cca96 100644 --- a/reuseport/reuseport_bsd.go +++ b/reuseport/reuseport_bsd.go @@ -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 +} diff --git a/reuseport/reuseport_linux.go b/reuseport/reuseport_linux.go index 483aba1..c7f31db 100644 --- a/reuseport/reuseport_linux.go +++ b/reuseport/reuseport_linux.go @@ -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) +} |