Repository navigation
Description
Summary
On the raw TCP proxy (lib/src/tcp.rs), TcpSession::backend is never set: it is initialised to None in both constructors and no code path assigns it. TcpSession::connect_to_backend gets the selected Rc<RefCell<Backend>> from BackendMap::backend_from_cluster_id, uses it only to copy backend_id into the session metrics, and drops it.
Every per-backend bookkeeping step that reads self.backend is therefore a no-op on TCP:
TcpSession::fail_backend_connection(called fromTcpSession::ready_innerwhen aConnectingbackend socket reports HUP): noBackend::failures += 1, noretry_policy.fail(), nobackend.connections.error, nobackend.down/backend.available/BackendDownevent.TcpSession::set_back_connected(Connected): noretry_policy.succeed(), nofailures = 0, noset_connection_time(the PeakEWMA input), nobackend.up/BackendUpon recovery.TcpSession::remove_backend: noBackend::dec_connections(), whileBackend::try_connectdidinc_connections()on every successful dial, soBackend::active_connectionsonly grows on TCP clusters.
The mux does charge the backend: it keeps the handle in its BackendRegistry and runs the same failure accounting in Mux::ready for a Position::Client(.., BackendStatus::Connecting(_)).
A synchronous connect(2) error is already charged by Backend::try_connect itself. The gap is the usual case of a non-blocking connect: connect(2) returns EINPROGRESS and the refusal arrives later as a HUP on the registered socket, which only fail_backend_connection can account.
Impact
- A TCP backend that refuses connections is never taken out of selection by the retry policy and never marked down: round-robin keeps dialing it, and each session that lands on it pays a failed connect and a retry before reaching a live backend (or exhausts
CONN_RETRIESwhen several backends are dead). backend.connections.error,backend.down,backend.up,backend.availableand theBackendDown/BackendUpevents are never emitted for raw TCP backends, and the cluster availability surface described indoc/health_checks.md("each TCP connect failure on the data path armsBackend::retry_policy") does not hold for the TCP proxy without an active health check.Backend::active_connectionsleaks upward, which skews connection-based load balancing (LeastLoaded,PowerOfTwowith the connections metric).
Reproduction
A TCP cluster with two backends, the first on a closed port, the second listening, round-robin; 8 sequential sessions through the TCP listener. All 8 sessions are served in both cases (each failed connect is retried on the live backend inside the session), but with RUST_LOG=debug the worker's Connecting cluster_0 -> (addr, ..) lines show:
| dials to the refused backend | backend.connections.error (cluster row) |
|
|---|---|---|
main (b3f2c78) |
8 of 8 sessions | absent |
| with the session keeping its backend handle | 1 (then excluded for its back-off window) | 1 |
The e2e test test_tcp_refused_backend_is_accounted_and_avoided (e2e/src/tests/tcp_tests.rs) in the linked pull request encodes this scenario and fails on main.
Fix
Store the selected handle in TcpSession::connect_to_backend (self.backend = Some(Rc::clone(&backend))), so that the existing accounting in fail_backend_connection, set_back_connected and remove_backend runs as written.
Activity
fix(tcp): charge refused connects to the backend the TCP session dialed
fix(tcp): charge refused connects to the backend the TCP session dialed
fix(tcp): charge refused connects to the backend the TCP session dialed
fix(tcp): charge refused connects to the backend the TCP session dial…