fix(concurrency): return permits on request cancellation - #2064
Conversation
Signed-off-by: lixiang5 <lixiang5@sensetime.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughSummary by CodeRabbit
WalkthroughThe concurrency middleware now uses owned RAII token permits. Queue and middleware paths transfer permits through responses and wrap response bodies with shared logic. Tests cover token release after body drops, task cancellation, and queued request cancellation. ChangesConcurrency permit lifecycle
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant RequestMiddleware
participant PermitQueue
participant TokenPermit
participant TokenGuardBody
RequestMiddleware->>PermitQueue: request permit
PermitQueue->>TokenPermit: acquire tokens
PermitQueue-->>RequestMiddleware: send owned permit
RequestMiddleware->>TokenGuardBody: wrap response with permit
TokenGuardBody-->>TokenPermit: drop on completion or cancellation
TokenPermit->>PermitQueue: return tokens
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@model_gateway/src/middleware/concurrency.rs`:
- Around line 75-77: Update TokenGuardBody::new so it cannot create a
TokenPermit without first acquiring tokens: remove the constructor or make it
call TokenBucket::try_acquire and return Result<Self, ()>. Keep TokenPermit-only
wrapping private within the module, and run the type-design-analyzer for the
Rust type invariant and encapsulation review.
- Around line 320-329: Update the concurrency test around the spawned task to
synchronize on successful permit acquisition instead of relying on
tokio::task::yield_now(). Signal from inside the task immediately after
TokenPermit::try_acquire succeeds, await that signal before asserting
bucket.available_tokens() is zero, and preserve the existing abort and
token-restoration assertions.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 19977f10-0132-42a1-a7cf-5dee0b1edfd2
📒 Files selected for processing (1)
model_gateway/src/middleware/concurrency.rs
Signed-off-by: lixiang5 <lixiang5@sensetime.com>
Description
Problem
The concurrency middleware only transferred an acquired token into
TokenGuardBodyafternext.run(request).awaitcompleted. If the client disconnected or the request future was cancelled before a response body was created, the acquired token was never returned. Queued requests could also leak a token when the oneshot receiver disappeared after acquisition. With a zero refill rate, each leak permanently reduced concurrency capacity.Solution
Represent each successful acquisition as an RAII
TokenPermit. The middleware owns the permit while the request future runs, then moves it into the response body guard. Dropping the request future, response body, or an undeliverable queued permit now returns the token synchronously.Changes
TokenPermitto couple token acquisition and release.next.runso cancellation returns the token.Test Plan
cargo +nightly fmt --allcargo clippy --all-targets --all-features -- -D warningsenv -u RUST_LOG cargo testcargo test -p smg middleware::concurrency::tests --libChecklist
cargo +nightly fmtpassescargo clippy --all-targets --all-features -- -D warningspasses