cap-concurrency-for-expensive-endpoints
Use when one expensive endpoint exhausts the shared pool and starves everything else — give it a dedicated bounded lane and its own connection pool.
- License 许可
- MIT
- 最近更新
- 2026-11-12
- Stars
- ★ 81
- Forks
- 17
01 前言 Front Matter
- name
- cap-concurrency-for-expensive-endpoints
- description
- Use when one expensive endpoint exhausts the shared pool and starves everything else — give it a dedicated bounded lane and its own connection pool.
02 资源文件 3
- 许可证LICENSE1.0 KB
- 参考文档SOURCE.md365 B
- 参考文档TRUST.auto.yaml2.2 KB
03 SKILL.md 正文
Cap concurrency for expensive endpoints
A few expensive endpoints share the same connection pool and thread budget as cheap ones. Run concurrently, they consume every connection and starve health checks and fast reads. Give expensive work its own small, bounded lane.
Procedure
- Identify expensive endpoints by measured cost: p99 over 1s, or query rows/CPU per call far above the mean. Cross-reference the endpoint with
pg_stat_statementstotal time. - Give each a dedicated concurrency limit, separate from the general request limiter:
REPORT_SEM = asyncio.Semaphore(4)
async def report(req):
if not await acquire_now(REPORT_SEM): # non-blocking try-acquire
raise HTTPException(429, "report lane full", headers={"Retry-After": "5"})
try:
...
finally:
REPORT_SEM.release() # release on every path
- Or use a bulkhead: a separate connection pool or worker pool for expensive work, so it can never take the general pool's connections:
report_engine = create_engine(url, pool_size=4, pool_timeout=2)
- Return
429withRetry-Afterwhen the lane is full, rather than queueing until everything times out. - Move genuinely heavy work — exports, big reports — to a background job: return
202 Acceptedwith a job id and let the client poll; do not hold an HTTP connection for 30s. - Set a per-endpoint timeout shorter than the request budget so a runaway report is cancelled and releases its slot.
- Watch the lane's queue depth and the general pool's wait time; if expensive endpoints still starve the general pool, the bulkhead is not actually separate.
Pitfalls
- A global rate limiter that does not distinguish endpoints, so cheap requests are throttled while expensive ones slip through elsewhere.
- A semaphore acquired but not released on the error path, leaking slots until the lane is permanently full.
- A bulkhead that shares the same underlying connection pool, so it is not a bulkhead at all.
429withoutRetry-After, so clients retry immediately and hold the lane saturated.- Streaming a 30s report through the request path instead of making it a job.
- Setting the lane size above the database's useful concurrency, so the cap protects nothing.
Verification
hey -z 30s -c 50 http://localhost:8080/report &
hey -z 30s -c 50 http://localhost:8080/ping
curl -s localhost:9090/metrics | grep -E "report_inflight|pool_wait_seconds"
# pass: /ping p99 unchanged, /report returns 429 past the cap, pool wait near zero
Report the endpoint's concurrency cap and pool, the 429 behavior, and the p99 of a cheap endpoint while the expensive one is saturated.
04 同仓库更多 Skills
recipe-performance
Diagnose a specific MetaMask user flow's real-app performance and Sentry critical-user-flow telemetry with deterministic mm-harness recipes. Use for CUF boundaries, cold/warm/background-resume behavior, latency regressions, lifecycle tags, duplicate or blocking network requests, long tasks, layout/paint cost, screen redraws, React rerenders, or browser trace analysis where tests alone cannot prove the real app.
mexc-spot-websocket
MEXC Spot WebSocket workflows for public streams, protobuf handling, depth maintenance, listen keys, and private streams without exposing API keys.
mexc-spot-rest
MEXC Spot REST workflows for public data, signed account requests, orders, wallet/admin actions, and secure signing without exposing API keys.
mexc-futures-websocket
MEXC Futures WebSocket workflows for public streams, private account/order/position streams, ping/pong, subscriptions, and depth maintenance.
0xklen-version-and-refresh-sanctions-lists
暂无简介
verify-tests-cover-changed-lines
Use when a PR adds code that the existing suite may not exercise. Measures patch coverage — the share of newly changed lines hit by tests — before approving.