feat(quotas): add graceful shutdown drain for /api/quotas - #1042
Conversation
Closes CalloraOrg#883 Implements a SIGTERM-safe drain for in-flight /api/quotas requests so the process never closes database connections while quota count queries are still executing. Changes: - src/routes/quotas/counts.ts Creates a module-level createInFlightDrainTracker("quotas") instance (quotasDrainTracker) and applies its middleware to the router. During shutdown the middleware sets Connection: close on new responses so clients reconnect after the process restarts. - src/app.ts Re-exports quotasDrainTracker from counts.ts so application entry-points can import it without reaching into the route module. - src/index.ts Imports quotasDrainTracker and registers its subsystem in the shutdownSubsystems array passed to createGracefulShutdownHandler. The shutdown sequence now drains /api/quotas alongside the existing gateway-proxy, api-keys, and webhook-dispatcher subsystems. - src/routes/quotas/counts.test.ts (new) 14 focused tests covering: * quotasDrainTracker shape (middleware + subsystem exported) * subsystem.name = "quotas" * awaitIdle resolves immediately when idle * awaitIdle waits for in-flight request to complete (drain path) * beginShutdown sets Connection: close on subsequent responses * middleware is transparent for normal 200 responses * GET /api/quotas/counts happy-path counts (total/pending/approved/rejected) * 401 without auth * Cross-user isolation * Correlation ID echo * Error propagation to errorHandler Coverage on counts.ts: 92.85% stmts | 100% funcs | 100% branches tested
|
@mrteeednut007-dotcom Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
|
feat(quotas): add graceful shutdown drain for /api/quotas Closes #883 Summary Implements a SIGTERM-safe drain for in-flight /api/quotas requests so the process never closes What changed ┌─────────────────────────────┬─────────────────────────────────────────────────────────────┐ How it works On SIGTERM / SIGINT the existing createGracefulShutdownHandler iterates shutdownSubsystems and
No new dependencies. No changes to the public API shape. Testing Tests: 14 passed Covers:
Lint passes on all changed files. Pre-existing warnings in src/index.ts (createRateLimiter, |
|
LGTM ✅ green CI, clean work — merging! |
|
feat(quotas): add graceful shutdown drain for /api/quotas Closes #890 Summary Implements a SIGTERM-safe drain for in-flight /api/quotas requests so the process never closes What changed ┌─────────────────────────────┬─────────────────────────────────────────────────────────────┐ How it works On SIGTERM / SIGINT the existing createGracefulShutdownHandler iterates shutdownSubsystems and
No new dependencies. No changes to the public API shape. Testing Tests: 14 passed Covers:
Lint passes on all changed files. Pre-existing warnings in src/index.ts (createRateLimiter, closes #890 |
Closes #883
Summary
Implements a SIGTERM-safe drain for in-flight
/api/quotasrequests so the process never closes database connections while quota count queries are still executing.What changed
src/routes/quotas/counts.tsquotasDrainTrackerviacreateInFlightDrainTracker("quotas"), applies its middleware to the routersrc/app.tsquotasDrainTrackerso entry-points can import it without reaching into the route modulesrc/index.tsquotasDrainTracker.subsystemin theshutdownSubsystemsarray passed tocreateGracefulShutdownHandlersrc/routes/quotas/counts.test.tsHow it works
On
SIGTERM/SIGINTthe existingcreateGracefulShutdownHandleriteratesshutdownSubsystemsand callsbeginShutdown()+awaitIdle()on each. AddingquotasDrainTracker.subsystemto that list means:beginShutdown()— the tracker stops accepting new work and setsConnection: closeon responses so clients reconnect after restart.awaitIdle()— the shutdown handler waits until the in-flight counter reaches zero before proceeding to close the database pool.Testing
Lint passes on all changed files. Pre-existing warnings in
src/index.ts(createRateLimiter,createSettlementReconciliationJobunused) are unchanged frommain.