fix: recoverable client on connection failure#252
Conversation
pinodeca
left a comment
There was a problem hiding this comment.
Reviewed with Opus 4.8
Suggestions (non-blocking)
-
Unnecessary
Stringallocations. The new.to_string()conversions (inst_id,fn_name,inp,evt_name, etc.) aren't needed.rt.block_on(...)runs synchronously within the function scope, so the closure can capture the original&strarguments directly. This adds an allocation per call on the hot path ofdf.start()/df.signal()/df.cancel(). Minor, but easy to drop. -
is_connection_erroris broad. Matching bare substrings"connection"and"closed"can produce false positives (e.g. a business error mentioning "connection" or "closed"). The consequence is benign — just an extra pool re-creation — but worth a one-line comment noting that false positives are acceptable-by-design (cheap recovery, never incorrect). Anchoring on phrases like"connection refused"/"connection reset"/"connection closed"would tighten it if you'd prefer. -
Reentrancy caveat. The immutable borrow is held across
f's entireblock_on. Today none of the three callers re-enterwith_duroxide_client, so it's safe, but a future caller that did would panic. A short comment onwith_duroxide_clientwarning against reentrant calls would protect maintainers. -
Removed
set_varunsafety note. The original comment warning thatstd::env::set_varbecomesunsafein the 2024 edition was dropped. The crate isedition = "2021"so this is fine today, but that note was a useful landmine marker for a future edition bump — consider keeping it. -
Test redundancy. The
pg_test(test_is_connection_error_detects_failures) largely duplicates the#[cfg(test)]unit tests. Not a problem — just flagging that theis_connection_error_for_testwrapper exists solely to expose the fn through thepg_testharness.
Verdict
Approve with minor nits. None of the above is blocking; #1 (drop the allocations) and a comment for #2/#3 would be the highest-value follow-ups.
Replace OnceLock<Client> with thread_local RefCell<Option<Client>> that auto-resets on connection errors (broken pipe, pool timeout, connection refused, etc.). Subsequent calls transparently re-create the client pool instead of failing permanently for the session's lifetime. Add is_connection_error() heuristic with unit tests and pg_test integration coverage.
57400ac to
ee89db7
Compare
Split from #221 to ease review.
Replace
OnceLock<Client>withthread_local RefCell<Option<Client>>that auto-resets on connection errors (broken pipe, pool timeout, connection refused, etc.). Subsequent calls transparently re-create the client pool instead of failing permanently for the session's lifetime.Add
is_connection_error()heuristic with unit tests andpg_testintegration coverage.Files:
src/client.rs,src/lib.rsTests: 6 unit tests + 1 pg_test