From 6629d331fa7c291c8dd73f1781ee244333ab8ea7 Mon Sep 17 00:00:00 2001 From: xqzr <34030394+xqzr@users.noreply.github.com> Date: Tue, 1 Apr 2025 02:27:06 +0800 Subject: [PATCH 1/4] Sockopt: Add Windows `TCP_KEEPINTVL` & `TCP_KEEPIDLE` --- transport/internet/sockopt_windows.go | 32 ++++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/transport/internet/sockopt_windows.go b/transport/internet/sockopt_windows.go index cbd3b41e..57b15c47 100644 --- a/transport/internet/sockopt_windows.go +++ b/transport/internet/sockopt_windows.go @@ -16,6 +16,8 @@ const ( IP_MULTICAST_IF = 9 IPV6_MULTICAST_IF = 9 IPV6_V6ONLY = 27 + TCP_KEEPIDLE = 3 + TCP_KEEPINTVL = 17 ) func setTFO(fd syscall.Handle, tfo int) error { @@ -61,11 +63,18 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf if err := setTFO(syscall.Handle(fd), config.ParseTFOValue()); err != nil { return err } - if config.TcpKeepAliveIdle > 0 { - if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { - return errors.New("failed to set SO_KEEPALIVE", err) + if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 { + if config.TcpKeepAliveInterval > 0 { + if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_TCP, TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil { + return errors.New("failed to set TCP_KEEPINTVL", err) + } } - } else if config.TcpKeepAliveIdle < 0 { + if config.TcpKeepAliveIdle > 0 { + if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_TCP, TCP_KEEPIDLE, int(config.TcpKeepAliveIdle)); err != nil { + return errors.New("failed to set TCP_KEEPIDLE", err) + } + } + } else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 { if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil { return errors.New("failed to unset SO_KEEPALIVE", err) } @@ -80,11 +89,18 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) if err := setTFO(syscall.Handle(fd), config.ParseTFOValue()); err != nil { return err } - if config.TcpKeepAliveIdle > 0 { - if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { - return errors.New("failed to set SO_KEEPALIVE", err) + if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 { + if config.TcpKeepAliveInterval > 0 { + if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_TCP, TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil { + return errors.New("failed to set TCP_KEEPINTVL", err) + } } - } else if config.TcpKeepAliveIdle < 0 { + if config.TcpKeepAliveIdle > 0 { + if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_TCP, TCP_KEEPIDLE, int(config.TcpKeepAliveIdle)); err != nil { + return errors.New("failed to set TCP_KEEPIDLE", err) + } + } + } else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 { if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil { return errors.New("failed to unset SO_KEEPALIVE", err) } From 3938c0be40f31a0d85e3be03ba9fe6359fa5fb99 Mon Sep 17 00:00:00 2001 From: xqzr <34030394+xqzr@users.noreply.github.com> Date: Tue, 1 Apr 2025 02:42:52 +0800 Subject: [PATCH 2/4] Add `SO_KEEPALIVE` --- transport/internet/sockopt_windows.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/transport/internet/sockopt_windows.go b/transport/internet/sockopt_windows.go index 57b15c47..c452aeab 100644 --- a/transport/internet/sockopt_windows.go +++ b/transport/internet/sockopt_windows.go @@ -74,6 +74,9 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf return errors.New("failed to set TCP_KEEPIDLE", err) } } + if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { + return errors.New("failed to set SO_KEEPALIVE", err) + } } else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 { if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil { return errors.New("failed to unset SO_KEEPALIVE", err) @@ -100,6 +103,9 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) return errors.New("failed to set TCP_KEEPIDLE", err) } } + if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { + return errors.New("failed to set SO_KEEPALIVE", err) + } } else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 { if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil { return errors.New("failed to unset SO_KEEPALIVE", err) From b016b7cb6568f09260ca8eb2e4233621541e8917 Mon Sep 17 00:00:00 2001 From: xqzr <34030394+xqzr@users.noreply.github.com> Date: Tue, 1 Apr 2025 02:54:18 +0800 Subject: [PATCH 3/4] RM `SO_KEEPALIVE` --- transport/internet/sockopt_windows.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/transport/internet/sockopt_windows.go b/transport/internet/sockopt_windows.go index c452aeab..57b15c47 100644 --- a/transport/internet/sockopt_windows.go +++ b/transport/internet/sockopt_windows.go @@ -74,9 +74,6 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf return errors.New("failed to set TCP_KEEPIDLE", err) } } - if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { - return errors.New("failed to set SO_KEEPALIVE", err) - } } else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 { if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil { return errors.New("failed to unset SO_KEEPALIVE", err) @@ -103,9 +100,6 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) return errors.New("failed to set TCP_KEEPIDLE", err) } } - if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { - return errors.New("failed to set SO_KEEPALIVE", err) - } } else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 { if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil { return errors.New("failed to unset SO_KEEPALIVE", err) From 7cff8746711417b57d1d248bcffd8dc0b44a2655 Mon Sep 17 00:00:00 2001 From: xqzr <34030394+xqzr@users.noreply.github.com> Date: Tue, 1 Apr 2025 03:01:46 +0800 Subject: [PATCH 4/4] Add `SO_KEEPALIVE` --- transport/internet/sockopt_windows.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/transport/internet/sockopt_windows.go b/transport/internet/sockopt_windows.go index 57b15c47..c452aeab 100644 --- a/transport/internet/sockopt_windows.go +++ b/transport/internet/sockopt_windows.go @@ -74,6 +74,9 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf return errors.New("failed to set TCP_KEEPIDLE", err) } } + if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { + return errors.New("failed to set SO_KEEPALIVE", err) + } } else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 { if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil { return errors.New("failed to unset SO_KEEPALIVE", err) @@ -100,6 +103,9 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) return errors.New("failed to set TCP_KEEPIDLE", err) } } + if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { + return errors.New("failed to set SO_KEEPALIVE", err) + } } else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 { if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil { return errors.New("failed to unset SO_KEEPALIVE", err)