package tcplisten
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"syscall"
)
const (
soReusePort = 0x0F
tcpFastOpen = 0x17
)
func enableDeferAccept(fd int ) error {
if err := syscall .SetsockoptInt (fd , syscall .IPPROTO_TCP , syscall .TCP_DEFER_ACCEPT , 1 ); err != nil {
return fmt .Errorf ("cannot enable TCP_DEFER_ACCEPT: %s" , err )
}
return nil
}
func enableFastOpen(fd int ) error {
if err := syscall .SetsockoptInt (fd , syscall .SOL_TCP , tcpFastOpen , fastOpenQlen ); err != nil {
return fmt .Errorf ("cannot enable TCP_FASTOPEN(qlen=%d): %s" , fastOpenQlen , err )
}
return nil
}
const fastOpenQlen = 16 * 1024
func soMaxConn() (int , error ) {
data , err := ioutil .ReadFile (soMaxConnFilePath )
if err != nil {
if os .IsNotExist (err ) {
return syscall .SOMAXCONN , nil
}
return -1 , err
}
s := strings .TrimSpace (string (data ))
n , err := strconv .Atoi (s )
if err != nil || n <= 0 {
return -1 , fmt .Errorf ("cannot parse somaxconn %q read from %s: %s" , s , soMaxConnFilePath , err )
}
if n > 1 <<16 -1 {
n = 1 <<16 - 1
}
return n , nil
}
const soMaxConnFilePath = "/proc/sys/net/core/somaxconn"
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .