adaptive-concurrency 0.10.0 → 0.10.1

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.
@@ -17,6 +17,16 @@ export interface AIMDLimitOptions {
17
17
  * Default: 5000
18
18
  */
19
19
  timeout?: number;
20
+ /**
21
+ * Absolute amount to use as a +/- jitter band around `backoffRatio` for
22
+ * each multiplicative decrease. Breaks lockstep oscillation when multiple
23
+ * independent clients share the same configuration.
24
+ *
25
+ * For example, with `backoffRatio: 0.9` and `backoffJitter: 0.02`, each
26
+ * decrease multiplies by a uniformly random value in [0.88, 0.92].
27
+ * Must be in [0, 0.05]. Default: 0.02.
28
+ */
29
+ backoffJitter?: number;
20
30
  }
21
31
  export declare class AIMDLimit implements AdaptiveLimit {
22
32
  private _limit;
@@ -25,6 +35,7 @@ export declare class AIMDLimit implements AdaptiveLimit {
25
35
  private readonly timeout;
26
36
  private readonly minLimit;
27
37
  private readonly maxLimit;
38
+ private readonly backoffJitter;
28
39
  constructor(options?: AIMDLimitOptions);
29
40
  addSample(_startTime: number, rtt: number, inflight: number, didDrop: boolean): void;
30
41
  get currentLimit(): number;
@@ -1 +1 @@
1
- {"version":3,"file":"AIMDLimit.d.ts","sourceRoot":"","sources":["../../src/limit/AIMDLimit.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,SAAU,YAAW,aAAa;IAC7C,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,QAAQ,CAAC,cAAc,C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OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;gBAE3B,OAAO,GAAE,gBAAqB;IA0B1C,SAAS,CACP,UAAU,EAAE,MAAM,EAClB,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,OAAO,GACf,IAAI;IAuBP,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,OAAO,CAAC,aAAa;IAOrB,SAAS,CACP,QAAQ,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,EACpC,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,WAAW,CAAA;KAAO,GACrC,MAAM,IAAI;IAIb,QAAQ,IAAI,MAAM;CAGnB"}
@@ -6,6 +6,7 @@ export class AIMDLimit {
6
6
  timeout;
7
7
  minLimit;
8
8
  maxLimit;
9
+ backoffJitter;
9
10
  constructor(options = {}) {
10
11
  const initialLimit = options.initialLimit ?? 20;
11
12
  this._limit = initialLimit;
@@ -13,19 +14,27 @@ export class AIMDLimit {
13
14
  this.timeout = options.timeout ?? 5_000;
14
15
  this.minLimit = options.minLimit ?? 20;
15
16
  this.maxLimit = options.maxLimit ?? 200;
17
+ this.backoffJitter = options.backoffJitter ?? 0.02;
16
18
  if (this.backoffRatio >= 1.0 || this.backoffRatio < 0.5) {
17
19
  throw new Error("Backoff ratio must be in the range [0.5, 1.0)");
18
20
  }
19
21
  if (this.timeout <= 0) {
20
22
  throw new Error("Timeout must be positive");
21
23
  }
24
+ if (this.backoffJitter < 0 || this.backoffJitter > 0.05) {
25
+ throw new Error("backoffJitter must be in the range [0, 0.05]");
26
+ }
27
+ if (this.backoffRatio + this.backoffJitter >= 1.0) {
28
+ throw new Error("backoffRatio + backoffJitter must be < 1.0 to guarantee the limit decreases on drop");
29
+ }
22
30
  }
23
31
  addSample(_startTime, rtt, inflight, didDrop) {
24
32
  let currentLimit = this._limit;
25
33
  if (didDrop || rtt > this.timeout) {
26
- currentLimit = Math.floor(currentLimit * this.backoffRatio);
34
+ const jitteredRatio = Math.max(0.5, Math.min(1 - Number.EPSILON, this.backoffRatio + (Math.random() * 2 - 1) * this.backoffJitter));
35
+ currentLimit = Math.floor(currentLimit * jitteredRatio);
27
36
  }
28
- else if (inflight * 2 >= currentLimit) {
37
+ else if (inflight >= 0.5 * currentLimit) {
29
38
  currentLimit = currentLimit + 1;
30
39
  }
31
40
  const newLimit = Math.min(this.maxLimit, Math.max(this.minLimit, currentLimit));
@@ -0,0 +1,78 @@
1
+ import type { AdaptiveLimit } from "./StreamingLimit.js";
2
+ /**
3
+ * Decorator that normalizes RTT samples by operation group before forwarding
4
+ * them to a delegate {@link AdaptiveLimit}.
5
+ *
6
+ * When a single limiter covers heterogeneous operations (e.g. fast reads and
7
+ * slow writes), raw RTT variance can reflect the **operation mix** rather than
8
+ * downstream congestion. A shift toward slower operations looks like a latency
9
+ * spike and causes the delegate to needlessly reduce the limit.
10
+ *
11
+ * `OperationGroupedLimit` solves this by maintaining a per-group RTT baseline
12
+ * (exponential moving average with a long window). Each sample's RTT is
13
+ * expressed as a ratio relative to its group's own baseline, then scaled by a
14
+ * fixed reference value:
15
+ *
16
+ * normalizedRtt = (rtt / groupBaseline) × referenceRtt
17
+ *
18
+ * `referenceRtt` is a constant configured at construction time (derived from
19
+ * the first group's baseline after warmup if not explicitly provided). Because
20
+ * it never changes, a shift in operation mix cannot cause a spurious RTT spike.
21
+ * Meanwhile, real congestion within any group shows through because it causes
22
+ * `rtt / groupBaseline > 1`.
23
+ *
24
+ * Per-group baselines use a long EMA window so they represent the group's
25
+ * **intrinsic** latency characteristic and don't absorb short-term congestion.
26
+ *
27
+ * The `groupFor` function maps each sample to a group key. Samples whose group
28
+ * cannot be determined (returns `undefined`) are forwarded with their raw RTT.
29
+ */
30
+ export declare class OperationGroupedLimit implements AdaptiveLimit {
31
+ private readonly delegate;
32
+ private readonly groupFor;
33
+ private readonly groups;
34
+ private readonly groupWindow;
35
+ private readonly groupWarmup;
36
+ /**
37
+ * Fixed reference RTT used as the absolute scale for normalized values.
38
+ * Once set, it never changes, ensuring mix shifts don't affect normalization.
39
+ * `undefined` until enough samples have been seen to establish it.
40
+ */
41
+ private referenceRtt;
42
+ private readonly configuredReferenceRtt;
43
+ constructor(delegate: AdaptiveLimit, options: {
44
+ /**
45
+ * Maps a sample's `startTime` to its operation group key.
46
+ *
47
+ * In practice, the caller records the group when acquiring a limiter
48
+ * allotment (at which point `startTime` is captured by the `Limiter`),
49
+ * then provides a lookup here that resolves that `startTime` back to the
50
+ * group. Returning `undefined` forwards the sample unnormalized.
51
+ */
52
+ groupFor: (startTime: number) => string | undefined;
53
+ /**
54
+ * EMA window size for per-group RTT baselines. These should be long
55
+ * enough to represent the group's intrinsic latency so short-term
56
+ * congestion shows through as deviations. Default: 500
57
+ */
58
+ groupWindow?: number;
59
+ /**
60
+ * Number of initial samples averaged arithmetically before switching to
61
+ * exponential decay (per-group). Default: 10
62
+ */
63
+ groupWarmup?: number;
64
+ /**
65
+ * Fixed reference RTT (in milliseconds) used as the scale for all
66
+ * normalized values. If not provided, it is automatically set from the
67
+ * first group's baseline after its warmup phase completes.
68
+ */
69
+ referenceRtt?: number;
70
+ });
71
+ addSample(startTime: number, rtt: number, inflight: number, didDrop: boolean): void;
72
+ get currentLimit(): number;
73
+ subscribe(consumer: (newLimit: number) => void, options?: {
74
+ signal?: AbortSignal;
75
+ }): () => void;
76
+ toString(): string;
77
+ }
78
+ //# sourceMappingURL=OperationGroupedLimit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OperationGroupedLimit.d.ts","sourceRoot":"","sources":["../../src/limit/OperationGroupedLimit.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,qBAAa,qBAAsB,YAAW,aAAa;IACzD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgB;IACzC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA4C;IACrE,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuC;IAC9D,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IAErC;;;;OAIG;IACH,OAAO,CAAC,YAAY,CAAqB;IACzC,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAAqB;gBAG1D,QAAQ,EAAE,aAAa,EACvB,OAAO,EAAE;QACP;;;;;;;WAOG;QACH,QAAQ,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;QAEpD;;;;WAIG;QACH,WAAW,CAAC,EAAE,MAAM,CAAC;QAErB;;;WAGG;QACH,WAAW,CAAC,EAAE,MAAM,CAAC;QAErB;;;;WAIG;QACH,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB;IAUH,SAAS,CACP,SAAS,EAAE,MAAM,EACjB,GAAG,EAAE,MAAM,EACX,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,OAAO,GACf,IAAI;IAqCP,IAAI,YAAY,IAAI,MAAM,CAEzB;IAED,SAAS,CACP,QAAQ,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,EACpC,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,WAAW,CAAA;KAAE,GACjC,MAAM,IAAI;IAIb,QAAQ,IAAI,MAAM;CAOnB"}
@@ -0,0 +1,93 @@
1
+ import { ExpMovingAverage } from "../statistics/ExpMovingAverage.js";
2
+ /**
3
+ * Decorator that normalizes RTT samples by operation group before forwarding
4
+ * them to a delegate {@link AdaptiveLimit}.
5
+ *
6
+ * When a single limiter covers heterogeneous operations (e.g. fast reads and
7
+ * slow writes), raw RTT variance can reflect the **operation mix** rather than
8
+ * downstream congestion. A shift toward slower operations looks like a latency
9
+ * spike and causes the delegate to needlessly reduce the limit.
10
+ *
11
+ * `OperationGroupedLimit` solves this by maintaining a per-group RTT baseline
12
+ * (exponential moving average with a long window). Each sample's RTT is
13
+ * expressed as a ratio relative to its group's own baseline, then scaled by a
14
+ * fixed reference value:
15
+ *
16
+ * normalizedRtt = (rtt / groupBaseline) × referenceRtt
17
+ *
18
+ * `referenceRtt` is a constant configured at construction time (derived from
19
+ * the first group's baseline after warmup if not explicitly provided). Because
20
+ * it never changes, a shift in operation mix cannot cause a spurious RTT spike.
21
+ * Meanwhile, real congestion within any group shows through because it causes
22
+ * `rtt / groupBaseline > 1`.
23
+ *
24
+ * Per-group baselines use a long EMA window so they represent the group's
25
+ * **intrinsic** latency characteristic and don't absorb short-term congestion.
26
+ *
27
+ * The `groupFor` function maps each sample to a group key. Samples whose group
28
+ * cannot be determined (returns `undefined`) are forwarded with their raw RTT.
29
+ */
30
+ export class OperationGroupedLimit {
31
+ delegate;
32
+ groupFor;
33
+ groups = new Map();
34
+ groupWindow;
35
+ groupWarmup;
36
+ /**
37
+ * Fixed reference RTT used as the absolute scale for normalized values.
38
+ * Once set, it never changes, ensuring mix shifts don't affect normalization.
39
+ * `undefined` until enough samples have been seen to establish it.
40
+ */
41
+ referenceRtt;
42
+ configuredReferenceRtt;
43
+ constructor(delegate, options) {
44
+ this.delegate = delegate;
45
+ this.groupFor = options.groupFor;
46
+ this.groupWindow = options.groupWindow ?? 500;
47
+ this.groupWarmup = options.groupWarmup ?? 10;
48
+ this.configuredReferenceRtt = options.referenceRtt;
49
+ this.referenceRtt = options.referenceRtt;
50
+ }
51
+ addSample(startTime, rtt, inflight, didDrop) {
52
+ const group = this.groupFor(startTime);
53
+ if (group === undefined) {
54
+ this.delegate.addSample(startTime, rtt, inflight, didDrop);
55
+ return;
56
+ }
57
+ let groupEma = this.groups.get(group);
58
+ let isNew = false;
59
+ if (!groupEma) {
60
+ groupEma = new ExpMovingAverage(this.groupWindow, this.groupWarmup);
61
+ this.groups.set(group, groupEma);
62
+ isNew = true;
63
+ }
64
+ // Read the group baseline *before* incorporating this sample so that the
65
+ // current observation doesn't immediately anchor its own normalization.
66
+ const prevGroupBaseline = groupEma.currentValue;
67
+ groupEma.addSample(rtt);
68
+ // Auto-detect the reference RTT from the first group's baseline once its
69
+ // warmup is complete.
70
+ if (this.referenceRtt === undefined && !isNew && prevGroupBaseline > 0) {
71
+ this.referenceRtt = prevGroupBaseline;
72
+ }
73
+ let normalizedRtt;
74
+ if (prevGroupBaseline <= 0 || this.referenceRtt === undefined) {
75
+ normalizedRtt = rtt;
76
+ }
77
+ else {
78
+ normalizedRtt = (rtt / prevGroupBaseline) * this.referenceRtt;
79
+ }
80
+ this.delegate.addSample(startTime, normalizedRtt, inflight, didDrop);
81
+ }
82
+ get currentLimit() {
83
+ return this.delegate.currentLimit;
84
+ }
85
+ subscribe(consumer, options) {
86
+ return this.delegate.subscribe(consumer, options);
87
+ }
88
+ toString() {
89
+ return (`OperationGroupedLimit [groups=${this.groups.size}` +
90
+ `, referenceRtt=${this.referenceRtt?.toFixed(3) ?? "pending"}` +
91
+ `, delegate=${this.delegate}]`);
92
+ }
93
+ }
@@ -43,9 +43,10 @@ export interface VegasLimitOptions {
43
43
  */
44
44
  increase?(limit: number): number;
45
45
  /**
46
- * Compute the new limit when decreasing.
46
+ * Compute the new limit when decreasing. If didDrop is true, decrease() is
47
+ * being called because of a drop.
47
48
  */
48
- decrease?(limit: number): number;
49
+ decrease?(limit: number, didDrop: boolean): number;
49
50
  };
50
51
  /**
51
52
  * The limiter will probe for a new noload RTT every
@@ -66,9 +67,9 @@ export declare class VegasLimit implements AdaptiveLimit {
66
67
  private readonly smoothing;
67
68
  private readonly policy;
68
69
  private readonly rttSampleListener;
69
- private readonly probeMultiplier;
70
70
  private probeCount;
71
71
  private probeJitter;
72
+ private readonly probeMultiplier;
72
73
  constructor(options?: VegasLimitOptions);
73
74
  private resetProbeJitter;
74
75
  private shouldProbe;
@@ -1 +1 @@
1
- {"version":3,"file":"VegasLimit.d.ts","sour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AAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,EACpC,OAAO,GAAE;QAAE,MAAM,CAAC,EAAE,WAAW,CAAA;KAAO,GACrC,MAAM,IAAI;IAIb,OAAO,CAAC,gBAAgB;IA+BxB,OAAO,CAAC,oBAAoB;IA2C5B,QAAQ,IAAI,MAAM;CAGnB"}
@@ -1,6 +1,5 @@
1
1
  import { ListenerSet } from "../ListenerSet.js";
2
- import { MetricIds } from "../MetricRegistry.js";
3
- import { NoopMetricRegistry } from "../MetricRegistry.js";
2
+ import { MetricIds, NoopMetricRegistry } from "../MetricRegistry.js";
4
3
  /**
5
4
  * Sublinear scale of the concurrency limit (floor of log10, lower-bounded for
6
5
  * small n). Same idea as Netflix's Log10RootIntFunction. Used only for
@@ -21,9 +20,9 @@ export class VegasLimit {
21
20
  smoothing;
22
21
  policy;
23
22
  rttSampleListener;
24
- probeMultiplier;
25
23
  probeCount = 0;
26
24
  probeJitter;
25
+ probeMultiplier;
27
26
  constructor(options = {}) {
28
27
  const initialLimit = options.initialLimit ?? 20;
29
28
  this._limit = initialLimit;
@@ -47,7 +46,8 @@ export class VegasLimit {
47
46
  return this.probeJitter;
48
47
  }
49
48
  shouldProbe() {
50
- return this.probeJitter * this.probeMultiplier * this.estimatedLimit <= this.probeCount;
49
+ return (this.probeJitter * this.probeMultiplier * this.estimatedLimit <=
50
+ this.probeCount);
51
51
  }
52
52
  addSample(startTime, rtt, inflight, didDrop) {
53
53
  this.applyNewLimit(this.computeNextLimit(startTime, rtt, inflight, didDrop));
@@ -73,11 +73,13 @@ export class VegasLimit {
73
73
  this.resetProbeJitter();
74
74
  this.probeCount = 0;
75
75
  this.rttNoload = rtt;
76
+ this.rttSampleListener.addSample(rtt);
76
77
  return Math.floor(this.estimatedLimit);
77
78
  }
78
79
  const rttNoload = this.rttNoload;
79
80
  if (rttNoload === 0 || rtt < rttNoload) {
80
81
  this.rttNoload = rtt;
82
+ this.rttSampleListener.addSample(rtt);
81
83
  return Math.floor(this.estimatedLimit);
82
84
  }
83
85
  this.rttSampleListener.addSample(rttNoload);
@@ -89,7 +91,7 @@ export class VegasLimit {
89
91
  let newLimit;
90
92
  // Treat any drop (i.e timeout) as needing to reduce the limit
91
93
  if (didDrop) {
92
- newLimit = this.policy.decrease(estimatedLimit);
94
+ newLimit = this.policy.decrease(estimatedLimit, didDrop);
93
95
  // Prevent upward drift if not close to the limit
94
96
  }
95
97
  else if (inflight * 2 < estimatedLimit) {
@@ -109,7 +111,7 @@ export class VegasLimit {
109
111
  // Detecting latency so decrease
110
112
  }
111
113
  else if (queueSize > beta) {
112
- newLimit = this.policy.decrease(estimatedLimit);
114
+ newLimit = this.policy.decrease(estimatedLimit, didDrop);
113
115
  // We're within the sweet spot so nothing to do
114
116
  }
115
117
  else {
@@ -117,7 +119,8 @@ export class VegasLimit {
117
119
  }
118
120
  }
119
121
  newLimit = Math.max(1, Math.min(this.maxLimit, newLimit));
120
- newLimit = (1 - this.smoothing) * estimatedLimit + this.smoothing * newLimit;
122
+ newLimit =
123
+ (1 - this.smoothing) * estimatedLimit + this.smoothing * newLimit;
121
124
  this.estimatedLimit = newLimit;
122
125
  return Math.floor(newLimit);
123
126
  }
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "dynamic rate limiting"
10
10
  ],
11
11
  "author": "Ethan Resnick <ethan.resnick@gmail.com>",
12
- "version": "0.10.0",
12
+ "version": "0.10.1",
13
13
  "type": "module",
14
14
  "exports": {
15
15
  ".": {