Skip to content

Commit

Permalink
Add tcpKeepAliveInterval in transport sockopt (#754)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahmad Karimi <ak12hastam@gmail.com>
Co-authored-by: Shelikhoo <xiaokangwang@outlook.com>
  • Loading branch information
3 people committed Oct 12, 2021
1 parent ef4c638 commit 4bb6170
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
15 changes: 9 additions & 6 deletions infra/conf/transport_internet.go
Expand Up @@ -489,6 +489,8 @@ type SocketConfig struct {
AcceptProxyProtocol bool `json:"acceptProxyProtocol"`
DomainStrategy string `json:"domainStrategy"`
DialerProxy string `json:"dialerProxy"`

TCPKeepAliveInterval int32 `json:"tcpKeepAliveInterval"`
}

// Build implements Buildable.
Expand Down Expand Up @@ -529,12 +531,13 @@ func (c *SocketConfig) Build() (*internet.SocketConfig, error) {
}

return &internet.SocketConfig{
Mark: c.Mark,
Tfo: tfo,
Tproxy: tproxy,
DomainStrategy: dStrategy,
AcceptProxyProtocol: c.AcceptProxyProtocol,
DialerProxy: c.DialerProxy,
Mark: c.Mark,
Tfo: tfo,
Tproxy: tproxy,
DomainStrategy: dStrategy,
AcceptProxyProtocol: c.AcceptProxyProtocol,
DialerProxy: c.DialerProxy,
TcpKeepAliveInterval: c.TCPKeepAliveInterval,
}, nil
}

Expand Down
56 changes: 34 additions & 22 deletions transport/internet/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions transport/internet/config.proto
Expand Up @@ -90,5 +90,8 @@ message SocketConfig {
bool accept_proxy_protocol = 7;

DomainStrategy domain_strategy = 8;

string dialer_proxy = 9;

int32 tcp_keep_alive_interval = 10;
}
12 changes: 12 additions & 0 deletions transport/internet/sockopt_linux.go
Expand Up @@ -57,6 +57,12 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf
return newError("failed to set TCP_FASTOPEN_CONNECT=", tfo).Base(err)
}
}

if config.TcpKeepAliveInterval != 0 {
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
return newError("failed to set TCP_KEEPINTVL", err)
}
}
}

if config.Tproxy.IsEnabled() {
Expand All @@ -81,6 +87,12 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig)
return newError("failed to set TCP_FASTOPEN=", tfo).Base(err)
}
}

if config.TcpKeepAliveInterval != 0 {
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
return newError("failed to set TCP_KEEPINTVL", err)
}
}
}

if config.Tproxy.IsEnabled() {
Expand Down

0 comments on commit 4bb6170

Please sign in to comment.