feat: concurrency-bounded fair webhook dispatch worker#925
Merged
1nonlypiece merged 2 commits intoJun 29, 2026
Merged
Conversation
- Add src/services/boundedWebhookDispatcher.ts (390 lines) - BoundedWebhookDispatcher class with configurable WEBHOOK_MAX_CONCURRENCY - Round-robin fair scheduling across subscribers (no monopolisation) - Skips subscribers with OPEN circuit breakers in scheduling - Recursive drain() after each slot freed for continuous throughput - Preserves existing circuit-breaker, backoff, dead-letter logic exactly - Refactor dispatchWebhookEvent() 70 lines → 18 lines in webhooks.ts - Export breakerCache, inFlightProbes, deadLetter for dispatcher access - Add disciplr_webhook_dispatch_in_flight Prometheus gauge - Add disciplr_webhook_dispatch_queue_depth Prometheus gauge - Update /metrics endpoint to read dispatcher state on each scrape - Add src/tests/webhooks.concurrency.test.ts (17 tests, 450+ lines) - Test in-flight ceiling never exceeded across burst scenarios - Test round-robin fairness — slow subscriber cannot monopolise budget - Test circuit-breaker-open subscribers skipped gracefully - Test all-breakers-open exits drain without infinite loop - Test gauge accuracy at all dispatch phases - Create docs/webhooks.md (555+ lines, 16 sections) - No regressions in 9 existing webhook test files Closes Disciplr-Org#840
|
@Danielobito009 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! 🚀 |
Contributor
|
fair dispatch worker is in, queue should stay bounded under load now |
10 tasks
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
Implements Issue #840 — Concurrency-bounded fair webhook dispatch
worker preventing unbounded socket creation during burst events.
Problem
dispatchWebhookEvent()usedPromise.all()with no concurrencylimit. A burst of vault lifecycle events fanned to many subscribers
could open hundreds of simultaneous sockets, exhausting file
descriptors and starving Horizon/S3 outbound calls.
Solution
A
BoundedWebhookDispatcherwith configurable in-flight ceilingand per-subscriber round-robin fair scheduling.
Files Changed
src/services/boundedWebhookDispatcher.ts(new, 390 lines)BoundedWebhookDispatcherclassenqueue()— adds delivery to per-subscriber queuedrain()— fills slots up tomaxConcurrencynextFairDelivery()— round-robin across subscribersdrain()call infinallyfor continuous throughputsrc/services/webhooks.ts(modified)dispatchWebhookEvent()refactored 70 → 18 linesPromise.all()breakerCache,inFlightProbes,deadLettersrc/services/metrics.ts(modified)disciplr_webhook_dispatch_in_flightgaugedisciplr_webhook_dispatch_queue_depthgaugesrc/tests/webhooks.concurrency.test.ts(new, 450+ lines)17 tests across 5 suites
docs/webhooks.md(new, 555+ lines, 16 sections)Test Coverage (17 tests)
Configuration
WEBHOOK_MAX_CONCURRENCY=10 # defaultPrometheus Metrics
disciplr_webhook_dispatch_in_flightdisciplr_webhook_dispatch_queue_depthFair Scheduling Example
Dispatch order (maxConcurrency=3):
→ Sub A e1, Sub B e1, Sub C e1
→ Sub A e2, Sub B e2
→ Sub A e3
Breaking Change
dispatchWebhookEvent()return value changed fromWebhookDeliveryResult[]to[]— delivery now happensasynchronously in the background.
Verification
while (inFlight < max)loopchecked < totalguard innextFairDeliveryCloses #840