forked from database64128/tfo-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfo_listen_generic.go
45 lines (39 loc) · 994 Bytes
/
tfo_listen_generic.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
//go:build freebsd || linux || windows
package tfo
import (
"context"
"errors"
"net"
"syscall"
)
// defaultBacklog is Go std's listen(2) backlog.
// We use this as the default TFO backlog.
const defaultBacklog = 4096
func (lc *ListenConfig) listenTFO(ctx context.Context, network, address string) (net.Listener, error) {
ctrlFn := lc.Control
backlog := lc.Backlog
if backlog == 0 {
backlog = defaultBacklog
}
llc := *lc
llc.Control = func(network, address string, c syscall.RawConn) (err error) {
if ctrlFn != nil {
if err = ctrlFn(network, address, c); err != nil {
return err
}
}
if cerr := c.Control(func(fd uintptr) {
err = setTFOListenerWithBacklog(fd, backlog)
}); cerr != nil {
return cerr
}
if err != nil {
if !lc.Fallback || !errors.Is(err, ErrUnsupported) {
return wrapSyscallError("setsockopt(TCP_FASTOPEN)", err)
}
runtimeListenNoTFO.Store(true)
}
return nil
}
return llc.ListenConfig.Listen(ctx, network, address)
}