mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-28 03:07:08 +08:00
fix(upstream): do not fail the dial when TCP_USER_TIMEOUT cannot be set
upstreamDialControl returned the setsockopt error, so a kernel or socket that refuses TCP_USER_TIMEOUT made every dial through newUpstreamDialer fail, where the keep-alive options alone never do that. A failure is now logged and the dial goes on without the option, as the standard library does for the keep-alive options.
This commit is contained in:
1 parent
5854a1db43
commit
94e56dd192
2 files changed
+24
-5
No files matched your search
@@ -2,20 +2,27 @@
|
||||
|
||||
package repository
|
||||
|
||||
import "syscall"
|
||||
import (
|
||||
"log/slog"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// tcpUserTimeout is TCP_USER_TIMEOUT from Linux include/uapi/linux/tcp.h; the
|
||||
// standard library's syscall package does not define it on linux/amd64.
|
||||
const tcpUserTimeout = 0x12
|
||||
|
||||
// upstreamDialControl sets TCP_USER_TIMEOUT on each upstream socket
|
||||
// before it connects.
|
||||
func upstreamDialControl(_, _ string, raw syscall.RawConn) error {
|
||||
// before it connects. A failure is logged and the dial goes on without the
|
||||
// option, like the standard library does for the keep-alive options.
|
||||
func upstreamDialControl(network, _ string, raw syscall.RawConn) error {
|
||||
var sockErr error
|
||||
if err := raw.Control(func(fd uintptr) {
|
||||
sockErr = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, tcpUserTimeout, int(upstreamTCPUserTimeout.Milliseconds()))
|
||||
}); err != nil {
|
||||
return err
|
||||
sockErr = err
|
||||
}
|
||||
return sockErr
|
||||
if sockErr != nil {
|
||||
slog.Warn("upstream_tcp_user_timeout_not_set", "network", network, "error", sockErr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -71,3 +71,15 @@ func TestUpstreamDialerSetsKeepAliveAndUserTimeoutOnTheSocket(t *testing.T) {
|
||||
"TCP_USER_TIMEOUT": 30000,
|
||||
}, got)
|
||||
}
|
||||
|
||||
// A socket option the kernel refuses must not fail the dial: TCP_USER_TIMEOUT
|
||||
// does not apply to a UDP socket, so setting it there returns an error.
|
||||
func TestUpstreamDialerIgnoresUserTimeoutFailure(t *testing.T) {
|
||||
packetConn, err := net.ListenPacket("udp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = packetConn.Close() })
|
||||
|
||||
conn, err := newUpstreamDialer().DialContext(context.Background(), "udp", packetConn.LocalAddr().String())
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, conn.Close())
|
||||
}
|
||||
Reference in new issue
Block a user