shenyu icon indicating copy to clipboard operation
shenyu copied to clipboard

[Task] Add distributed MCP invocation and concurrency quotas

Open BobSong-dev opened this issue 1 month ago • 0 comments

Background

Parent #7949. This task owns distributed MCP business-quota admission. Per-process resource admission is not a distributed tenant or subject business quota across gateway replicas.

Existing capabilities and reuse

Reuse trusted identity and deny-first tools authorization, Redis and existing rate-limiter infrastructure, current deadlines, #7951 resource bounds, #7956 decisions and existing audit and metrics. Review #7702 and PR #7848 atomic and idempotent accounting patterns where applicable, but MCP invocation counts are not LLM tokens.

Implementation scope

  • Implement tenant and subject invocation-count windows first; explicitly scoped tool and server dimensions and multilevel concurrency are added only where required by the chosen policy, without allowing cross-tenant keys.
  • Provide atomic distributed admission, deterministic window expiry and separate count-debit and concurrency-release semantics.
  • Use unique bounded concurrency leases, renewal where necessary, crash expiry and idempotent release. Define failed admissions, cancellation, retry and unknown upstream outcome accounting.
  • Validate trusted configuration; define Redis failure behavior explicitly and preserve underlying authentication, authorization and resource limits.
  • Ship shadow and enforce modes using #7956, isolated shadow state, audit decisions and bounded metrics in the initial feature.

Non-goals

A second rate-limiter platform, elapsed-time billing or quota, LLM token and cost budgets (#7958), or replaying side-effecting tools. Existing deadlines remain the execution-time boundary.

Acceptance criteria

  • [ ] Two or more gateways cannot admit beyond the configured shared count or concurrency policy through races.
  • [ ] Count consumption and permit release are independently correct; duplicate completion cannot refund counts or release another request's lease.
  • [ ] Crashed holders and expired windows recover within documented bounds; renewal cannot resurrect removed policies or released leases.
  • [ ] Redis outage, configuration updates and cancellation have explicit outcomes; state and key cardinality and shadow retention are bounded.
  • [ ] Shadow does not debit production counters; enforce rejects at the actual execution boundary with matching audit and metrics.

Tests

Real Redis multi-instance concurrent admission, window boundaries, renewal and release races, process-loss and expiry and Redis outage and recovery; authenticated tenant isolation, configuration invalidation, shadow and enforce transitions and actual MCP side-effect counts. Avoid timing-only assertions where controllable clocks or barriers are possible.

Dependencies

Completed identity and request/terminal-context contracts, #7951 and #7956; #7950 audit vocabulary. Reliable atomic admission lessons should be reused by #7958, not a hard requirement to create another accounting store.

BobSong-dev avatar Oct 21 '26 11:10 BobSong-dev