Skip to content

Commit 4455db1

Browse files
committed
updated types
1 parent 3460adc commit 4455db1

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/@types/rateLimit.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,19 @@ export interface TokenBucketOptions {
4545
refillRate: number;
4646
}
4747

48+
/**
49+
* @type {number} windowSize - Size of each fixed window and the rolling window
50+
* @type {number} capacity - Number of tokens a window can hold
51+
*/
52+
export interface SlidingWindowCounterOptions {
53+
windowSize: number;
54+
capacity: number;
55+
}
56+
4857
// TODO: This will be a union type where we can specify Option types for other Rate Limiters
4958
// Record<string, never> represents the empty object for alogorithms that don't require settings
5059
// and might be able to be removed in the future.
51-
export type RateLimiterOptions = TokenBucketOptions | Record<string, never>;
60+
export type RateLimiterOptions =
61+
| TokenBucketOptions
62+
| SlidingWindowCounterOptions
63+
| Record<string, never>;

src/rateLimiters/slidingWindowCounter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ class SlidingWindowCounter implements RateLimiter {
166166

167167
// if request is blocked
168168
await this.client.setex(uuid, keyExpiry, JSON.stringify(updatedUserWindow));
169-
return { success: false, tokens: this.capacity - updatedUserWindow.currentTokens };
169+
return {
170+
success: false,
171+
tokens: this.capacity - (updatedUserWindow.currentTokens + previousRollingTokens),
172+
};
170173
}
171174

172175
/**

0 commit comments

Comments
 (0)