feat(trade): pool TradeContext connections instead of opening one per call - #135
Open
hogan-yuan wants to merge 1 commit into
Open
feat(trade): pool TradeContext connections instead of opening one per call#135hogan-yuan wants to merge 1 commit into
hogan-yuan wants to merge 1 commit into
Conversation
… call Every trade tool call (15 call sites across trade.rs and portfolio.rs) opened a fresh WebSocket connection via TradeContext::new(), unlike QuoteContext which has always been pooled per identity via ws_pool.rs. Under concurrent load this plausibly contributes to elevated error rates on trade-related tools (connection-limit/timeout errors), and is architecturally inconsistent with how quote calls are handled. Extracted the pooling engine (LRU + idle-TTL + per-key init serialization + eviction-on-error) out of ws_pool.rs into a generic context_pool.rs, and added trade_pool.rs as a TradeContext pool built on it, mirroring ws_pool.rs exactly (same identity-keying, own Prometheus metrics, own env vars for tuning). Rewired all 15 TradeContext::new() call sites through mctx.get_trade_context(), with eviction-on-error wired into every trade error path so a broken pooled connection doesn't get reused. Added a compile-time lint (trade_tools_use_pooled_context_constructor) mirroring the existing quote-side one, so any future trade tool that bypasses the pool fails CI. Split out from PR #134 (which bundled this with unrelated fixes) to keep it independently reviewable.
hogan-yuan
added a commit
that referenced
this pull request
Aug 31, 2026
Splits out everything pooling-related from this PR to keep it independently reviewable: reverts src/tools/trade.rs, src/tools/portfolio.rs, src/main.rs, src/metrics.rs, and src/ws_pool.rs to their pre-pooling state, removes src/context_pool.rs and src/trade_pool.rs, and drops McpContext::get_trade_context()/evict_trade_context() plus the trade-pooling compile-time lint from src/tools/mod.rs. The pooling work itself now lives on feat/trade-context-pooling (#135), unchanged from what was here. This PR is left with: the option_quote symbol-format documentation fix, the candlesticks count validation/ boundary-retry fix, the error_hint structured-error-code improvements, and the version bump.
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
Split out of #134, which bundled this with unrelated fixes.
Every trade tool call (15 call sites across
trade.rsandportfolio.rs) opened a fresh WebSocket connection viaTradeContext::new(), unlikeQuoteContextwhich has always been pooled per identity viaws_pool.rs. Under concurrent load this plausibly contributes to elevated error rates on trade-related tools (connection-limit/timeout errors), and is architecturally inconsistent with how quote calls are handled.ws_pool.rsinto a genericcontext_pool.rs.trade_pool.rsas aTradeContextpool built on it, mirroringws_pool.rsexactly (same identity-keying, own Prometheus metrics, own env vars for tuning).TradeContext::new()call sites throughmctx.get_trade_context(), with eviction-on-error wired into every trade error path.trade_tools_use_pooled_context_constructor) mirroring the existing quote-side one, so any future trade tool that bypasses the pool fails CI.Test plan
cargo buildcleancargo +nightly fmt --checkcleancargo clippy --all-features --all-targets -- -D warningscleancargo test— 207 passed, 0 failed#[ignore]d stress tests (stress_pool_bounded_concurrency,stress_high_concurrency) run manually against real thread contention and pass