Repository navigation
perf(ssh): share one session per route, use the probe channel, keep libssh2 off the pool - #4573
Merged
Merged
Conversation
…ibssh2 off the pool Connections to the same database through the same bastion, user, sign-in and secrets share one authenticated session (SSHSessionRegistry, SSHSessionRoute, LibSSH2SharedSession); each keeps its own listener in LibSSH2Tunnel. Concurrent connects wait on the one sign-in under way, each until its own deadline. The destination check keeps its channel for the first client, so a connect opens one channel instead of two. Channel opens queue per session and an abandoned open is settled before the next, since libssh2 keeps one pending open per session. Closing one tunnel ends only its relays (SSHSessionPump.endRelay). The TCP connect, handshake, sign-in, jump opens and SFTP start run on a queue per attempt instead of the cooperative pool. SSH secrets are read through DeferredSecretRead, only those the sign-in method uses. The SSH socket is dialed RFC 8305 style (SSHTransportDial), on macOS and iOS, with TCP_NODELAY (SSHTransportSocketOptions, renamed from SSHTransportKeepAlive), and the tunnel listen backlog is SOMAXCONN.
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.
Summary
SSHSessionRegistry,SSHSessionRoute,LibSSH2SharedSession). Connections that start together wait on the one sign-in already running, so host key, passphrase and code prompts come once. A waiter that is cancelled or reaches its own deadline leaves without failing the others. When the shared transport dies, every connection on it starts its own recovery. Each connection keeps its own listener, clients and forward diagnosis (LibSSH2Tunnel).session->open_state). An unreachable database behind a firewall that drops packets holds that open until the server's own connect gives up, often minutes later. Sharing across destinations would make every other database wait behind it, so it is not done here.SSHSessionPump.endRelay. A relay waiting for the channel's send window never reads its local socket's end of stream.SSHConnectionAttempt.runBlocking). The destination check runs on the tunnel's relay queue. Cancelling still works through the attempt's existing socket shutdown.buildEffectiveConnectionandsshCredentials(remote file, remote SQLite) read throughDeferredSecretRead, and only the secrets the sign-in method uses (SSHStoredSecretNeeds).SSHTransportDial, which followscore/crates/net/src/dial.rs: address families alternate, a new attempt starts every 250 ms or as soon as one fails, all attempts share one deadline, and the first to connect wins. The iOS app dials the same way. It used to give every address its own 10 s timeout.SSHTransportSocketOptions, renamed fromSSHTransportKeepAlive). A jump hop's next session runs over a socketpair, so the first hop's TCP socket is the only one that needs it.SOMAXCONN. On a busy Mac the30-clients-one-tunnelmatrix cells failed with "Connection reset by peer" about one run in three, onmaintoo. They passed 12 of 12 runs after the change.Test plan
Measured with the matrix harness (
scripts/probes/ssh-tunnel-matrix), using the app's own tunnel code against thev10OpenSSH server behind a toxiproxy that adds 25 ms each way. Channel opens are counted from thectype direct-tcpiplines in sshd's DEBUG1 log, sign-ins from itsAcceptedlines. Before isorigin/mainf5e5ea6.sample): cooperative threads blocked inpollSSHTransportDialTests,SSHTransportSocketOptionsTests(renamed, NODELAY added),SSHChannelHandOffTests, twoSSHSessionPumpTestscases forendRelay,SSHSessionRegistryTests,SSHSessionRouteTests,SSHStoredSecretNeedsTests.scripts/probes/check-ssh-tunnel-matrix.sh --app macos: 86 of 87 cells as expected. The one miss was the backlog reset described above, which the backlog change fixes.Closes #4545