dynamic-throttled-queue 2.2.0-rc.e9be515 → 2.2.1-rc.b3d1777
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -6
- package/dist/adaptive-rate.d.ts +48 -0
- package/dist/adaptive-rate.js +237 -0
- package/dist/adaptive-rate.js.map +1 -0
- package/dist/dynamic-throttled-queue.d.ts +29 -0
- package/dist/dynamic-throttled-queue.js +26 -4
- package/dist/dynamic-throttled-queue.js.map +1 -1
- package/dist/retry-backoff.d.ts +2 -0
- package/dist/retry-backoff.js +31 -0
- package/dist/retry-backoff.js.map +1 -0
- package/dist/scheduler.d.ts +2 -2
- package/dist/scheduler.js +157 -173
- package/dist/scheduler.js.map +1 -1
- package/package.json +1 -1
- package/dist/rate-controller.d.ts +0 -22
- package/dist/rate-controller.js +0 -43
- package/dist/rate-controller.js.map +0 -1
package/README.md
CHANGED
|
@@ -26,7 +26,7 @@ throttle(() => {
|
|
|
26
26
|
});
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
Callbacks can return `false` to signal a failure (used for dynamic rate adjustment and retry). Async callbacks (returning a Promise) are also supported — rejections and `false` resolutions count as failures. By default, every failure reduces the adaptive rate; use `rateOutcomeClassifier` when only selected failures should do so.
|
|
29
|
+
Callbacks can return `false` to signal a failure (used for dynamic rate adjustment and retry). Async callbacks (returning a Promise) are also supported — rejections and `false` resolutions count as failures. By default, every failure reduces the adaptive rate; use `rateOutcomeClassifier` when only selected failures should do so. Use `retryClassifier` to separately decide whether a failure is eligible for retry.
|
|
30
30
|
|
|
31
31
|
Each callback receives an execution context containing the queue-owned `AbortSignal`. Existing zero-argument callbacks remain supported. Use the signal to cooperatively cancel in-flight work:
|
|
32
32
|
|
|
@@ -39,6 +39,8 @@ throttle(async ({ signal }) => {
|
|
|
39
39
|
|
|
40
40
|
Set `concurrency` to bound callbacks that are still awaiting asynchronous completion. This limit is independent of the request-start rate; omitting it preserves the existing unlimited in-flight behavior.
|
|
41
41
|
|
|
42
|
+
Set `maxQueueSize` to bound accepted work. Capacity is reserved from enqueue through terminal success or failure, including pending callbacks, active callbacks, and retries. A full queue makes `enqueue()` throw synchronously in v2; unlimited capacity remains the default. In v3, a full queue will return `false` instead.
|
|
43
|
+
|
|
42
44
|
## Options
|
|
43
45
|
|
|
44
46
|
| Param | Type | Default | Description |
|
|
@@ -50,14 +52,19 @@ Set `concurrency` to bound callbacks that are still awaiting asynchronous comple
|
|
|
50
52
|
| `errors_per_interval` | `number` | `5` | Positive integer error threshold per interval before adjusting rate |
|
|
51
53
|
| `back_off` | `boolean` | `false` | Back off for 1 full interval when error threshold is hit |
|
|
52
54
|
| `retry` | `number` | `0` | Non-negative integer number of times to retry failed callbacks |
|
|
55
|
+
| `retryBackoff` | `RetryBackoff` | — | Per-retry fixed, linear, or exponential delay policy; omit for immediate retries |
|
|
53
56
|
| `concurrency` | `number` | — | Maximum callbacks awaiting asynchronous completion; omit for no limit |
|
|
57
|
+
| `maxQueueSize` | `number` | — | Maximum accepted callbacks not yet terminal, including pending, active, and retried work; omit for no limit |
|
|
54
58
|
| `compact_threshold` | `number` | `512` | Non-negative integer minimum dead slots before internal queue compaction triggers; `0` compacts at the earliest eligible point |
|
|
55
59
|
| `rateStrategy` | `RateStrategy` | `linear` | Pure policy that requests the next rate and an optional backoff after each observation window |
|
|
56
60
|
| `rateOutcomeClassifier` | `RateOutcomeClassifier` | — | Decides whether a failed callback outcome contributes to adaptive-rate error counting |
|
|
57
61
|
| `adjustmentTiming` | `"interval" \| "settled"` | `"interval"` | Defines whether rate decisions use outcomes settled each interval or complete started-attempt sets |
|
|
62
|
+
| `retryClassifier` | `RetryClassifier` | — | Decides whether a failed callback outcome is eligible for another attempt |
|
|
58
63
|
| `onRateChange` | `(rate: number) => void` | — | Called when the current rate changes |
|
|
59
64
|
|
|
60
|
-
`retry`, `errors_per_interval`, and `
|
|
65
|
+
`retry`, `errors_per_interval`, `compact_threshold`, and `maxQueueSize` reject fractional and non-finite values. `errors_per_interval` must be at least `1`; `retry` and `compact_threshold` may be `0`. `retryBackoff.baseDelay` and optional `maxDelay` are finite non-negative millisecond values (fractional values are accepted); optional `jitter` is finite from `0` through `1`.
|
|
66
|
+
|
|
67
|
+
`retryBackoff` is `{ strategy: "fixed" | "linear" | "exponential"; baseDelay: number; maxDelay?: number; jitter?: number; random?: () => number }`. The first retry has index `1`: fixed uses `baseDelay`, linear uses `baseDelay × retryIndex`, and exponential uses `baseDelay × 2^(retryIndex - 1)`. The calculated delay is capped at `maxDelay`, when supplied, then optional symmetric percentage jitter is applied and capped again. `random` makes jitter deterministic for tests; a thrown, non-finite, or out-of-range result safely falls back to no jitter. Retry backoff begins when the failed attempt settles and is independent of adaptive-rate `back_off`.
|
|
61
68
|
|
|
62
69
|
### Adaptive-rate timing
|
|
63
70
|
|
|
@@ -75,7 +82,9 @@ With `adjustmentTiming: "settled"`, an observation window contains every callbac
|
|
|
75
82
|
| `resume()` | `() => void` | Resume a paused queue with a fresh pacing and observation window |
|
|
76
83
|
| `stop()` | `() => void` | Stop processing the queue immediately |
|
|
77
84
|
| `abort()` | `() => void` | Terminally discard queued work and signal active callbacks |
|
|
78
|
-
| `
|
|
85
|
+
| `waitForIdle()` | `() => Promise<void>` | Resolves when all pending, active, and delayed-retry work has completed |
|
|
86
|
+
| `getState()` | `() => QueueState` | Returns a frozen point-in-time snapshot of queue state and counters |
|
|
87
|
+
| `pending` | `number` (readonly) | Number of callbacks waiting in the queue, including delayed retries |
|
|
79
88
|
|
|
80
89
|
```ts
|
|
81
90
|
const throttle = createThrottledQueue({ min_rpi: 5, interval: 1000 });
|
|
@@ -91,18 +100,22 @@ throttle.stop(); // halt processing
|
|
|
91
100
|
| State | Pending work and enqueue | Active work | Scheduling and lifecycle operations |
|
|
92
101
|
| ----- | ------------------------ | ----------- | ----------------------------------- |
|
|
93
102
|
| Running | Pending work can start; new callbacks are accepted. | Continues normally. | `pause()` retains work and stops new starts. `stop()` retains work and clears timers. `abort()` is terminal. |
|
|
94
|
-
| Paused | Pending work, newly enqueued callbacks, and retries are retained but do not start. | Continues and settles normally. | `resume()` restarts pacing at the current rate and begins a fresh adaptive-rate observation window. `pause()` is idempotent. `stop()` clears the paused state. |
|
|
95
|
-
| Stopped | Pending work is retained. A later enqueue restarts scheduling. | Continues and settles normally. | `pause()` and `resume()` are no-ops. `stop()` is idempotent. |
|
|
103
|
+
| Paused | Pending work, newly enqueued callbacks, and retries are retained but do not start. | Continues and settles normally. | `resume()` restarts pacing at the current rate and begins a fresh adaptive-rate observation window. Delayed retries freeze their remaining delay and continue only after resume. `pause()` is idempotent. `stop()` clears the paused state. |
|
|
104
|
+
| Stopped | Pending work is retained. A later enqueue restarts scheduling. | Continues and settles normally. | Delayed retries freeze their remaining delay; the later enqueue resumes them. `pause()` and `resume()` are no-ops. `stop()` is idempotent. |
|
|
96
105
|
| Aborted | Pending work is discarded and future enqueues throw. | Receives the shared abort signal and may finish cooperatively. | `pause()`, `resume()`, `stop()`, and `abort()` do not restart scheduling; `abort()` is idempotent. |
|
|
97
106
|
|
|
98
107
|
While paused, callback outcomes do not contribute to adaptive-rate adjustment. A failed active callback may still create a configured retry, but that retry remains pending until `resume()`.
|
|
99
108
|
|
|
100
109
|
In settled timing, `pause()` discards the in-progress observation window rather than making a partial or late rate decision. `stop()` retains its non-restarting behavior, and `abort()` remains terminal: neither produces post-stop or post-abort settled-window accounting.
|
|
101
110
|
|
|
102
|
-
`stop()` clears scheduler timers and retains callbacks that have not started. It cannot cancel an active asynchronous callback; if that callback later succeeds, returns `false`, or rejects, its settlement does not restart scheduling. A retry created by a failed active callback is retained with the pending work. Enqueueing another callback resumes the queue.
|
|
111
|
+
`stop()` clears scheduler timers and retains callbacks that have not started. It cannot cancel an active asynchronous callback; if that callback later succeeds, returns `false`, or rejects, its settlement does not restart scheduling. A retry created by a failed active callback is retained with the pending work. Enqueueing another callback resumes the queue and any frozen delayed retries.
|
|
103
112
|
|
|
104
113
|
`abort()` is terminal and idempotent. It clears scheduler timers, discards pending callbacks, and aborts the one shared signal supplied to active callbacks. Future enqueue attempts throw. Cancellation is cooperative: callbacks that ignore the signal can continue running, but their later success or failure does not retry work or affect adaptive-rate accounting.
|
|
105
114
|
|
|
115
|
+
`getState()` returns a frozen `QueueState` snapshot. Each call is independent; mutations to the returned object do not affect the queue. Fields: `rate` (current rate), `pending` (queued + delayed retries), `active` (callbacks executing), `state` (`"running"` | `"paused"` | `"stopped"` | `"aborted"`), and monotonic lifetime counters `started`, `succeeded`, `failed`, `retried`, `rateIncreases`, `rateDecreases`. `started`/`succeeded`/`failed` count callback attempts: a retry is a new attempt; a failure is counted even when a later retry succeeds. `retried` increments only when another attempt is actually scheduled. Rate-direction counters match applied rate changes and `onRateChange` notifications exactly. Counters do not change for settlements after `abort()`.
|
|
116
|
+
|
|
117
|
+
`waitForIdle()` returns a `Promise<void>` that resolves once all pending callbacks, active executions, and delayed retries have reached a terminal outcome. If the queue is already idle the promise resolves immediately. Each call is one-shot: a later enqueue does not affect a promise that has already resolved. Multiple simultaneous callers all resolve at the same idle transition without retaining waiter state. A callback failure that triggers a retry never exposes a transient idle transition between the failed attempt and the retry. `stop()` retains pending work, so existing waiters remain pending until that work completes after a later enqueue resumes the queue. `abort()` discards pending work but waiters remain pending until every active callback settles; post-abort settlements do not create new retry work. A paused queue resolves waiters only when both pending and active work are zero.
|
|
118
|
+
|
|
106
119
|
## Examples
|
|
107
120
|
|
|
108
121
|
### Basic
|
|
@@ -181,6 +194,23 @@ const throttle = createThrottledQueue({
|
|
|
181
194
|
|
|
182
195
|
The normalized outcomes are `{ kind: "returned-false" }`, `{ kind: "thrown", error }`, and `{ kind: "rejected", error }`. Omitting the classifier preserves the default behavior: every failure reduces the adaptive rate. If the classifier throws, the original failure safely counts as rate-reducing and no separate classifier error is surfaced.
|
|
183
196
|
|
|
197
|
+
### Retry classification
|
|
198
|
+
|
|
199
|
+
`retryClassifier` receives the same normalized failure outcome and a one-based attempt number, including the initial callback start. It must return literal `true` for the failure to be retried; any other value makes it permanent. The configured `retry` value remains the hard cap on additional attempts, so the classifier is not called after that budget is exhausted. If it throws, the queue preserves legacy behavior and retries subject to the remaining budget.
|
|
200
|
+
|
|
201
|
+
Retry eligibility is independent from adaptive-rate accounting. A failure can be retryable, rate-reducing, both, or neither:
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
const throttle = createThrottledQueue({
|
|
205
|
+
min_rpi: 1,
|
|
206
|
+
interval: 1000,
|
|
207
|
+
retry: 2,
|
|
208
|
+
retryClassifier: (outcome, attempt) =>
|
|
209
|
+
outcome.kind === "rejected" && attempt < 3,
|
|
210
|
+
rateOutcomeClassifier: outcome => outcome.kind === "rejected",
|
|
211
|
+
});
|
|
212
|
+
```
|
|
213
|
+
|
|
184
214
|
### Rate strategies
|
|
185
215
|
|
|
186
216
|
The default `linear` strategy preserves the adaptive behavior above: it changes the rate by one request per interval. Use `aimd` when a capacity signal should reduce throughput more quickly while recovery remains gradual. AIMD increases by a fixed amount after a clean eligible observation window and reduces the rate by a multiplier when the error threshold is reached.
|
|
@@ -258,6 +288,24 @@ throttle(async () => {
|
|
|
258
288
|
});
|
|
259
289
|
```
|
|
260
290
|
|
|
291
|
+
### Retry backoff
|
|
292
|
+
|
|
293
|
+
```ts
|
|
294
|
+
const throttle = createThrottledQueue({
|
|
295
|
+
min_rpi: 5,
|
|
296
|
+
interval: 1000,
|
|
297
|
+
retry: 3,
|
|
298
|
+
retryBackoff: {
|
|
299
|
+
strategy: "exponential",
|
|
300
|
+
baseDelay: 250,
|
|
301
|
+
maxDelay: 10_000,
|
|
302
|
+
jitter: 0.2,
|
|
303
|
+
},
|
|
304
|
+
});
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
Delayed retries remain pending, return to the normal queue tail when due, and then obey normal scheduler pacing. Equal due times preserve the order their retry delays were scheduled. `abort()` discards delayed retries along with other pending work.
|
|
308
|
+
|
|
261
309
|
## Migration from v1
|
|
262
310
|
|
|
263
311
|
- `errors_per_second` removed — use `errors_per_interval` (counts errors per full interval window, not per second).
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { AdjustmentTiming, RateFailureOutcome, RateOutcomeClassifier, RateStrategy } from "./dynamic-throttled-queue.js";
|
|
2
|
+
/**
|
|
3
|
+
* Whether callbacks may start. `open` allows paced starts, `held` blocks starts until the module
|
|
4
|
+
* reopens them, and `idle` means the module is not observing and waits for `start()`.
|
|
5
|
+
*/
|
|
6
|
+
export type Pacing = "idle" | "open" | "held";
|
|
7
|
+
/** Reports how one started callback settled: `undefined` for success, otherwise the failure. */
|
|
8
|
+
export type SettlementReporter = (outcome: RateFailureOutcome | undefined) => void;
|
|
9
|
+
export type AdaptiveRateOptions = {
|
|
10
|
+
min_rpi: number;
|
|
11
|
+
max_rpi: number;
|
|
12
|
+
interval: number;
|
|
13
|
+
errors_per_interval: number;
|
|
14
|
+
back_off: boolean;
|
|
15
|
+
adjustmentTiming: AdjustmentTiming;
|
|
16
|
+
rateStrategy: RateStrategy;
|
|
17
|
+
rateOutcomeClassifier?: RateOutcomeClassifier;
|
|
18
|
+
onRateChange?: (rate: number) => void;
|
|
19
|
+
};
|
|
20
|
+
export type AdaptiveRateHost = {
|
|
21
|
+
/** Whether accepted work is waiting to start. */
|
|
22
|
+
hasPendingWork: () => boolean;
|
|
23
|
+
/** Begins fresh pacing: the next paced start is one spacing from now. */
|
|
24
|
+
resumeStarts: () => void;
|
|
25
|
+
/** Cancels the next paced start, or reschedules it `deferBy` ms beyond one spacing from now. */
|
|
26
|
+
holdStarts: (deferBy?: number) => void;
|
|
27
|
+
/** Observation ended: cancel the next paced start. */
|
|
28
|
+
idle: () => void;
|
|
29
|
+
};
|
|
30
|
+
export type AdaptiveRate = {
|
|
31
|
+
readonly rate: number;
|
|
32
|
+
readonly rateIncreases: number;
|
|
33
|
+
readonly rateDecreases: number;
|
|
34
|
+
readonly pacing: Pacing;
|
|
35
|
+
/** Ends a pause, then begins observing if idle and work is pending. */
|
|
36
|
+
start: () => void;
|
|
37
|
+
/** Reports that every queued callback has started. */
|
|
38
|
+
drained: () => void;
|
|
39
|
+
/** Ends observation. Settled timing discards its in-progress window. */
|
|
40
|
+
stop: () => void;
|
|
41
|
+
/** Stops and discards the observation. Settlements are ignored until `start()` or `stop()`. */
|
|
42
|
+
pause: () => void;
|
|
43
|
+
/** Reports a callback start. Call the returned reporter once when that callback settles. */
|
|
44
|
+
started: () => SettlementReporter;
|
|
45
|
+
/** Throws the strategy failure that permanently halted adaptive rate, if one occurred. */
|
|
46
|
+
throwIfFailed: () => void;
|
|
47
|
+
};
|
|
48
|
+
export declare function createAdaptiveRate(options: AdaptiveRateOptions, host: AdaptiveRateHost): AdaptiveRate;
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
const ignoreSettlement = () => { };
|
|
2
|
+
function validateDecision(decision) {
|
|
3
|
+
if (typeof decision !== "object" || decision === null) {
|
|
4
|
+
throw new TypeError("rate strategy must return a decision object");
|
|
5
|
+
}
|
|
6
|
+
const candidate = decision;
|
|
7
|
+
if (!Number.isFinite(candidate.nextRate) || !Number.isInteger(candidate.nextRate)) {
|
|
8
|
+
throw new TypeError("rate strategy must return a finite integer nextRate");
|
|
9
|
+
}
|
|
10
|
+
if (typeof candidate.shouldBackOff !== "boolean") {
|
|
11
|
+
throw new TypeError("rate strategy must return a boolean shouldBackOff");
|
|
12
|
+
}
|
|
13
|
+
return candidate;
|
|
14
|
+
}
|
|
15
|
+
/** Decides every interval from the outcomes that settled during it, whenever those callbacks started. */
|
|
16
|
+
function intervalTiming(context) {
|
|
17
|
+
let tick;
|
|
18
|
+
let run = 0;
|
|
19
|
+
function adjust() {
|
|
20
|
+
tick = undefined;
|
|
21
|
+
const current = run;
|
|
22
|
+
const hold = context.decide();
|
|
23
|
+
if (current !== run)
|
|
24
|
+
return;
|
|
25
|
+
if (hold)
|
|
26
|
+
context.hold(context.interval);
|
|
27
|
+
else if (context.pacing === "held")
|
|
28
|
+
context.reopen();
|
|
29
|
+
tick = setTimeout(adjust, context.interval);
|
|
30
|
+
}
|
|
31
|
+
function stop() {
|
|
32
|
+
clearTimeout(tick);
|
|
33
|
+
tick = undefined;
|
|
34
|
+
run++;
|
|
35
|
+
context.idle();
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
start() {
|
|
39
|
+
context.resume();
|
|
40
|
+
tick = setTimeout(adjust, context.interval);
|
|
41
|
+
},
|
|
42
|
+
started: () => context.record,
|
|
43
|
+
drained: stop,
|
|
44
|
+
stop,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** Decides once every callback that started during one collection interval has settled. */
|
|
48
|
+
function settledTiming(context) {
|
|
49
|
+
let timer;
|
|
50
|
+
let window = 0;
|
|
51
|
+
let collecting = false;
|
|
52
|
+
let outstanding = 0;
|
|
53
|
+
function open() {
|
|
54
|
+
timer = undefined;
|
|
55
|
+
if (!context.hasPendingWork()) {
|
|
56
|
+
context.idle();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
window++;
|
|
60
|
+
collecting = true;
|
|
61
|
+
outstanding = 0;
|
|
62
|
+
context.resume();
|
|
63
|
+
timer = setTimeout(close, context.interval);
|
|
64
|
+
}
|
|
65
|
+
function close() {
|
|
66
|
+
timer = undefined;
|
|
67
|
+
collecting = false;
|
|
68
|
+
context.hold();
|
|
69
|
+
if (outstanding === 0)
|
|
70
|
+
finish();
|
|
71
|
+
}
|
|
72
|
+
function finish() {
|
|
73
|
+
const current = window;
|
|
74
|
+
const hold = context.decide();
|
|
75
|
+
if (current !== window)
|
|
76
|
+
return;
|
|
77
|
+
if (hold)
|
|
78
|
+
timer = setTimeout(endBackoff, context.interval);
|
|
79
|
+
else
|
|
80
|
+
open();
|
|
81
|
+
}
|
|
82
|
+
function endBackoff() {
|
|
83
|
+
timer = undefined;
|
|
84
|
+
context.endBackoff();
|
|
85
|
+
if (context.hasPendingWork())
|
|
86
|
+
open();
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
start: open,
|
|
90
|
+
started() {
|
|
91
|
+
if (!collecting)
|
|
92
|
+
return ignoreSettlement;
|
|
93
|
+
const startedIn = window;
|
|
94
|
+
outstanding++;
|
|
95
|
+
return outcome => {
|
|
96
|
+
if (startedIn !== window)
|
|
97
|
+
return;
|
|
98
|
+
context.record(outcome);
|
|
99
|
+
outstanding--;
|
|
100
|
+
if (!collecting && outstanding === 0)
|
|
101
|
+
finish();
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
drained() { },
|
|
105
|
+
stop() {
|
|
106
|
+
clearTimeout(timer);
|
|
107
|
+
timer = undefined;
|
|
108
|
+
collecting = false;
|
|
109
|
+
window++;
|
|
110
|
+
context.idle();
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export function createAdaptiveRate(options, host) {
|
|
115
|
+
const { min_rpi, max_rpi, errors_per_interval, back_off, rateStrategy, rateOutcomeClassifier, onRateChange } = options;
|
|
116
|
+
let rate = Math.ceil((max_rpi + min_rpi) / 2);
|
|
117
|
+
let rateIncreases = 0;
|
|
118
|
+
let rateDecreases = 0;
|
|
119
|
+
let errorCount = 0;
|
|
120
|
+
let wasBackedOff = false;
|
|
121
|
+
let ignoringSettlements = false;
|
|
122
|
+
let pacing = "idle";
|
|
123
|
+
let failure;
|
|
124
|
+
function isRateReducing(outcome) {
|
|
125
|
+
try {
|
|
126
|
+
return rateOutcomeClassifier?.(outcome) ?? true;
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function applyRate(next) {
|
|
133
|
+
if (next === rate)
|
|
134
|
+
return;
|
|
135
|
+
if (next > rate)
|
|
136
|
+
rateIncreases++;
|
|
137
|
+
else
|
|
138
|
+
rateDecreases++;
|
|
139
|
+
rate = next;
|
|
140
|
+
onRateChange?.(rate);
|
|
141
|
+
}
|
|
142
|
+
function decide() {
|
|
143
|
+
const observation = Object.freeze({
|
|
144
|
+
currentRate: rate,
|
|
145
|
+
minRate: min_rpi,
|
|
146
|
+
maxRate: max_rpi,
|
|
147
|
+
errorCount,
|
|
148
|
+
errorThreshold: errors_per_interval,
|
|
149
|
+
hasPendingWork: host.hasPendingWork(),
|
|
150
|
+
wasBackedOff,
|
|
151
|
+
});
|
|
152
|
+
let decision;
|
|
153
|
+
try {
|
|
154
|
+
decision = validateDecision(rateStrategy(observation));
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
failure = { error };
|
|
158
|
+
timing.stop();
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
const hold = back_off && decision.shouldBackOff;
|
|
162
|
+
errorCount = 0;
|
|
163
|
+
wasBackedOff = hold;
|
|
164
|
+
applyRate(Math.min(max_rpi, Math.max(min_rpi, decision.nextRate)));
|
|
165
|
+
return hold;
|
|
166
|
+
}
|
|
167
|
+
const context = {
|
|
168
|
+
interval: options.interval,
|
|
169
|
+
get pacing() {
|
|
170
|
+
return pacing;
|
|
171
|
+
},
|
|
172
|
+
hasPendingWork: host.hasPendingWork,
|
|
173
|
+
record(outcome) {
|
|
174
|
+
if (!ignoringSettlements && outcome && isRateReducing(outcome))
|
|
175
|
+
errorCount++;
|
|
176
|
+
},
|
|
177
|
+
decide,
|
|
178
|
+
resume() {
|
|
179
|
+
pacing = "open";
|
|
180
|
+
host.resumeStarts();
|
|
181
|
+
},
|
|
182
|
+
hold(deferBy) {
|
|
183
|
+
pacing = "held";
|
|
184
|
+
host.holdStarts(deferBy);
|
|
185
|
+
},
|
|
186
|
+
reopen() {
|
|
187
|
+
pacing = "open";
|
|
188
|
+
},
|
|
189
|
+
endBackoff() {
|
|
190
|
+
wasBackedOff = false;
|
|
191
|
+
},
|
|
192
|
+
idle() {
|
|
193
|
+
wasBackedOff = false;
|
|
194
|
+
pacing = "idle";
|
|
195
|
+
host.idle();
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
const timing = options.adjustmentTiming === "settled" ? settledTiming(context) : intervalTiming(context);
|
|
199
|
+
return {
|
|
200
|
+
get rate() {
|
|
201
|
+
return rate;
|
|
202
|
+
},
|
|
203
|
+
get rateIncreases() {
|
|
204
|
+
return rateIncreases;
|
|
205
|
+
},
|
|
206
|
+
get rateDecreases() {
|
|
207
|
+
return rateDecreases;
|
|
208
|
+
},
|
|
209
|
+
get pacing() {
|
|
210
|
+
return pacing;
|
|
211
|
+
},
|
|
212
|
+
start() {
|
|
213
|
+
ignoringSettlements = false;
|
|
214
|
+
if (pacing !== "idle" || !host.hasPendingWork())
|
|
215
|
+
return;
|
|
216
|
+
timing.start();
|
|
217
|
+
},
|
|
218
|
+
drained() {
|
|
219
|
+
timing.drained();
|
|
220
|
+
},
|
|
221
|
+
stop() {
|
|
222
|
+
ignoringSettlements = false;
|
|
223
|
+
timing.stop();
|
|
224
|
+
},
|
|
225
|
+
pause() {
|
|
226
|
+
timing.stop();
|
|
227
|
+
errorCount = 0;
|
|
228
|
+
ignoringSettlements = true;
|
|
229
|
+
},
|
|
230
|
+
started: () => timing.started(),
|
|
231
|
+
throwIfFailed() {
|
|
232
|
+
if (failure)
|
|
233
|
+
throw failure.error;
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
//# sourceMappingURL=adaptive-rate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adaptive-rate.js","sourceRoot":"","sources":["../src/adaptive-rate.ts"],"names":[],"mappings":"AAmFA,MAAM,gBAAgB,GAAuB,GAAG,EAAE,GAAE,CAAC,CAAC;AAEtD,SAAS,gBAAgB,CAAC,QAAiB;IACzC,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACtD,MAAM,IAAI,SAAS,CAAC,6CAA6C,CAAC,CAAC;IACrE,CAAC;IACD,MAAM,SAAS,GAAG,QAAgC,CAAC;IACnD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAA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B,MAAM,CAAC,IAAI,EAAE,CAAC;QAChB,CAAC;QACD,KAAK;YACH,MAAM,CAAC,IAAI,EAAE,CAAC;YACd,UAAU,GAAG,CAAC,CAAC;YACf,mBAAmB,GAAG,IAAI,CAAC;QAC7B,CAAC;QACD,OAAO,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,EAAE;QAC/B,aAAa;YACX,IAAI,OAAO;gBAAE,MAAM,OAAO,CAAC,KAAK,CAAC;QACnC,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
export type RetryBackoff = {
|
|
2
|
+
strategy: "fixed" | "linear" | "exponential";
|
|
3
|
+
baseDelay: number;
|
|
4
|
+
maxDelay?: number;
|
|
5
|
+
jitter?: number;
|
|
6
|
+
random?: () => number;
|
|
7
|
+
};
|
|
1
8
|
export type RateStrategyObservation = Readonly<{
|
|
2
9
|
currentRate: number;
|
|
3
10
|
minRate: number;
|
|
@@ -26,6 +33,7 @@ export type RateFailureOutcome = Readonly<{
|
|
|
26
33
|
error: unknown;
|
|
27
34
|
}>;
|
|
28
35
|
export type RateOutcomeClassifier = (outcome: RateFailureOutcome) => boolean;
|
|
36
|
+
export type RetryClassifier = (outcome: RateFailureOutcome, attempt: number) => boolean;
|
|
29
37
|
export type AdjustmentTiming = "interval" | "settled";
|
|
30
38
|
export declare const linear: RateStrategy;
|
|
31
39
|
export declare function aimd({ increaseBy, decreaseFactor }?: AimdOptions): RateStrategy;
|
|
@@ -44,24 +52,45 @@ export type ThrottleOptions = {
|
|
|
44
52
|
back_off?: boolean;
|
|
45
53
|
/** Non-negative integer retries for each failed callback. Default 0. */
|
|
46
54
|
retry?: number;
|
|
55
|
+
/** Per-retry delay policy. Omit to preserve immediate retries. */
|
|
56
|
+
retryBackoff?: RetryBackoff;
|
|
47
57
|
/** Maximum number of callbacks awaiting asynchronous settlement. Omit for no limit. */
|
|
48
58
|
concurrency?: number;
|
|
59
|
+
/** Maximum accepted callbacks that have not reached a terminal outcome. Omit for no limit. */
|
|
60
|
+
maxQueueSize?: number;
|
|
49
61
|
/** Non-negative integer dead slots before queue compaction triggers. Default 512. */
|
|
50
62
|
compact_threshold?: number;
|
|
51
63
|
/** Policy used to request the next rate and any backoff after each observation window. */
|
|
52
64
|
rateStrategy?: RateStrategy;
|
|
53
65
|
/** Decides whether a failed callback outcome contributes to adaptive-rate error counting. */
|
|
54
66
|
rateOutcomeClassifier?: RateOutcomeClassifier;
|
|
67
|
+
/** Decides whether a failed callback outcome is eligible for another attempt. */
|
|
68
|
+
retryClassifier?: RetryClassifier;
|
|
55
69
|
/** When adaptive-rate observations are adjusted. Defaults to interval compatibility behavior. */
|
|
56
70
|
adjustmentTiming?: AdjustmentTiming;
|
|
57
71
|
onRateChange?: (rate: number) => void;
|
|
58
72
|
};
|
|
59
73
|
export type ThrottleFn = (callback: ThrottleCallback) => void;
|
|
74
|
+
export type QueueLifecycleState = "running" | "paused" | "stopped" | "aborted";
|
|
75
|
+
export type QueueState = Readonly<{
|
|
76
|
+
rate: number;
|
|
77
|
+
pending: number;
|
|
78
|
+
active: number;
|
|
79
|
+
state: QueueLifecycleState;
|
|
80
|
+
started: number;
|
|
81
|
+
succeeded: number;
|
|
82
|
+
failed: number;
|
|
83
|
+
retried: number;
|
|
84
|
+
rateIncreases: number;
|
|
85
|
+
rateDecreases: number;
|
|
86
|
+
}>;
|
|
60
87
|
export type ThrottleHandle = ThrottleFn & {
|
|
61
88
|
pause: () => void;
|
|
62
89
|
resume: () => void;
|
|
63
90
|
stop: () => void;
|
|
64
91
|
abort: () => void;
|
|
92
|
+
waitForIdle: () => Promise<void>;
|
|
93
|
+
getState: () => QueueState;
|
|
65
94
|
readonly pending: number;
|
|
66
95
|
};
|
|
67
96
|
export declare function createThrottledQueue(options: ThrottleOptions): ThrottleHandle;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { createRateController } from "./rate-controller.js";
|
|
2
1
|
import { createScheduler } from "./scheduler.js";
|
|
3
2
|
const adjustmentTimings = new Set(["interval", "settled"]);
|
|
4
3
|
export const linear = ({ minRate, maxRate, currentRate, errorCount, errorThreshold, hasPendingWork, wasBackedOff, }) => {
|
|
@@ -27,8 +26,21 @@ export function aimd({ increaseBy = 1, decreaseFactor = 0.5 } = {}) {
|
|
|
27
26
|
return { nextRate: currentRate, shouldBackOff: false };
|
|
28
27
|
};
|
|
29
28
|
}
|
|
29
|
+
function validateRetryBackoff(retryBackoff) {
|
|
30
|
+
if (retryBackoff === undefined)
|
|
31
|
+
return;
|
|
32
|
+
if (!Number.isFinite(retryBackoff.baseDelay) || retryBackoff.baseDelay < 0) {
|
|
33
|
+
throw new Error("retryBackoff.baseDelay must be a finite non-negative number");
|
|
34
|
+
}
|
|
35
|
+
if (retryBackoff.maxDelay !== undefined && (!Number.isFinite(retryBackoff.maxDelay) || retryBackoff.maxDelay < 0)) {
|
|
36
|
+
throw new Error("retryBackoff.maxDelay must be a finite non-negative number");
|
|
37
|
+
}
|
|
38
|
+
if (retryBackoff.jitter !== undefined && (!Number.isFinite(retryBackoff.jitter) || retryBackoff.jitter < 0 || retryBackoff.jitter > 1)) {
|
|
39
|
+
throw new Error("retryBackoff.jitter must be a finite number from 0 through 1");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
30
42
|
export function createThrottledQueue(options) {
|
|
31
|
-
const { min_rpi, interval, max_rpi = min_rpi, concurrency, retry = 0, compact_threshold = 512 } = options;
|
|
43
|
+
const { min_rpi, interval, max_rpi = min_rpi, concurrency, maxQueueSize, retry = 0, compact_threshold = 512 } = options;
|
|
32
44
|
const errors_per_interval = options.errors_per_interval ?? 5;
|
|
33
45
|
if (!Number.isInteger(min_rpi) || min_rpi < 1) {
|
|
34
46
|
throw new Error("min_rpi must be a positive integer");
|
|
@@ -42,6 +54,9 @@ export function createThrottledQueue(options) {
|
|
|
42
54
|
if (concurrency !== undefined && (!Number.isInteger(concurrency) || concurrency < 1)) {
|
|
43
55
|
throw new Error("concurrency must be a positive integer");
|
|
44
56
|
}
|
|
57
|
+
if (maxQueueSize !== undefined && (!Number.isSafeInteger(maxQueueSize) || maxQueueSize < 0)) {
|
|
58
|
+
throw new Error("maxQueueSize must be a non-negative safe integer");
|
|
59
|
+
}
|
|
45
60
|
if (!Number.isInteger(errors_per_interval) || errors_per_interval < 1) {
|
|
46
61
|
throw new Error("errors_per_interval must be a positive integer");
|
|
47
62
|
}
|
|
@@ -54,10 +69,17 @@ export function createThrottledQueue(options) {
|
|
|
54
69
|
if (options.adjustmentTiming !== undefined && !adjustmentTimings.has(options.adjustmentTiming)) {
|
|
55
70
|
throw new Error("adjustmentTiming must be either interval or settled");
|
|
56
71
|
}
|
|
57
|
-
|
|
72
|
+
validateRetryBackoff(options.retryBackoff);
|
|
73
|
+
return createScheduler(options, {
|
|
58
74
|
min_rpi,
|
|
59
75
|
max_rpi,
|
|
76
|
+
interval,
|
|
60
77
|
errors_per_interval,
|
|
61
|
-
|
|
78
|
+
back_off: options.back_off ?? false,
|
|
79
|
+
adjustmentTiming: options.adjustmentTiming ?? "interval",
|
|
80
|
+
rateStrategy: options.rateStrategy ?? linear,
|
|
81
|
+
rateOutcomeClassifier: options.rateOutcomeClassifier,
|
|
82
|
+
onRateChange: options.onRateChange,
|
|
83
|
+
});
|
|
62
84
|
}
|
|
63
85
|
//# sourceMappingURL=dynamic-throttled-queue.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dynamic-throttled-queue.js","sourceRoot":"","sources":["../src/dynamic-throttled-queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"dynamic-throttled-queue.js","sourceRoot":"","sources":["../src/dynamic-throttled-queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,uBAAuB;AA2CjD,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAS,CAAE,UAAU,EAAE,SAAS,CAAE,CAAC,CAAC;AAErE,MAAM,CAAC,MAAM,MAA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D,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,iBAAiB,CAAC,IAAI,iBAAiB,GAAG,CAAC,EAAE,CAAC;QAClE,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;IACtE,CAAC;IACD,IAAI,OAAO,CAAC,gBAAgB,KAAK,SAAS,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAAE,CAAC;QAC/F,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;IACzE,CAAC;IACD,oBAAoB,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IAC3C,OAAO,eAAe,CAAC,OAAO,EAAE;QAC9B,OAAO;QACP,OAAO;QACP,QAAQ;QACR,mBAAmB;QACnB,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,KAAK;QACnC,gBAAgB,EAAE,OAAO,CAAC,gBAAgB,IAAI,UAAU;QACxD,YAAY,EAAE,OAAO,CAAC,YAAY,IAAI,MAAM;QAC5C,qBAAqB,EAAE,OAAO,CAAC,qBAAqB;QACpD,YAAY,EAAE,OAAO,CAAC,YAAY;KACnC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function readRandom(random) {
|
|
2
|
+
try {
|
|
3
|
+
// eslint-disable-next-line sonarjs/pseudo-random -- Default jitter requires a random source.
|
|
4
|
+
const value = random?.() ?? Math.random();
|
|
5
|
+
return Number.isFinite(value) && value >= 0 && value <= 1 ? value : undefined;
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function calculateRetryDelay(policy, retryIndex) {
|
|
12
|
+
let delay;
|
|
13
|
+
switch (policy.strategy) {
|
|
14
|
+
case "linear":
|
|
15
|
+
delay = policy.baseDelay * retryIndex;
|
|
16
|
+
break;
|
|
17
|
+
case "exponential":
|
|
18
|
+
delay = policy.baseDelay * 2 ** (retryIndex - 1);
|
|
19
|
+
break;
|
|
20
|
+
default: delay = policy.baseDelay;
|
|
21
|
+
}
|
|
22
|
+
const cappedDelay = policy.maxDelay === undefined ? delay : Math.min(delay, policy.maxDelay);
|
|
23
|
+
if (policy.jitter === undefined)
|
|
24
|
+
return cappedDelay;
|
|
25
|
+
const random = readRandom(policy.random);
|
|
26
|
+
if (random === undefined)
|
|
27
|
+
return cappedDelay;
|
|
28
|
+
const jitteredDelay = cappedDelay * (1 + (random * 2 - 1) * policy.jitter);
|
|
29
|
+
return policy.maxDelay === undefined ? jitteredDelay : Math.min(jitteredDelay, policy.maxDelay);
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=retry-backoff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry-backoff.js","sourceRoot":"","sources":["../src/retry-backoff.ts"],"names":[],"mappings":"AAEA,SAAS,UAAU,CAAC,MAAkC;IACpD,IAAI,CAAC;QACH,6FAA6F;QAC7F,MAAM,KAAK,GAAG,MAAM,EAAE,EAAE,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;QAC1C,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;IAChF,CAAC;IACD,MAAM,CAAC;QACL,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,MAAoB,EAAE,UAAkB;IAC1E,IAAI,KAAa,CAAC;IAClB,QAAQ,MAAM,CAAC,QAAQ,EAAE,CAAC;QACxB,KAAK,QAAQ;YAAE,KAAK,GAAG,MAAM,CAAC,SAAS,GAAG,UAAU,CAAC;YAAC,MAAM;QAC5D,KAAK,aAAa;YAAE,KAAK,GAAG,MAAM,CAAC,SAAS,GAAG,CAAC,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;YAAC,MAAM;QAC5E,OAAO,CAAC,CAAC,KAAK,GAAG,MAAM,CAAC,SAAS,CAAC;IACpC,CAAC;IACD,MAAM,WAAW,GAAG,MAAM,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC7F,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS;QAAE,OAAO,WAAW,CAAC;IACpD,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACzC,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,WAAW,CAAC;IAC7C,MAAM,aAAa,GAAG,WAAW,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;IAC3E,OAAO,MAAM,CAAC,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,aAAa,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC;AAClG,CAAC"}
|
package/dist/scheduler.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
+
import type { AdaptiveRateOptions } from "./adaptive-rate.js";
|
|
1
2
|
import type { ThrottleHandle, ThrottleOptions } from "./dynamic-throttled-queue.js";
|
|
2
|
-
|
|
3
|
-
export declare function createScheduler(options: ThrottleOptions, rateController: RateController): ThrottleHandle;
|
|
3
|
+
export declare function createScheduler(options: ThrottleOptions, adaptiveRateOptions: AdaptiveRateOptions): ThrottleHandle;
|
package/dist/scheduler.js
CHANGED
|
@@ -1,138 +1,194 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
let dyn_interval = evenly_spaced ? interval / current_rpi : interval;
|
|
6
|
-
let dyn_requests_per_interval = evenly_spaced ? 1 : current_rpi;
|
|
7
|
-
let skippedLast = false;
|
|
8
|
-
let isRunning = false;
|
|
1
|
+
import { createAdaptiveRate } from "./adaptive-rate.js";
|
|
2
|
+
import { calculateRetryDelay } from "./retry-backoff.js";
|
|
3
|
+
export function createScheduler(options, adaptiveRateOptions) {
|
|
4
|
+
const { interval, evenly_spaced = true, retry = 0, retryBackoff, concurrency, maxQueueSize, compact_threshold = 512, retryClassifier, } = options;
|
|
9
5
|
let last_called = 0;
|
|
10
6
|
let timeout;
|
|
11
|
-
let dynTimeout;
|
|
12
7
|
let active_count = 0;
|
|
13
8
|
let isPaused = false;
|
|
14
9
|
let isStopped = false;
|
|
15
10
|
let isAborted = false;
|
|
16
|
-
let
|
|
17
|
-
let
|
|
11
|
+
let cnt_started = 0;
|
|
12
|
+
let cnt_succeeded = 0;
|
|
13
|
+
let cnt_failed = 0;
|
|
14
|
+
let cnt_retried = 0;
|
|
18
15
|
const abortController = new AbortController();
|
|
19
16
|
const max_concurrency = concurrency ?? Infinity;
|
|
17
|
+
const max_queue_size = maxQueueSize ?? Infinity;
|
|
20
18
|
const queue = [];
|
|
19
|
+
const delayedRetries = [];
|
|
21
20
|
let head = 0;
|
|
22
|
-
let
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
21
|
+
let reserved_count = 0;
|
|
22
|
+
const idleWaiters = [];
|
|
23
|
+
const adaptiveRate = createAdaptiveRate(adaptiveRateOptions, {
|
|
24
|
+
hasPendingWork: () => queue.length > head,
|
|
25
|
+
resumeStarts() {
|
|
26
|
+
last_called = Date.now();
|
|
27
|
+
clearTimeout(timeout);
|
|
28
|
+
timeout = setTimeout(dequeue, spacing());
|
|
29
|
+
},
|
|
30
|
+
holdStarts(deferBy) {
|
|
31
|
+
clearTimeout(timeout);
|
|
32
|
+
timeout = deferBy === undefined ? undefined : setTimeout(dequeue, spacing() + deferBy);
|
|
33
|
+
},
|
|
34
|
+
idle() {
|
|
35
|
+
clearTimeout(timeout);
|
|
36
|
+
timeout = undefined;
|
|
37
|
+
if (head >= queue.length) {
|
|
38
|
+
queue.length = 0;
|
|
39
|
+
head = 0;
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
function spacing() {
|
|
44
|
+
return evenly_spaced ? interval / adaptiveRate.rate : interval;
|
|
45
|
+
}
|
|
46
|
+
function isIdle() {
|
|
47
|
+
return active_count === 0 && queue.length <= head && delayedRetries.length === 0;
|
|
48
|
+
}
|
|
49
|
+
function notifyIdle() {
|
|
50
|
+
if (!isIdle())
|
|
51
|
+
return;
|
|
52
|
+
const waiters = idleWaiters.splice(0);
|
|
53
|
+
for (const resolve of waiters)
|
|
54
|
+
resolve();
|
|
55
|
+
}
|
|
56
|
+
function releaseDelayedRetry(delayedRetry) {
|
|
57
|
+
const index = delayedRetries.indexOf(delayedRetry);
|
|
58
|
+
if (index < 0)
|
|
59
|
+
return;
|
|
60
|
+
delayedRetries.splice(index, 1);
|
|
61
|
+
queue.push(delayedRetry.item);
|
|
62
|
+
start();
|
|
63
|
+
}
|
|
64
|
+
function startDelayedRetry(delayedRetry) {
|
|
65
|
+
delayedRetry.due = Date.now() + delayedRetry.remaining;
|
|
66
|
+
delayedRetry.timeout = setTimeout(() => {
|
|
67
|
+
releaseDelayedRetry(delayedRetry);
|
|
68
|
+
}, delayedRetry.remaining);
|
|
69
|
+
}
|
|
70
|
+
function freezeDelayedRetries() {
|
|
71
|
+
for (const delayedRetry of delayedRetries) {
|
|
72
|
+
if (delayedRetry.timeout === undefined)
|
|
73
|
+
continue;
|
|
74
|
+
if (delayedRetry.due === undefined)
|
|
75
|
+
continue;
|
|
76
|
+
clearTimeout(delayedRetry.timeout);
|
|
77
|
+
delayedRetry.timeout = undefined;
|
|
78
|
+
delayedRetry.remaining = Math.max(0, delayedRetry.due - Date.now());
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function resumeDelayedRetries() {
|
|
82
|
+
for (const delayedRetry of delayedRetries) {
|
|
83
|
+
if (delayedRetry.timeout === undefined)
|
|
84
|
+
startDelayedRetry(delayedRetry);
|
|
36
85
|
}
|
|
37
86
|
}
|
|
38
|
-
function
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
87
|
+
function scheduleDelayedRetry(item, delay) {
|
|
88
|
+
const delayedRetry = { item, remaining: delay };
|
|
89
|
+
delayedRetries.push(delayedRetry);
|
|
90
|
+
if (!isPaused && !isStopped)
|
|
91
|
+
startDelayedRetry(delayedRetry);
|
|
42
92
|
}
|
|
43
93
|
function stop() {
|
|
44
94
|
isStopped = true;
|
|
45
95
|
isPaused = false;
|
|
46
|
-
|
|
47
|
-
|
|
96
|
+
freezeDelayedRetries();
|
|
97
|
+
adaptiveRate.stop();
|
|
48
98
|
}
|
|
49
99
|
function pause() {
|
|
50
100
|
if (isAborted || isStopped || isPaused)
|
|
51
101
|
return;
|
|
52
102
|
isPaused = true;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
discardSettledWindow();
|
|
103
|
+
freezeDelayedRetries();
|
|
104
|
+
adaptiveRate.pause();
|
|
56
105
|
}
|
|
57
106
|
function resume() {
|
|
58
107
|
if (!isPaused)
|
|
59
108
|
return;
|
|
60
109
|
isPaused = false;
|
|
61
|
-
|
|
62
|
-
|
|
110
|
+
resumeDelayedRetries();
|
|
111
|
+
start();
|
|
63
112
|
}
|
|
64
113
|
function abort() {
|
|
65
114
|
if (isAborted)
|
|
66
115
|
return;
|
|
67
116
|
isAborted = true;
|
|
68
|
-
|
|
69
|
-
|
|
117
|
+
adaptiveRate.stop();
|
|
118
|
+
freezeDelayedRetries();
|
|
119
|
+
delayedRetries.length = 0;
|
|
70
120
|
queue.length = 0;
|
|
71
121
|
head = 0;
|
|
122
|
+
reserved_count = 0;
|
|
72
123
|
abortController.abort();
|
|
73
124
|
}
|
|
74
|
-
function
|
|
75
|
-
if (!outcome)
|
|
125
|
+
function isRetryable(item, outcome) {
|
|
126
|
+
if (!outcome || item.retries === 0)
|
|
76
127
|
return false;
|
|
77
128
|
try {
|
|
78
|
-
return
|
|
129
|
+
return retryClassifier ? retryClassifier(outcome, retry - item.retries + 1) === true : true;
|
|
79
130
|
}
|
|
80
131
|
catch {
|
|
81
132
|
return true;
|
|
82
133
|
}
|
|
83
134
|
}
|
|
84
|
-
function handleResult(item, outcome) {
|
|
135
|
+
function handleResult(item, outcome, reportSettlement) {
|
|
85
136
|
if (isAborted)
|
|
86
137
|
return;
|
|
87
|
-
if (
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
138
|
+
if (outcome)
|
|
139
|
+
cnt_failed++;
|
|
140
|
+
else
|
|
141
|
+
cnt_succeeded++;
|
|
142
|
+
if (isRetryable(item, outcome)) {
|
|
143
|
+
cnt_retried++;
|
|
144
|
+
const retryItem = { fn: item.fn, retries: item.retries - 1 };
|
|
145
|
+
if (retryBackoff === undefined) {
|
|
146
|
+
queue.push(retryItem);
|
|
93
147
|
start();
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
const retryIndex = retry - item.retries + 1;
|
|
151
|
+
scheduleDelayedRetry(retryItem, calculateRetryDelay(retryBackoff, retryIndex));
|
|
152
|
+
}
|
|
94
153
|
}
|
|
154
|
+
else
|
|
155
|
+
reserved_count--;
|
|
156
|
+
reportSettlement(outcome);
|
|
95
157
|
}
|
|
96
|
-
function handleSettlement(item, outcome, resume = false) {
|
|
158
|
+
function handleSettlement(item, outcome, reportSettlement, resume = false) {
|
|
97
159
|
active_count--;
|
|
98
|
-
handleResult(item, outcome);
|
|
99
|
-
if (
|
|
100
|
-
settledOutstanding--;
|
|
101
|
-
if (!collectingSettledWindow && settledOutstanding === 0)
|
|
102
|
-
finishSettledWindow();
|
|
103
|
-
}
|
|
104
|
-
if (resume && isRunning && !skippedLast && queue.length > head && (!usesSettledTiming || collectingSettledWindow))
|
|
160
|
+
handleResult(item, outcome, reportSettlement);
|
|
161
|
+
if (resume && adaptiveRate.pacing === "open" && queue.length > head)
|
|
105
162
|
dequeue();
|
|
163
|
+
notifyIdle();
|
|
106
164
|
}
|
|
107
165
|
function execute(item) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
settledOutstanding++;
|
|
111
|
-
}
|
|
166
|
+
const reportSettlement = adaptiveRate.started();
|
|
167
|
+
cnt_started++;
|
|
112
168
|
active_count++;
|
|
113
169
|
let result;
|
|
114
170
|
try {
|
|
115
171
|
result = item.fn({ signal: abortController.signal });
|
|
116
172
|
}
|
|
117
173
|
catch (error) {
|
|
118
|
-
handleSettlement(item, { kind: "thrown", error });
|
|
174
|
+
handleSettlement(item, { kind: "thrown", error }, reportSettlement);
|
|
119
175
|
return;
|
|
120
176
|
}
|
|
121
177
|
if (result instanceof Promise) {
|
|
122
|
-
void result.then(value => handleSettlement(item, value === false ? { kind: "returned-false" } : undefined, true), (error) => handleSettlement(item, { kind: "rejected", error }, true));
|
|
178
|
+
void result.then(value => handleSettlement(item, value === false ? { kind: "returned-false" } : undefined, reportSettlement, true), (error) => handleSettlement(item, { kind: "rejected", error }, reportSettlement, true));
|
|
123
179
|
return;
|
|
124
180
|
}
|
|
125
|
-
handleSettlement(item, result === false ? { kind: "returned-false" } : undefined);
|
|
181
|
+
handleSettlement(item, result === false ? { kind: "returned-false" } : undefined, reportSettlement);
|
|
126
182
|
}
|
|
127
183
|
function dequeue() {
|
|
128
|
-
const threshold = last_called +
|
|
184
|
+
const threshold = last_called + spacing();
|
|
129
185
|
const now = Date.now();
|
|
130
186
|
if (now < threshold) {
|
|
131
187
|
clearTimeout(timeout);
|
|
132
188
|
timeout = setTimeout(dequeue, threshold - now);
|
|
133
189
|
return;
|
|
134
190
|
}
|
|
135
|
-
const end = Math.min(head +
|
|
191
|
+
const end = Math.min(head + (evenly_spaced ? 1 : adaptiveRate.rate), queue.length);
|
|
136
192
|
let started = 0;
|
|
137
193
|
while (head < end && active_count < max_concurrency) {
|
|
138
194
|
const item = queue[head++];
|
|
@@ -144,130 +200,58 @@ export function createScheduler(options, rateController) {
|
|
|
144
200
|
queue.splice(0, head);
|
|
145
201
|
head = 0;
|
|
146
202
|
}
|
|
147
|
-
if (head >= queue.length
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
if (active_count >= max_concurrency)
|
|
152
|
-
return;
|
|
153
|
-
timeout = setTimeout(dequeue, dyn_interval);
|
|
154
|
-
}
|
|
155
|
-
function applyRate(newRpi) {
|
|
156
|
-
if (newRpi === current_rpi)
|
|
157
|
-
return;
|
|
158
|
-
current_rpi = newRpi;
|
|
159
|
-
onRateChange?.(current_rpi);
|
|
160
|
-
if (evenly_spaced)
|
|
161
|
-
dyn_interval = interval / current_rpi;
|
|
162
|
-
else
|
|
163
|
-
dyn_requests_per_interval = current_rpi;
|
|
164
|
-
}
|
|
165
|
-
function adjustRate() {
|
|
166
|
-
dynTimeout = undefined;
|
|
167
|
-
const wasSkipped = skippedLast;
|
|
168
|
-
skippedLast = false;
|
|
169
|
-
let decision;
|
|
170
|
-
try {
|
|
171
|
-
decision = rateController.observe({ hasPendingWork: queue.length > head, wasBackedOff: wasSkipped });
|
|
172
|
-
}
|
|
173
|
-
catch (error) {
|
|
174
|
-
hasStrategyFailure = true;
|
|
175
|
-
strategyFailure = error;
|
|
176
|
-
halt();
|
|
177
|
-
throw error;
|
|
178
|
-
}
|
|
179
|
-
applyRate(decision.rate);
|
|
180
|
-
if (decision.shouldBackOff && back_off) {
|
|
181
|
-
clearTimeout(timeout);
|
|
182
|
-
skippedLast = true;
|
|
183
|
-
timeout = setTimeout(dequeue, dyn_interval + interval);
|
|
184
|
-
}
|
|
185
|
-
if (isRunning)
|
|
186
|
-
dynTimeout = setTimeout(adjustRate, interval);
|
|
187
|
-
}
|
|
188
|
-
function finishSettledWindow() {
|
|
189
|
-
if (observationWindow === undefined || isPaused || isStopped || isAborted)
|
|
190
|
-
return;
|
|
191
|
-
const wasSkipped = skippedLast;
|
|
192
|
-
skippedLast = false;
|
|
193
|
-
let decision;
|
|
194
|
-
try {
|
|
195
|
-
decision = rateController.observe({ hasPendingWork: queue.length > head, wasBackedOff: wasSkipped });
|
|
196
|
-
}
|
|
197
|
-
catch (error) {
|
|
198
|
-
hasStrategyFailure = true;
|
|
199
|
-
strategyFailure = error;
|
|
200
|
-
halt();
|
|
201
|
-
discardSettledWindow();
|
|
202
|
-
throw error;
|
|
203
|
-
}
|
|
204
|
-
observationWindow = undefined;
|
|
205
|
-
applyRate(decision.rate);
|
|
206
|
-
if (decision.shouldBackOff && back_off) {
|
|
207
|
-
skippedLast = true;
|
|
208
|
-
timeout = setTimeout(resumeSettledScheduling, interval);
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
if (queue.length > head)
|
|
212
|
-
beginSettledWindow();
|
|
213
|
-
else
|
|
214
|
-
halt();
|
|
215
|
-
}
|
|
216
|
-
function closeSettledWindow() {
|
|
217
|
-
dynTimeout = undefined;
|
|
218
|
-
collectingSettledWindow = false;
|
|
219
|
-
clearTimeout(timeout);
|
|
220
|
-
timeout = undefined;
|
|
221
|
-
if (settledOutstanding === 0)
|
|
222
|
-
finishSettledWindow();
|
|
223
|
-
}
|
|
224
|
-
function resumeSettledScheduling() {
|
|
225
|
-
skippedLast = false;
|
|
226
|
-
beginSettledWindow();
|
|
227
|
-
}
|
|
228
|
-
function beginSettledWindow() {
|
|
229
|
-
if (isAborted || isPaused || isStopped || skippedLast || queue.length <= head)
|
|
203
|
+
if (head >= queue.length)
|
|
204
|
+
adaptiveRate.drained();
|
|
205
|
+
if (adaptiveRate.pacing !== "open" || active_count >= max_concurrency)
|
|
230
206
|
return;
|
|
231
|
-
|
|
232
|
-
collectingSettledWindow = true;
|
|
233
|
-
observationWindow = nextObservationWindow++;
|
|
234
|
-
last_called = Date.now();
|
|
235
|
-
clearTimeout(timeout);
|
|
236
|
-
timeout = setTimeout(dequeue, dyn_interval);
|
|
237
|
-
clearTimeout(dynTimeout);
|
|
238
|
-
dynTimeout = setTimeout(closeSettledWindow, interval);
|
|
207
|
+
timeout = setTimeout(dequeue, spacing());
|
|
239
208
|
}
|
|
240
209
|
function start() {
|
|
241
210
|
if (isAborted || isPaused || isStopped)
|
|
242
211
|
return;
|
|
243
|
-
|
|
244
|
-
return;
|
|
245
|
-
if (usesSettledTiming) {
|
|
246
|
-
beginSettledWindow();
|
|
247
|
-
return;
|
|
248
|
-
}
|
|
249
|
-
isRunning = true;
|
|
250
|
-
last_called = Date.now();
|
|
251
|
-
clearTimeout(timeout);
|
|
252
|
-
timeout = setTimeout(dequeue, dyn_interval);
|
|
253
|
-
if (!dynTimeout)
|
|
254
|
-
dynTimeout = setTimeout(adjustRate, interval);
|
|
212
|
+
adaptiveRate.start();
|
|
255
213
|
}
|
|
256
214
|
function enqueue(callback) {
|
|
257
215
|
if (isAborted)
|
|
258
216
|
throw new Error("Cannot enqueue work after the queue has been aborted");
|
|
259
|
-
|
|
260
|
-
|
|
217
|
+
adaptiveRate.throwIfFailed();
|
|
218
|
+
if (reserved_count >= max_queue_size)
|
|
219
|
+
throw new Error("Cannot enqueue work: maxQueueSize has been reached");
|
|
220
|
+
reserved_count++;
|
|
261
221
|
queue.push({ fn: callback, retries: retry });
|
|
222
|
+
const wasStopped = isStopped;
|
|
262
223
|
isStopped = false;
|
|
263
|
-
if (
|
|
264
|
-
|
|
224
|
+
if (wasStopped)
|
|
225
|
+
resumeDelayedRetries();
|
|
226
|
+
start();
|
|
227
|
+
}
|
|
228
|
+
function getLifecycleState() {
|
|
229
|
+
if (isAborted)
|
|
230
|
+
return "aborted";
|
|
231
|
+
if (isStopped)
|
|
232
|
+
return "stopped";
|
|
233
|
+
if (isPaused)
|
|
234
|
+
return "paused";
|
|
235
|
+
return "running";
|
|
265
236
|
}
|
|
266
237
|
enqueue.pause = pause;
|
|
267
238
|
enqueue.resume = resume;
|
|
268
239
|
enqueue.stop = stop;
|
|
269
240
|
enqueue.abort = abort;
|
|
270
|
-
|
|
241
|
+
enqueue.waitForIdle = async () => isIdle() ? Promise.resolve() : new Promise(resolve => { idleWaiters.push(resolve); });
|
|
242
|
+
enqueue.getState = () => Object.freeze({
|
|
243
|
+
rate: adaptiveRate.rate,
|
|
244
|
+
pending: queue.length - head + delayedRetries.length,
|
|
245
|
+
active: active_count,
|
|
246
|
+
state: getLifecycleState(),
|
|
247
|
+
started: cnt_started,
|
|
248
|
+
succeeded: cnt_succeeded,
|
|
249
|
+
failed: cnt_failed,
|
|
250
|
+
retried: cnt_retried,
|
|
251
|
+
rateIncreases: adaptiveRate.rateIncreases,
|
|
252
|
+
rateDecreases: adaptiveRate.rateDecreases,
|
|
253
|
+
});
|
|
254
|
+
Object.defineProperty(enqueue, "pending", { get: () => queue.length - head + delayedRetries.length });
|
|
271
255
|
return enqueue;
|
|
272
256
|
}
|
|
273
257
|
//# sourceMappingURL=scheduler.js.map
|
package/dist/scheduler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scheduler.js","sourceRoot":"","sources":["../src/scheduler.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"scheduler.js","sourceRoot":"","sources":["../src/scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,2BAA2B;AAGxD,OAAO,EAAE,mBAAmB,EAAE,2BAA2B;AAUzD,MAAM,UAAU,eAAe,CAAC,OAAwB,EAAE,mBAAwC;IAChG,MAAM,EACJ,QAAQ,EACR,aAAa,GAAG,IAAI,EACpB,KAAK,GAAG,CAAC,EACT,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,iBAAiB,GAAG,GAAG,EACvB,eAAe,GAChB,GAAG,OAAO,CAAC;IACZ,IAAI,WAAW,GAAG,CAAC,CAAC;IACpB,IAAI,OAAkD,CAAC;IACvD,IAAI,YAAY,GAAG,CAAC,CAAC;IACrB,IAAI,QAAQ,GA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K,GAAG,KAAK,CAAC;IACtB,OAAO,CAAC,WAAW,GAAG,KAAK,IAAI,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI,OAAO,CAAO,OAAO,CAAC,EAAE,GAAG,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9H,OAAO,CAAC,QAAQ,GAAG,GAAe,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC;QACjD,IAAI,EAAE,YAAY,CAAC,IAAI;QACvB,OAAO,EAAE,KAAK,CAAC,MAAM,GAAG,IAAI,GAAG,cAAc,CAAC,MAAM;QACpD,MAAM,EAAE,YAAY;QACpB,KAAK,EAAE,iBAAiB,EAAE;QAC1B,OAAO,EAAE,WAAW;QACpB,SAAS,EAAE,aAAa;QACxB,MAAM,EAAE,UAAU;QAClB,OAAO,EAAE,WAAW;QACpB,aAAa,EAAE,YAAY,CAAC,aAAa;QACzC,aAAa,EAAE,YAAY,CAAC,aAAa;KAC1C,CAAC,CAAC;IACH,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,GAAG,cAAc,CAAC,MAAM,EAAE,CAAC,CAAC;IACtG,OAAO,OAAyB,CAAC;AACnC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dynamic-throttled-queue",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.1-rc.b3d1777",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Dynamically throttles arbitrary code to execute between a minimum and maximum number of times per interval. Best for making throttled API requests.",
|
|
6
6
|
"files": [
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { RateStrategy } from "./dynamic-throttled-queue.js";
|
|
2
|
-
type RateControllerOptions = {
|
|
3
|
-
min_rpi: number;
|
|
4
|
-
max_rpi: number;
|
|
5
|
-
errors_per_interval: number;
|
|
6
|
-
};
|
|
7
|
-
type Observation = {
|
|
8
|
-
hasPendingWork: boolean;
|
|
9
|
-
wasBackedOff: boolean;
|
|
10
|
-
};
|
|
11
|
-
export type RateDecision = {
|
|
12
|
-
rate: number;
|
|
13
|
-
shouldBackOff: boolean;
|
|
14
|
-
};
|
|
15
|
-
export type RateController = {
|
|
16
|
-
readonly rate: number;
|
|
17
|
-
clearObservation: () => void;
|
|
18
|
-
recordCompletion: (isRateReducing: boolean) => void;
|
|
19
|
-
observe: (observation: Observation) => RateDecision;
|
|
20
|
-
};
|
|
21
|
-
export declare function createRateController(options: RateControllerOptions, strategy: RateStrategy): RateController;
|
|
22
|
-
export {};
|
package/dist/rate-controller.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
function validateDecision(decision) {
|
|
2
|
-
if (typeof decision !== "object" || decision === null) {
|
|
3
|
-
throw new TypeError("rate strategy must return a decision object");
|
|
4
|
-
}
|
|
5
|
-
const candidate = decision;
|
|
6
|
-
if (!Number.isFinite(candidate.nextRate) || !Number.isInteger(candidate.nextRate)) {
|
|
7
|
-
throw new TypeError("rate strategy must return a finite integer nextRate");
|
|
8
|
-
}
|
|
9
|
-
if (typeof candidate.shouldBackOff !== "boolean") {
|
|
10
|
-
throw new TypeError("rate strategy must return a boolean shouldBackOff");
|
|
11
|
-
}
|
|
12
|
-
return candidate;
|
|
13
|
-
}
|
|
14
|
-
export function createRateController(options, strategy) {
|
|
15
|
-
let rate = Math.ceil((options.max_rpi + options.min_rpi) / 2);
|
|
16
|
-
let errorCount = 0;
|
|
17
|
-
return {
|
|
18
|
-
get rate() {
|
|
19
|
-
return rate;
|
|
20
|
-
},
|
|
21
|
-
clearObservation() {
|
|
22
|
-
errorCount = 0;
|
|
23
|
-
},
|
|
24
|
-
recordCompletion(isRateReducing) {
|
|
25
|
-
if (isRateReducing)
|
|
26
|
-
errorCount++;
|
|
27
|
-
},
|
|
28
|
-
observe(observation) {
|
|
29
|
-
const decision = validateDecision(strategy(Object.freeze({
|
|
30
|
-
currentRate: rate,
|
|
31
|
-
minRate: options.min_rpi,
|
|
32
|
-
maxRate: options.max_rpi,
|
|
33
|
-
errorCount,
|
|
34
|
-
errorThreshold: options.errors_per_interval,
|
|
35
|
-
...observation,
|
|
36
|
-
})));
|
|
37
|
-
rate = Math.min(options.max_rpi, Math.max(options.min_rpi, decision.nextRate));
|
|
38
|
-
errorCount = 0;
|
|
39
|
-
return { rate, shouldBackOff: decision.shouldBackOff };
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
//# sourceMappingURL=rate-controller.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rate-controller.js","sourceRoot":"","sources":["../src/rate-controller.ts"],"names":[],"mappings":"AAyBA,SAAS,gBAAgB,CAAC,QAAiB;IACzC,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACtD,MAAM,IAAI,SAAS,CAAC,6CAA6C,CAAC,CAAC;IACrE,CAAC;IACD,MAAM,SAAS,GAAG,QAAgC,CAAC;IACnD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC;QAClF,MAAM,IAAI,SAAS,CAAC,qDAAqD,CAAC,CAAC;IAC7E,CAAC;IACD,IAAI,OAAO,SAAS,CAAC,aAAa,KAAK,SAAS,EAAE,CAAC;QACjD,MAAM,IAAI,SAAS,CAAC,mDAAmD,CAAC,CAAC;IAC3E,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,OAA8B,EAAE,QAAsB;IACzF,IAAI,IAAI,GAAG,IAAI,CAAC,IAAI,CA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