Skip to content

Commit 5b69b19

Browse files
committed
created dedicated fixed window type
1 parent bff6b2b commit 5b69b19

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

src/@types/rateLimit.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ export interface RedisBucket {
2424
timestamp: number;
2525
}
2626

27-
export interface RedisWindow {
27+
export interface FixedWindow {
2828
currentTokens: number;
29-
previousTokens: number;
3029
fixedWindowStart: number;
3130
}
31+
export interface RedisWindow extends FixedWindow {
32+
previousTokens: number;
33+
}
3234

3335
export type RedisLog = RedisBucket[];
3436

src/rateLimiters/fixedWindow.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Redis from 'ioredis';
2-
import { RateLimiter, RateLimiterResponse, RedisWindow } from '../@types/rateLimit';
2+
import { RateLimiter, RateLimiterResponse, FixedWindow as Window } from '../@types/rateLimit';
33

44
/**
55
* The FixedWindow instance of a RateLimiter limits requests based on a unique user ID and a fixed time window.
@@ -69,7 +69,7 @@ class FixedWindow implements RateLimiter {
6969
const windowJSON = await this.client.get(uuid);
7070

7171
if (windowJSON === null) {
72-
const newUserWindow: RedisWindow = {
72+
const newUserWindow: Window = {
7373
currentTokens: tokens > this.capacity ? 0 : tokens,
7474
fixedWindowStart: timestamp,
7575
};
@@ -81,7 +81,7 @@ class FixedWindow implements RateLimiter {
8181
await this.client.setex(uuid, keyExpiry, JSON.stringify(newUserWindow));
8282
return { success: true, tokens: this.capacity - newUserWindow.currentTokens };
8383
}
84-
const window: RedisWindow = await JSON.parse(windowJSON);
84+
const window: Window = await JSON.parse(windowJSON);
8585

8686
const updatedUserWindow = this.updateTimeWindow(window, timestamp);
8787
updatedUserWindow.currentTokens += tokens;
@@ -113,8 +113,8 @@ class FixedWindow implements RateLimiter {
113113
this.client.flushall();
114114
}
115115

116-
private updateTimeWindow = (window: RedisWindow, timestamp: number): RedisWindow => {
117-
const updatedUserWindow: RedisWindow = {
116+
private updateTimeWindow = (window: Window, timestamp: number): Window => {
117+
const updatedUserWindow: Window = {
118118
currentTokens: window.currentTokens,
119119
fixedWindowStart: window.fixedWindowStart,
120120
};

test/rateLimiters/fixedWindow.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ioredis from 'ioredis';
2-
import { RedisWindow } from '../../src/@types/rateLimit';
2+
import { FixedWindow as Window } from '../../src/@types/rateLimit';
33
import FixedWindow from '../../src/rateLimiters/fixedWindow';
44

55
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -15,7 +15,7 @@ const user1 = '1';
1515
const user2 = '2';
1616
const user3 = '3';
1717

18-
async function getWindowFromClient(redisClient: ioredis.Redis, uuid: string): Promise<RedisWindow> {
18+
async function getWindowFromClient(redisClient: ioredis.Redis, uuid: string): Promise<Window> {
1919
const res = await redisClient.get(uuid);
2020
// if no uuid is found, return -1 for tokens and timestamp, which are both impossible
2121
if (res === null) return { currentTokens: -1, fixedWindowStart: -1 };
@@ -28,7 +28,7 @@ async function setTokenCountInClient(
2828
currentTokens: number,
2929
fixedWindowStart: number
3030
) {
31-
const value: RedisWindow = { currentTokens, fixedWindowStart };
31+
const value: Window = { currentTokens, fixedWindowStart };
3232
await redisClient.set(uuid, JSON.stringify(value));
3333
}
3434
describe('Test FixedWindow Rate Limiter', () => {

0 commit comments

Comments
 (0)