All Challenges
3 PointsIntermediate
00:00

Async Promise Pool with Concurrency Limiter & Jittered Retries

Build an async worker queue that executes hundreds of async tasks with strict concurrency throttling, retry backoff with jitter, and AbortController.

### Problem Overview When fetching 1,000 API endpoints or processing batch files, firing all promises at once exhausts network sockets and triggers `429 Too Many Requests` or Node.js OOM crashes. Build a `PromisePool` class that manages task execution with strict concurrency throttling, intelligent retry backoff with jitter, and cancellation tokens. ### Functional Requirements 1. **`add<T>(task: () => Promise<T>, options?: TaskOptions): Promise<T>`**: - Enqueues an async task and returns a Promise that resolves when the task finishes. 2. **Concurrency Control (`concurrency: number`)**: - At no point should more than `concurrency` tasks be active in flight simultaneously. 3. **Automatic Retries with Jitter**: - If a task rejects, retry up to `retries` times using exponential backoff: \[ \text{delay} = \text{baseDelay} \times 2^{\text{attempt}} + \text{randomJitter} \] 4. **Cancellation (`signal?: AbortSignal`)**: - Support standard `AbortSignal` to cancel queued tasks before execution. 5. **`drain(): Promise<void>`**: - Resolves when all pending and active tasks have finished. ### Code Smell Guardrails - **Unbounded Queues**: Must track memory pressure and prevent dangling unhandled rejections. - **Starvation**: FIFO queue ordering must be strictly maintained.
AI Review Rubric Dimensions (1–10 Points):
  • Strict concurrency enforcement
  • Exponential backoff with randomized jitter
  • Drain resolution lifecycle
  • AbortSignal handling
solution.tsTYPESCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
36 lines•731 characters•
AI Review Ready