Enhance TCP Fast Open (#310)

This commit is contained in:
risetechlab 2021-03-06 22:45:12 +08:00 committed by GitHub
parent e1a5392beb
commit ad1807dd99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 147 additions and 198 deletions

View file

@ -130,14 +130,13 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
}
if isTCPSocket(network) {
switch config.Tfo {
case SocketConfig_Enable:
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_FASTOPEN, 1); err != nil {
return newError("failed to set TCP_FASTOPEN_CONNECT=1").Base(err)
}
case SocketConfig_Disable:
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_FASTOPEN, 0); err != nil {
return newError("failed to set TCP_FASTOPEN_CONNECT=0").Base(err)
tfo := int(config.Tfo)
if tfo > 0 {
tfo = 1
}
if tfo >= 0 {
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_FASTOPEN, tfo); err != nil {
return newError("failed to set TCP_FASTOPEN_CONNECT=", tfo).Base(err)
}
}
}
@ -164,14 +163,9 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
}
}
if isTCPSocket(network) {
switch config.Tfo {
case SocketConfig_Enable:
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_FASTOPEN, 1); err != nil {
return newError("failed to set TCP_FASTOPEN=1").Base(err)
}
case SocketConfig_Disable:
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_FASTOPEN, 0); err != nil {
return newError("failed to set TCP_FASTOPEN=0").Base(err)
if config.Tfo >= 0 {
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_FASTOPEN, int(config.Tfo)); err != nil {
return newError("failed to set TCP_FASTOPEN=", config.Tfo).Base(err)
}
}
}