feat(ssh): add SSH keepalive and auto-reconnect for proxy#37
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #37 +/- ##
==========================================
+ Coverage 80.92% 80.97% +0.04%
==========================================
Files 40 41 +1
Lines 2726 2901 +175
==========================================
+ Hits 2206 2349 +143
- Misses 397 423 +26
- Partials 123 129 +6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Problem: When running 'xsql proxy', if the SSH tunnel connection to the remote server is interrupted (e.g., network outage), the proxy becomes unusable. Users only discover this when they attempt to use the proxy and receive timeout errors. Solution: - Add SSH keepalive mechanism (keepalive@openssh.com probes) to detect dead connections proactively - Introduce ReconnectDialer that wraps SSH Client with automatic reconnection on connection failure or keepalive death detection - Integrate ReconnectDialer into the proxy command for seamless recovery - Emit status events (connected/disconnected/reconnecting/reconnected) to stderr for user visibility Changes: - internal/ssh/options.go: Add KeepaliveInterval and KeepaliveCountMax - internal/ssh/client.go: Add SendKeepalive(), Alive(), nil-safe DialContext - internal/ssh/reconnect.go: New ReconnectDialer with keepalive monitoring - internal/app/conn.go: Add ResolveReconnectableSSH, refactor resolveSSHOptions - cmd/xsql/proxy.go: Use ReconnectDialer with status logging - docs/ssh-proxy.md: Document keepalive and auto-reconnect behavior Testing: - 17 new tests for ReconnectDialer (reconnect_test.go) - 4 new tests for SSH client keepalive methods - 4 new tests for app layer reconnectable SSH - Fix pre-existing test timeouts (TestConnect_DefaultPort/User) - New code coverage: ssh 87.8%, app 85.1%, proxy 94.2% Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
fb7a2d9 to
01bba5e
Compare
|
zx06
pushed a commit
that referenced
this pull request
Mar 25, 2026
…nect coalescing Fix remaining issues from PR #37 (SSH keepalive & auto-reconnect): 1. Eliminate code duplication: ResolveConnection() now uses resolveSSHOptions() instead of duplicating SSH options construction (fixes SonarQube 3.1% > 3%) 2. Context-aware SSH Connect: Replace ssh.Dial() with net.DialContext() + ssh.NewClientConn() so context cancellation/timeout interrupts both TCP connection and SSH handshake phases 3. Reconnect coalescing: Multiple concurrent DialContext/keepalive failures trigger a single reconnect instead of racing. Lock released during retry loop to avoid blocking other DialContext callers 4. Keepalive recovery: Keepalive monitoring restarts after failed reconnect attempts, preventing permanent loss of health detection 5. In-process SSH test server: testutil_test.go provides a real SSH server (using golang.org/x/crypto/ssh) for testing connect, keepalive, tunnel forwarding, and reconnection — runs on all platforms without Docker Test coverage: internal/ssh 87.6% → 93.9%, internal/app 85.1% → 85.5% Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.


问题
启动
xsql proxy后,若 SSH 隧道与远端之间的网络断开,proxy 无法自动恢复。用户只在重试连接时才看到超时错误:根因
方案
引入 SSH Keepalive + ReconnectDialer 两个机制:
SSH Keepalive
keepalive@openssh.com探测连接存活(默认 30s 间隔)ReconnectDialer
变更文件
internal/ssh/options.goKeepaliveInterval、KeepaliveCountMax字段internal/ssh/client.goSendKeepalive()、Alive()、nil-safeDialContext()internal/ssh/reconnect.gointernal/app/conn.goResolveReconnectableSSH,重构resolveSSHOptionscmd/xsql/proxy.godocs/ssh-proxy.md测试覆盖率
internal/sshinternal/appinternal/proxy新增测试:
TestConnect_DefaultPort/DefaultUser测试超时问题不变更
xsql query/xsql schema dump不受影响(短生命周期连接)