@stimulcross/rate-limiter 0.0.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.
- package/.editorconfig +21 -0
- package/.github/workflows/node.yml +87 -0
- package/.husky/commit-msg +1 -0
- package/.husky/pre-commit +1 -0
- package/.megaignore +8 -0
- package/.prettierignore +3 -0
- package/LICENSE +21 -0
- package/README.md +7 -0
- package/commitlint.config.js +8 -0
- package/eslint.config.js +65 -0
- package/lint-staged.config.js +4 -0
- package/package.json +89 -0
- package/prettier.config.cjs +1 -0
- package/src/core/cancellable.ts +4 -0
- package/src/core/clock.ts +9 -0
- package/src/core/decision.ts +27 -0
- package/src/core/rate-limit-policy.ts +15 -0
- package/src/core/rate-limiter-status.ts +14 -0
- package/src/core/rate-limiter.ts +37 -0
- package/src/core/state-storage.ts +51 -0
- package/src/enums/rate-limit-error-code.ts +29 -0
- package/src/errors/custom.error.ts +14 -0
- package/src/errors/invalid-cost.error.ts +33 -0
- package/src/errors/rate-limit.error.ts +91 -0
- package/src/errors/rate-limiter-destroyed.error.ts +8 -0
- package/src/index.ts +11 -0
- package/src/interfaces/rate-limiter-options.ts +84 -0
- package/src/interfaces/rate-limiter-queue-options.ts +45 -0
- package/src/interfaces/rate-limiter-run-options.ts +58 -0
- package/src/limiters/abstract-rate-limiter.ts +206 -0
- package/src/limiters/composite.policy.ts +102 -0
- package/src/limiters/fixed-window/fixed-window.limiter.ts +121 -0
- package/src/limiters/fixed-window/fixed-window.options.ts +29 -0
- package/src/limiters/fixed-window/fixed-window.policy.ts +159 -0
- package/src/limiters/fixed-window/fixed-window.state.ts +10 -0
- package/src/limiters/fixed-window/fixed-window.status.ts +46 -0
- package/src/limiters/fixed-window/index.ts +4 -0
- package/src/limiters/generic-cell/generic-cell.limiter.ts +108 -0
- package/src/limiters/generic-cell/generic-cell.options.ts +23 -0
- package/src/limiters/generic-cell/generic-cell.policy.ts +115 -0
- package/src/limiters/generic-cell/generic-cell.state.ts +8 -0
- package/src/limiters/generic-cell/generic-cell.status.ts +54 -0
- package/src/limiters/generic-cell/index.ts +4 -0
- package/src/limiters/http-response-based/http-limit-info.extractor.ts +20 -0
- package/src/limiters/http-response-based/http-limit.info.ts +41 -0
- package/src/limiters/http-response-based/http-response-based-limiter.options.ts +18 -0
- package/src/limiters/http-response-based/http-response-based-limiter.state.ts +13 -0
- package/src/limiters/http-response-based/http-response-based-limiter.status.ts +74 -0
- package/src/limiters/http-response-based/http-response-based.limiter.ts +512 -0
- package/src/limiters/http-response-based/index.ts +6 -0
- package/src/limiters/leaky-bucket/index.ts +4 -0
- package/src/limiters/leaky-bucket/leaky-bucket.limiter.ts +105 -0
- package/src/limiters/leaky-bucket/leaky-bucket.options.ts +23 -0
- package/src/limiters/leaky-bucket/leaky-bucket.policy.ts +134 -0
- package/src/limiters/leaky-bucket/leaky-bucket.state.ts +9 -0
- package/src/limiters/leaky-bucket/leaky-bucket.status.ts +36 -0
- package/src/limiters/sliding-window-counter/index.ts +7 -0
- package/src/limiters/sliding-window-counter/sliding-window-counter.limiter.ts +76 -0
- package/src/limiters/sliding-window-counter/sliding-window-counter.options.ts +20 -0
- package/src/limiters/sliding-window-counter/sliding-window-counter.policy.ts +167 -0
- package/src/limiters/sliding-window-counter/sliding-window-counter.state.ts +10 -0
- package/src/limiters/sliding-window-counter/sliding-window-counter.status.ts +53 -0
- package/src/limiters/sliding-window-log/index.ts +4 -0
- package/src/limiters/sliding-window-log/sliding-window-log.limiter.ts +65 -0
- package/src/limiters/sliding-window-log/sliding-window-log.options.ts +20 -0
- package/src/limiters/sliding-window-log/sliding-window-log.policy.ts +166 -0
- package/src/limiters/sliding-window-log/sliding-window-log.state.ts +19 -0
- package/src/limiters/sliding-window-log/sliding-window-log.status.ts +44 -0
- package/src/limiters/token-bucket/index.ts +4 -0
- package/src/limiters/token-bucket/token-bucket.limiter.ts +110 -0
- package/src/limiters/token-bucket/token-bucket.options.ts +17 -0
- package/src/limiters/token-bucket/token-bucket.policy.ts +155 -0
- package/src/limiters/token-bucket/token-bucket.state.ts +10 -0
- package/src/limiters/token-bucket/token-bucket.status.ts +36 -0
- package/src/runtime/default-clock.ts +8 -0
- package/src/runtime/execution-tickets.ts +34 -0
- package/src/runtime/in-memory-state-store.ts +135 -0
- package/src/runtime/rate-limiter.executor.ts +286 -0
- package/src/runtime/semaphore.ts +31 -0
- package/src/runtime/task.ts +141 -0
- package/src/types/limit-behavior.ts +8 -0
- package/src/utils/generate-random-string.ts +16 -0
- package/src/utils/promise-with-resolvers.ts +23 -0
- package/src/utils/sanitize-error.ts +4 -0
- package/src/utils/sanitize-priority.ts +22 -0
- package/src/utils/validate-cost.ts +16 -0
- package/tests/integration/limiters/fixed-window.limiter.spec.ts +371 -0
- package/tests/integration/limiters/generic-cell.limiter.spec.ts +361 -0
- package/tests/integration/limiters/http-response-based.limiter.spec.ts +833 -0
- package/tests/integration/limiters/leaky-bucket.spec.ts +357 -0
- package/tests/integration/limiters/sliding-window-counter.limiter.spec.ts +175 -0
- package/tests/integration/limiters/sliding-window-log.spec.ts +185 -0
- package/tests/integration/limiters/token-bucket.limiter.spec.ts +363 -0
- package/tests/tsconfig.json +4 -0
- package/tests/unit/policies/composite.policy.spec.ts +244 -0
- package/tests/unit/policies/fixed-window.policy.spec.ts +260 -0
- package/tests/unit/policies/generic-cell.policy.spec.ts +178 -0
- package/tests/unit/policies/leaky-bucket.policy.spec.ts +215 -0
- package/tests/unit/policies/sliding-window-counter.policy.spec.ts +209 -0
- package/tests/unit/policies/sliding-window-log.policy.spec.ts +285 -0
- package/tests/unit/policies/token-bucket.policy.spec.ts +371 -0
- package/tests/unit/runtime/execution-tickets.spec.ts +121 -0
- package/tests/unit/runtime/in-memory-state-store.spec.ts +238 -0
- package/tests/unit/runtime/rate-limiter.executor.spec.ts +353 -0
- package/tests/unit/runtime/semaphore.spec.ts +98 -0
- package/tests/unit/runtime/task.spec.ts +182 -0
- package/tests/unit/utils/generate-random-string.spec.ts +51 -0
- package/tests/unit/utils/promise-with-resolvers.spec.ts +57 -0
- package/tests/unit/utils/sanitize-priority.spec.ts +46 -0
- package/tests/unit/utils/validate-cost.spec.ts +48 -0
- package/tsconfig.json +14 -0
- package/vitest.config.js +22 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type LeakyBucketState } from './leaky-bucket.state.js';
|
|
2
|
+
import { type RateLimiterOptions } from '../../interfaces/rate-limiter-options.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Options for the Leaky Bucket rate limiter.
|
|
6
|
+
*/
|
|
7
|
+
export interface LeakyBucketOptions extends RateLimiterOptions<LeakyBucketState> {
|
|
8
|
+
/**
|
|
9
|
+
* The maximum number of requests that can be queued in the bucket.
|
|
10
|
+
*
|
|
11
|
+
* In the Leaky Bucket algorithm, this represents the maximum depth of the bucket
|
|
12
|
+
* that holds incoming requests before they leak out at a constant rate.
|
|
13
|
+
*/
|
|
14
|
+
capacity: number;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The rate at which requests are processed from the bucket (requests per second).
|
|
18
|
+
*
|
|
19
|
+
* This defines the constant rate at which requests "leak" out of the bucket
|
|
20
|
+
* and are allowed to proceed.
|
|
21
|
+
*/
|
|
22
|
+
leakRate: number;
|
|
23
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { type LeakyBucketState } from './leaky-bucket.state.js';
|
|
2
|
+
import { type LeakyBucketStatus } from './leaky-bucket.status.js';
|
|
3
|
+
import { type RateLimitPolicy, type RateLimitPolicyResult } from '../../core/rate-limit-policy.js';
|
|
4
|
+
import { validateCost } from '../../utils/validate-cost.js';
|
|
5
|
+
|
|
6
|
+
/** @internal */
|
|
7
|
+
export class LeakyBucketPolicy implements RateLimitPolicy<LeakyBucketState, LeakyBucketStatus> {
|
|
8
|
+
constructor(
|
|
9
|
+
private readonly _capacity: number,
|
|
10
|
+
private readonly _leakRate: number,
|
|
11
|
+
private readonly _maxOverflow: number = Number.POSITIVE_INFINITY,
|
|
12
|
+
) {
|
|
13
|
+
if (!Number.isFinite(_capacity) || !Number.isSafeInteger(_capacity) || _capacity <= 0) {
|
|
14
|
+
throw new Error(`Invalid capacity: ${_capacity}. Must be a positive integer.`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!Number.isFinite(_leakRate) || _leakRate <= 0) {
|
|
18
|
+
throw new Error(`Invalid leakRate: ${_leakRate}. Must be a positive number.`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (_maxOverflow < 0 || (!Number.isSafeInteger(_maxOverflow) && _maxOverflow !== Number.POSITIVE_INFINITY)) {
|
|
22
|
+
throw new Error(`Invalid maxOverflow: ${_maxOverflow}. Must be a non-negative integer or Infinity.`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public get capacity(): number {
|
|
27
|
+
return this._capacity;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public get leakRate(): number {
|
|
31
|
+
return this._leakRate;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public getInitialState(): LeakyBucketState {
|
|
35
|
+
return { level: 0, lastUpdate: 0 };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public getStatus(state: LeakyBucketState, now: number): LeakyBucketStatus {
|
|
39
|
+
const { level } = this._syncState(state, now);
|
|
40
|
+
|
|
41
|
+
const volumeToClearForOne = Math.max(0, level + 1 - this._capacity);
|
|
42
|
+
const nextAvailableAt =
|
|
43
|
+
volumeToClearForOne === 0 ? now : now + Math.ceil((volumeToClearForOne / this._leakRate) * 1000);
|
|
44
|
+
|
|
45
|
+
const resetAt = level === 0 ? now : now + Math.ceil((level / this._leakRate) * 1000);
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
capacity: this._capacity,
|
|
49
|
+
leakRate: this._leakRate,
|
|
50
|
+
level,
|
|
51
|
+
remaining: Math.max(0, this._capacity - level),
|
|
52
|
+
nextAvailableAt,
|
|
53
|
+
resetAt,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public evaluate(
|
|
58
|
+
state: LeakyBucketState,
|
|
59
|
+
now: number,
|
|
60
|
+
cost: number,
|
|
61
|
+
shouldReserve?: boolean,
|
|
62
|
+
): RateLimitPolicyResult<LeakyBucketState> {
|
|
63
|
+
validateCost(cost, this._capacity);
|
|
64
|
+
|
|
65
|
+
const { level, lastUpdate } = this._syncState(state, now);
|
|
66
|
+
const nextLevel = level + cost;
|
|
67
|
+
|
|
68
|
+
if (nextLevel <= this._capacity) {
|
|
69
|
+
return {
|
|
70
|
+
decision: { kind: 'allow' },
|
|
71
|
+
nextState: { level: nextLevel, lastUpdate },
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const overflow = nextLevel - this._capacity;
|
|
76
|
+
|
|
77
|
+
if (shouldReserve) {
|
|
78
|
+
if (overflow > this._maxOverflow) {
|
|
79
|
+
const volumeToLeak = nextLevel - (this._capacity + this._maxOverflow);
|
|
80
|
+
const waitMs = Math.ceil((volumeToLeak / this._leakRate) * 1000);
|
|
81
|
+
const retryAt = now + waitMs;
|
|
82
|
+
|
|
83
|
+
return this._deny(level, lastUpdate, retryAt);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const waitMs = Math.ceil((overflow / this._leakRate) * 1000);
|
|
87
|
+
const runAt = now + waitMs;
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
decision: { kind: 'delay', runAt },
|
|
91
|
+
nextState: { level: nextLevel, lastUpdate },
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const waitMs = Math.ceil((overflow / this._leakRate) * 1000);
|
|
96
|
+
const retryAt = now + waitMs;
|
|
97
|
+
|
|
98
|
+
return this._deny(level, lastUpdate, retryAt);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public revert(state: LeakyBucketState, cost: number, now: number): LeakyBucketState {
|
|
102
|
+
const { level, lastUpdate } = this._syncState(state, now);
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
level: Math.max(0, level - cost),
|
|
106
|
+
lastUpdate,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private _deny(level: number, lastUpdate: number, retryAt: number): RateLimitPolicyResult<LeakyBucketState> {
|
|
111
|
+
return {
|
|
112
|
+
decision: { kind: 'deny', retryAt },
|
|
113
|
+
nextState: { level, lastUpdate },
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private _syncState(state: LeakyBucketState, now: number): LeakyBucketState {
|
|
118
|
+
if (state.lastUpdate === 0) {
|
|
119
|
+
return { level: 0, lastUpdate: now };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (now <= state.lastUpdate) {
|
|
123
|
+
return state;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const elapsedMs = now - state.lastUpdate;
|
|
127
|
+
const leaked = (elapsedMs / 1000) * this._leakRate;
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
level: Math.max(0, state.level - leaked),
|
|
131
|
+
lastUpdate: now,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type RateLimiterStatus } from '../../core/rate-limiter-status.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The status of the Leaky Bucket rate limiter.
|
|
5
|
+
*/
|
|
6
|
+
export interface LeakyBucketStatus extends RateLimiterStatus {
|
|
7
|
+
/**
|
|
8
|
+
* The maximum capacity of the bucket (maximum number of requests that can be queued).
|
|
9
|
+
*/
|
|
10
|
+
capacity: number;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The rate at which requests leak from the bucket (requests per second).
|
|
14
|
+
*/
|
|
15
|
+
leakRate: number;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The current number of requests in the bucket waiting to be processed.
|
|
19
|
+
*/
|
|
20
|
+
level: number;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The number of requests that can still be added to the bucket before reaching capacity.
|
|
24
|
+
*/
|
|
25
|
+
remaining: number;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Timestamp (in milliseconds) when the next request slot will become available.
|
|
29
|
+
*/
|
|
30
|
+
nextAvailableAt: number;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Timestamp (in milliseconds) when all queued requests will have leaked (bucket becomes empty).
|
|
34
|
+
*/
|
|
35
|
+
resetAt: number;
|
|
36
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type { SlidingWindowCounterState } from './sliding-window-counter.state.js';
|
|
2
|
+
export type { SlidingWindowCounterStatus } from './sliding-window-counter.status.js';
|
|
3
|
+
export type { SlidingWindowCounterOptions } from './sliding-window-counter.options.js';
|
|
4
|
+
export {
|
|
5
|
+
type SlidingWindowCounterLimiterRunOptions,
|
|
6
|
+
SlidingWindowCounterLimiter,
|
|
7
|
+
} from './sliding-window-counter.limiter.js';
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { type SlidingWindowCounterOptions } from './sliding-window-counter.options.js';
|
|
2
|
+
import { SlidingWindowCounterPolicy } from './sliding-window-counter.policy.js';
|
|
3
|
+
import { type SlidingWindowCounterState } from './sliding-window-counter.state.js';
|
|
4
|
+
import { type SlidingWindowCounterStatus } from './sliding-window-counter.status.js';
|
|
5
|
+
import { RateLimitErrorCode } from '../../enums/rate-limit-error-code.js';
|
|
6
|
+
import { RateLimitError } from '../../errors/rate-limit.error.js';
|
|
7
|
+
import { type RateLimiterRunOptions } from '../../interfaces/rate-limiter-run-options.js';
|
|
8
|
+
import { AbstractRateLimiter, type ExecutionContext } from '../abstract-rate-limiter.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The options for running a task in the Sliding Window Counter rate limiter.
|
|
12
|
+
*/
|
|
13
|
+
export type SlidingWindowCounterLimiterRunOptions = Omit<
|
|
14
|
+
RateLimiterRunOptions,
|
|
15
|
+
'limitBehavior' | 'priority' | 'maxWaitMs'
|
|
16
|
+
>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Sliding Window Counter rate limiter.
|
|
20
|
+
*
|
|
21
|
+
* Designed primarily for client-side use to respect third-party limits or protect resources.
|
|
22
|
+
* While this can be used as a server-side limiter with custom distributed storage
|
|
23
|
+
* (e.g., Redis), it is best-effort and not recommended due to high network round-trip latency.
|
|
24
|
+
*
|
|
25
|
+
* Note: Unlike queue-based limiters, this implementation operates in strict immediate mode.
|
|
26
|
+
* It does not support request queueing, delays, priorities, or task cancellation.
|
|
27
|
+
*/
|
|
28
|
+
export class SlidingWindowCounterLimiter extends AbstractRateLimiter<
|
|
29
|
+
SlidingWindowCounterState,
|
|
30
|
+
SlidingWindowCounterStatus
|
|
31
|
+
> {
|
|
32
|
+
protected override readonly _policy: SlidingWindowCounterPolicy;
|
|
33
|
+
|
|
34
|
+
private readonly _storeTtl: number;
|
|
35
|
+
|
|
36
|
+
constructor(options: SlidingWindowCounterOptions) {
|
|
37
|
+
super(options);
|
|
38
|
+
|
|
39
|
+
this._policy = new SlidingWindowCounterPolicy(options.limit, options.windowMs);
|
|
40
|
+
this._storeTtl = options.windowMs * 2;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected override async _runInternal<T>(fn: () => T | Promise<T>, ctx: ExecutionContext): Promise<T> {
|
|
44
|
+
const now = this._clock.now();
|
|
45
|
+
|
|
46
|
+
await this._store.acquireLock?.(ctx.key);
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const state = (await this._store.get(ctx.key)) ?? this._policy.getInitialState();
|
|
50
|
+
|
|
51
|
+
const { decision, nextState } = this._policy.evaluate(state, now, ctx.cost);
|
|
52
|
+
|
|
53
|
+
if (decision.kind === 'deny') {
|
|
54
|
+
this._shouldPrintDebug &&
|
|
55
|
+
this._logger.debug(`[DENY] [id: ${ctx.id}, key: ${ctx.key}] - Retry: +${decision.retryAt - now}ms`);
|
|
56
|
+
|
|
57
|
+
throw new RateLimitError(RateLimitErrorCode.LimitExceeded, decision.retryAt);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
await this._store.set(ctx.key, nextState, this._storeTtl);
|
|
61
|
+
|
|
62
|
+
this._shouldPrintDebug &&
|
|
63
|
+
this._logger.debug(
|
|
64
|
+
`[ALLOW] [id: ${ctx.id}, key: ${ctx.key}] - ${this._getDebugStateString(nextState)}`,
|
|
65
|
+
);
|
|
66
|
+
} finally {
|
|
67
|
+
await this._store.releaseLock?.(ctx.key);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return await this._execute(fn, now, this._storeTtl, ctx);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
protected override _getDebugStateString(state: SlidingWindowCounterState): string {
|
|
74
|
+
return `lim: ${this._policy.limit}; c/p: ${state.currentCount}/${state.previousCount}`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type SlidingWindowCounterState } from './sliding-window-counter.state.js';
|
|
2
|
+
import { type RateLimiterOptions } from '../../interfaces/rate-limiter-options.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Options for the Sliding Window Counter rate limiter.
|
|
6
|
+
*/
|
|
7
|
+
export interface SlidingWindowCounterOptions extends Omit<
|
|
8
|
+
RateLimiterOptions<SlidingWindowCounterState>,
|
|
9
|
+
'queue' | 'limitBehavior'
|
|
10
|
+
> {
|
|
11
|
+
/**
|
|
12
|
+
* Maximum number of requests allowed within the time window.
|
|
13
|
+
*/
|
|
14
|
+
limit: number;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Duration of the time window in milliseconds.
|
|
18
|
+
*/
|
|
19
|
+
windowMs: number;
|
|
20
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { type SlidingWindowCounterState } from './sliding-window-counter.state.js';
|
|
2
|
+
import { type SlidingWindowCounterStatus } from './sliding-window-counter.status.js';
|
|
3
|
+
import { type RateLimitPolicy, type RateLimitPolicyResult } from '../../core/rate-limit-policy.js';
|
|
4
|
+
import { validateCost } from '../../utils/validate-cost.js';
|
|
5
|
+
|
|
6
|
+
/** @internal */
|
|
7
|
+
export class SlidingWindowCounterPolicy implements RateLimitPolicy<
|
|
8
|
+
SlidingWindowCounterState,
|
|
9
|
+
SlidingWindowCounterStatus
|
|
10
|
+
> {
|
|
11
|
+
constructor(
|
|
12
|
+
private readonly _limit: number,
|
|
13
|
+
private readonly _windowMs: number,
|
|
14
|
+
) {
|
|
15
|
+
if (!Number.isFinite(_limit) || !Number.isInteger(_limit) || _limit < 0) {
|
|
16
|
+
throw new Error(`Invalid limit: ${_limit}. Must be a positive integer.`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!Number.isFinite(_windowMs) || !Number.isInteger(_windowMs) || _windowMs <= 0) {
|
|
20
|
+
throw new Error(`Invalid windowMs: ${_windowMs}. Must be a positive integer.`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public get limit(): number {
|
|
25
|
+
return this._limit;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public get windowMs(): number {
|
|
29
|
+
return this._windowMs;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public getInitialState(): SlidingWindowCounterState {
|
|
33
|
+
return {
|
|
34
|
+
windowStart: 0,
|
|
35
|
+
currentCount: 0,
|
|
36
|
+
previousCount: 0,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public getStatus(state: SlidingWindowCounterState, now: number): SlidingWindowCounterStatus {
|
|
41
|
+
const syncedState = this._syncState(state, now);
|
|
42
|
+
const { windowStart, previousCount, currentCount } = syncedState;
|
|
43
|
+
|
|
44
|
+
const timeIntoCurrentWindow = now - windowStart;
|
|
45
|
+
const weight = (this._windowMs - timeIntoCurrentWindow) / this._windowMs;
|
|
46
|
+
const estimatedCount = Math.floor(previousCount * weight + currentCount);
|
|
47
|
+
|
|
48
|
+
const remaining = Math.max(0, this._limit - estimatedCount);
|
|
49
|
+
|
|
50
|
+
const nextAvailableAt = this._calculateAvailableAt(windowStart, previousCount, currentCount, 1, now);
|
|
51
|
+
|
|
52
|
+
let resetAt: number;
|
|
53
|
+
|
|
54
|
+
if (currentCount > 0) {
|
|
55
|
+
resetAt = windowStart + 2 * this._windowMs;
|
|
56
|
+
} else if (previousCount > 0) {
|
|
57
|
+
resetAt = windowStart + this._windowMs;
|
|
58
|
+
} else {
|
|
59
|
+
resetAt = now;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
limit: this._limit,
|
|
64
|
+
windowMs: this._windowMs,
|
|
65
|
+
windowStart,
|
|
66
|
+
currentCount,
|
|
67
|
+
previousCount,
|
|
68
|
+
estimatedCount,
|
|
69
|
+
remaining,
|
|
70
|
+
nextAvailableAt,
|
|
71
|
+
resetAt,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public evaluate(
|
|
76
|
+
state: SlidingWindowCounterState,
|
|
77
|
+
now: number,
|
|
78
|
+
cost: number,
|
|
79
|
+
): RateLimitPolicyResult<SlidingWindowCounterState> {
|
|
80
|
+
validateCost(cost, this._limit);
|
|
81
|
+
|
|
82
|
+
const syncedState = this._syncState(state, now);
|
|
83
|
+
const { windowStart, previousCount, currentCount } = syncedState;
|
|
84
|
+
|
|
85
|
+
const timeIntoCurrentWindow = now - windowStart;
|
|
86
|
+
const weight = (this._windowMs - timeIntoCurrentWindow) / this._windowMs;
|
|
87
|
+
const estimatedCount = Math.floor(previousCount * weight + currentCount);
|
|
88
|
+
|
|
89
|
+
if (estimatedCount + cost <= this._limit) {
|
|
90
|
+
return {
|
|
91
|
+
decision: { kind: 'allow' },
|
|
92
|
+
nextState: {
|
|
93
|
+
windowStart,
|
|
94
|
+
previousCount,
|
|
95
|
+
currentCount: currentCount + cost,
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const availableAt = this._calculateAvailableAt(windowStart, previousCount, currentCount, cost, now);
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
decision: { kind: 'deny', retryAt: availableAt },
|
|
104
|
+
nextState: syncedState,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public revert(state: SlidingWindowCounterState, cost: number, now: number): SlidingWindowCounterState {
|
|
109
|
+
if (cost <= 0) {
|
|
110
|
+
return state;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const syncedState = this._syncState(state, now);
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
...syncedState,
|
|
117
|
+
currentCount: Math.max(0, syncedState.currentCount - cost),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private _syncState(state: SlidingWindowCounterState, now: number): SlidingWindowCounterState {
|
|
122
|
+
const currentWindowStart = Math.floor(now / this._windowMs) * this._windowMs;
|
|
123
|
+
|
|
124
|
+
if (currentWindowStart <= state.windowStart) {
|
|
125
|
+
return state;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const windowsPassed = Math.floor((currentWindowStart - state.windowStart) / this._windowMs);
|
|
129
|
+
|
|
130
|
+
if (windowsPassed > 1) {
|
|
131
|
+
return { windowStart: currentWindowStart, previousCount: 0, currentCount: 0 };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
windowStart: currentWindowStart,
|
|
136
|
+
previousCount: state.currentCount,
|
|
137
|
+
currentCount: 0,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private _calculateAvailableAt(windowStart: number, prev: number, curr: number, cost: number, now: number): number {
|
|
142
|
+
if (curr + cost <= this._limit) {
|
|
143
|
+
if (prev === 0) {
|
|
144
|
+
return now;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const numerator = this._windowMs * (prev + curr + cost - this._limit - 1);
|
|
148
|
+
const t = Math.max(0, Math.floor(numerator / prev) + 1);
|
|
149
|
+
const absoluteTime = windowStart + t;
|
|
150
|
+
|
|
151
|
+
return Math.max(now, absoluteTime);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const newPrev = curr;
|
|
155
|
+
const newWindowStart = windowStart + this._windowMs;
|
|
156
|
+
|
|
157
|
+
if (newPrev === 0) {
|
|
158
|
+
return Math.max(now, newWindowStart);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const numerator = this._windowMs * (newPrev + cost - this._limit - 1);
|
|
162
|
+
const t = Math.max(0, Math.floor(numerator / newPrev) + 1);
|
|
163
|
+
const absoluteTime = newWindowStart + t;
|
|
164
|
+
|
|
165
|
+
return Math.max(now, absoluteTime);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sliding Window Counter rate limiter state.
|
|
3
|
+
*
|
|
4
|
+
* When using a distributed state store, make sure it properly serializes and deserializes the state.
|
|
5
|
+
*/
|
|
6
|
+
export interface SlidingWindowCounterState {
|
|
7
|
+
windowStart: number;
|
|
8
|
+
currentCount: number;
|
|
9
|
+
previousCount: number;
|
|
10
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type RateLimiterStatus } from '../../core/rate-limiter-status.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The status of the Sliding Window Counter rate limiter.
|
|
5
|
+
*/
|
|
6
|
+
export interface SlidingWindowCounterStatus extends RateLimiterStatus {
|
|
7
|
+
/**
|
|
8
|
+
* Maximum number of requests allowed within the time window.
|
|
9
|
+
*/
|
|
10
|
+
limit: number;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Duration of the time window in milliseconds.
|
|
14
|
+
*/
|
|
15
|
+
windowMs: number;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Timestamp (in milliseconds) when the current window started.
|
|
19
|
+
*/
|
|
20
|
+
windowStart: number;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Number of requests made in the current window.
|
|
24
|
+
*/
|
|
25
|
+
currentCount: number;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Number of requests made in the previous window.
|
|
29
|
+
*/
|
|
30
|
+
previousCount: number;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Estimated request count based on the sliding window algorithm.
|
|
34
|
+
*
|
|
35
|
+
* Calculated by combining current and previous window counts proportionally.
|
|
36
|
+
*/
|
|
37
|
+
estimatedCount: number;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Number of requests remaining before hitting the limit.
|
|
41
|
+
*/
|
|
42
|
+
remaining: number;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Timestamp (in milliseconds) when the next request slot becomes available.
|
|
46
|
+
*/
|
|
47
|
+
nextAvailableAt: number;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Timestamp (in milliseconds) when the current window resets.
|
|
51
|
+
*/
|
|
52
|
+
resetAt: number;
|
|
53
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { SlidingWindowLogEntry, SlidingWindowLogState } from './sliding-window-log.state.js';
|
|
2
|
+
export type { SlidingWindowLogStatus } from './sliding-window-log.status.js';
|
|
3
|
+
export type { SlidingWindowLogOptions } from './sliding-window-log.options.js';
|
|
4
|
+
export { type SlidingWindowLogLimiterRunOptions, SlidingWindowLogLimiter } from './sliding-window-log.limiter.js';
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type SlidingWindowLogOptions } from './sliding-window-log.options.js';
|
|
2
|
+
import { SlidingWindowLogPolicy } from './sliding-window-log.policy.js';
|
|
3
|
+
import { type SlidingWindowLogState } from './sliding-window-log.state.js';
|
|
4
|
+
import { type SlidingWindowLogStatus } from './sliding-window-log.status.js';
|
|
5
|
+
import { RateLimitErrorCode } from '../../enums/rate-limit-error-code.js';
|
|
6
|
+
import { RateLimitError } from '../../errors/rate-limit.error.js';
|
|
7
|
+
import { type RateLimiterRunOptions } from '../../interfaces/rate-limiter-run-options.js';
|
|
8
|
+
import { AbstractRateLimiter, type ExecutionContext } from '../abstract-rate-limiter.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The options for running a task in the Sliding Window Log rate limiter.
|
|
12
|
+
*/
|
|
13
|
+
export type SlidingWindowLogLimiterRunOptions = Omit<RateLimiterRunOptions, 'limitBehavior' | 'priority'>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Sliding Window Log rate limiter.
|
|
17
|
+
*
|
|
18
|
+
* Designed primarily for client-side use to respect third-party limits or protect resources.
|
|
19
|
+
* While this can be used as a server-side limiter with custom distributed storage
|
|
20
|
+
* (e.g., Redis), it is best-effort and not recommended due to high network round-trip latency.
|
|
21
|
+
*
|
|
22
|
+
* Note: Unlike queue-based limiters, this implementation operates in strict immediate mode.
|
|
23
|
+
* It does not support request queueing, delays, priorities, or task cancellation.
|
|
24
|
+
*/
|
|
25
|
+
export class SlidingWindowLogLimiter extends AbstractRateLimiter<SlidingWindowLogState, SlidingWindowLogStatus> {
|
|
26
|
+
protected override readonly _policy: SlidingWindowLogPolicy;
|
|
27
|
+
|
|
28
|
+
constructor(options: SlidingWindowLogOptions) {
|
|
29
|
+
super(options);
|
|
30
|
+
|
|
31
|
+
this._policy = new SlidingWindowLogPolicy(options.limit, options.windowMs);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
protected override async _runInternal<T>(fn: () => T | Promise<T>, ctx: ExecutionContext): Promise<T> {
|
|
35
|
+
const now = this._clock.now();
|
|
36
|
+
const storeTtlMs = this._policy.windowMs;
|
|
37
|
+
|
|
38
|
+
await this._store.acquireLock?.(ctx.key);
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const state = (await this._store.get(ctx.key)) ?? this._policy.getInitialState();
|
|
42
|
+
|
|
43
|
+
const { decision, nextState } = this._policy.evaluate(state, now, ctx.cost);
|
|
44
|
+
|
|
45
|
+
if (decision.kind === 'deny') {
|
|
46
|
+
this._logger.debug(`[DENY] [id: ${ctx.id}, key: ${ctx.key}] - Retry: +${decision.retryAt - now}ms`);
|
|
47
|
+
throw new RateLimitError(RateLimitErrorCode.LimitExceeded, decision.retryAt);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await this._store.set(ctx.key, nextState, storeTtlMs);
|
|
51
|
+
|
|
52
|
+
this._logger.debug(
|
|
53
|
+
`[ALLOW] [id: ${ctx.id}, key: ${ctx.key}] - used/lim: ${nextState.totalUsed}/${this._policy.limit} logs: ${nextState.logs.size}`,
|
|
54
|
+
);
|
|
55
|
+
} finally {
|
|
56
|
+
await this._store.releaseLock?.(ctx.key);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return await this._execute<T>(fn, now, storeTtlMs, ctx);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protected override _getDebugStateString(state: SlidingWindowLogState): string {
|
|
63
|
+
return `used/lim: ${state.totalUsed}/${this._policy.limit} logs: ${state.logs.size}`;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type SlidingWindowLogState } from './sliding-window-log.state.js';
|
|
2
|
+
import { type RateLimiterOptions } from '../../interfaces/rate-limiter-options.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Options for the Sliding Window Log rate limiter.
|
|
6
|
+
*/
|
|
7
|
+
export interface SlidingWindowLogOptions extends Omit<
|
|
8
|
+
RateLimiterOptions<SlidingWindowLogState>,
|
|
9
|
+
'queue' | 'limitBehavior'
|
|
10
|
+
> {
|
|
11
|
+
/**
|
|
12
|
+
* Maximum number of requests allowed within the time window.
|
|
13
|
+
*/
|
|
14
|
+
limit: number;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Duration of the time window in milliseconds.
|
|
18
|
+
*/
|
|
19
|
+
windowMs: number;
|
|
20
|
+
}
|