-
Notifications
You must be signed in to change notification settings - Fork 9
/
listen_linux.go
49 lines (43 loc) · 1.14 KB
/
listen_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2019-2022 Moritz Fain
// Moritz Fain <[email protected]>
// +build linux
package tcpserver
import (
"fmt"
"syscall"
)
const (
soReusePort = 0x0F
tcpFastOpen = 0x17
)
type controlFunc func(network, address string, c syscall.RawConn) error
func applyListenSocketOptions(lc *ListenConfig) controlFunc {
return func(network, address string, c syscall.RawConn) error {
var err error
c.Control(func(fd uintptr) {
if lc.SocketReusePort {
err = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, soReusePort, 1)
if err != nil {
err = fmt.Errorf("unable to set SO_REUSEPORT option: %s", err)
}
}
if lc.SocketFastOpen {
qlen := lc.SocketFastOpenQueueLen
if qlen <= 0 {
qlen = 256
}
err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, tcpFastOpen, qlen)
if err != nil {
err = fmt.Errorf("unable to set TCP_FASTOPEN option: %s", err)
}
}
if lc.SocketDeferAccept {
err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_DEFER_ACCEPT, 1)
if err != nil {
err = fmt.Errorf("unable to set TCP_DEFER_ACCEPT option: %s", err)
}
}
})
return err
}
}