fix: unify stream idle timeout derivation and apply it to direct TCP relays - #127
Merged
Conversation
…relays Make config.DefaultStreamIdleTimeout derive from the StreamIdleTimeout(base) formula (single source of truth) and route the direct SOCKS5/HTTP CONNECT relay through the user-configured base timeout instead of a hardcoded 300s, matching the proxied path and server side.
…eout 120s instead of 300s at the default 30s base: reaps half-open peers sooner while staying generous enough for idle SSH/long-polling connections.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
此前存在两处不一致:
config.DefaultStreamIdleTimeout(硬编码 300s)与config.StreamIdleTimeout(base)(10 × base 派生公式)是两份独立定义,仅靠注释维持同步,存在漂移风险。DefaultStreamIdleTimeout(300s),不跟随用户配置的 base timeout——当用户配置timeout=60s时,代理路径与服务端的流空闲超时为 600s,而直连路径仍是 300s,行为不一致。StreamIdleTimeout采用 10 × base(默认 300s)偏大,半开/死连接需等 5 分钟才被回收。改动
统一超时来源 + 直连 TCP 跟随用户配置
config/types.go:DefaultStreamIdleTimeout由常量改为var,从StreamIdleTimeout(DefaultTimeout)公式导出,timeouts.go成为唯一事实来源。client/proxy/socks5.go:删除directRelayIdleTimeout常量;Socks5Server新增streamIdleTimeout字段;NewSocks5Server新增对应参数(≤0 时兜底DefaultStreamIdleTimeout);relayTCP新增idleTimeout参数。client/proxy/http_proxy.go:CONNECT direct 两处relayTCP改为config.StreamIdleTimeout(s.timeout)派生;base 兜底30 * time.Second统一引用config.DefaultTimeout。runner/runner.go:NewSocks5Server传入已派生的streamIdleTimeout(与代理路径同源)。默认流空闲超时 10× → 4×
config/timeouts.go:StreamIdleTimeout(base)由10 * base改为4 * base(默认 30s → 120s),同步更新注释。config/types.go:相关注释中的数值(300s → 120s)同步更新;DefaultStreamIdleTimeout由公式导出,自动跟随。config/timeouts_test.go:TestStreamIdleTimeout期望值更新。测试
config/timeouts_test.go:新增TestDefaultStreamIdleTimeoutDerived,守护"默认值恒等于公式在默认 base 下的结果"约束;更新TestStreamIdleTimeout期望值。client/proxy/direct_relay_test.go(新增):TestRelayTCPIdleTimeoutClosesBoth:验证 idle 超时后两端连接均被关闭(copy goroutine 不会悬挂)。TestRelayTCPHalfCloseAndCompletion:用两对 TCP(应用↔代理、代理↔远端)忠实模拟生产拓扑,验证 FIN 双向传播(CloseWrite half-close)、relay 完成后所有连接关闭。NewSocks5Server调用(含测试)。行为变化
验证
go build ./...、go vet、go test ./...(全仓)通过-count=5重复运行稳定make lint:0 issues