File tree Expand file tree Collapse file tree 2 files changed +17
-2
lines changed Expand file tree Collapse file tree 2 files changed +17
-2
lines changed Original file line number Diff line number Diff 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 > ;
Original file line number Diff line number Diff 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 /**
You can’t perform that action at this time.
0 commit comments