Skip to content

Commit 91b5df6

Browse files
committed
updated comments. enabled skipped slidingwindowlog tests
1 parent d243365 commit 91b5df6

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

src/rateLimiters/slidingWindowLog.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,32 +96,33 @@ class SlidingWindowLog implements RateLimiter {
9696
// Stop at the first timestamp that's expired and cut the rest.
9797

9898
const cutoff = timestamp - this.windowSize;
99-
let tokensInLog = 0;
99+
let tokensInLog = 0; // total active tokens in the log
100100
let cutoffIndex = 0; // index of oldest active request
101-
// TODO: Provide a timestamp for when the request will succeeed.
102-
// Compute time between response and this timestamp later on
103-
// FIXME: What should this be if the complexity is too big?
104-
let retryIndex = requestLog.length; // time the user must wait before a request can be allowed.
101+
let lastAllowedIndex = requestLog.length; // Index of oldest request in the log for which this request would be allowed.
105102

106103
for (let index = requestLog.length - 1; index >= 0; index--) {
107104
if (cutoff >= requestLog[index].timestamp) {
105+
// we reached the first expired request
108106
cutoffIndex = index + 1;
109107
break;
110108
} else {
111109
// the request is active
112110
tokensInLog += requestLog[index].tokens;
113111
if (this.capacity - tokensInLog >= tokens) {
114-
// the log is able to accept this request
115-
retryIndex = index;
112+
// the log is able to accept the current request
113+
lastAllowedIndex = index;
116114
}
117115
}
118116
}
119117

118+
// Time (ms) after which the current request would succeed if it is blocked.
120119
let retryAfter: number;
120+
121+
// Request will never be allowed
121122
if (tokens > this.capacity) retryAfter = Infinity;
122-
// need the request before retryIndex
123-
else if (retryIndex > 0)
124-
retryAfter = this.windowSize + requestLog[retryIndex - 1].timestamp;
123+
// need the request before lastAllowedIndex
124+
else if (lastAllowedIndex > 0)
125+
retryAfter = this.windowSize + requestLog[lastAllowedIndex - 1].timestamp;
125126
else retryAfter = 0; // request is allowed
126127

127128
// Conditional check to avoid unecessary slice

test/rateLimiters/slidingWindowLog.test.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ describe('SlidingWindowLog Rate Limiter', () => {
467467
expect(retryAfter).toBe(timestamp + 100 + WINDOW_SIZE);
468468
});
469469
});
470-
xtest('users have their own logs', async () => {
470+
test('users have their own logs', async () => {
471471
const requested = 6;
472472
const user3Tokens = 8;
473473
// // Add log for user 3 so we have both a user that exists in the store (3) and one that doesn't (2)
@@ -477,12 +477,14 @@ describe('SlidingWindowLog Rate Limiter', () => {
477477
await limiter.processRequest(user1, timestamp + 100, requested);
478478

479479
// // Check that each user has the expected log
480-
expect(await getLogFromClient(client, user1)).toEqual({
481-
timestamp: timestamp + 100,
482-
tokens: requested,
483-
});
480+
expect(await getLogFromClient(client, user1)).toEqual([
481+
{
482+
timestamp: timestamp + 100,
483+
tokens: requested,
484+
},
485+
]);
484486
expect(await getLogFromClient(client, user2)).toEqual([]);
485-
expect(await getLogFromClient(client, user3)).toEqual([{ timestamp, tokens: requested }]);
487+
expect(await getLogFromClient(client, user3)).toEqual([{ timestamp, tokens: user3Tokens }]);
486488

487489
await limiter.processRequest(user2, timestamp + 200, 1);
488490
expect(await getLogFromClient(client, user1)).toEqual([
@@ -497,7 +499,7 @@ describe('SlidingWindowLog Rate Limiter', () => {
497499
tokens: 1,
498500
},
499501
]);
500-
expect(await getLogFromClient(client, user3)).toEqual([{ timestamp, tokens: requested }]);
502+
expect(await getLogFromClient(client, user3)).toEqual([{ timestamp, tokens: user3Tokens }]);
501503
});
502504

503505
test('is able to be reset', async () => {
@@ -533,7 +535,7 @@ describe('SlidingWindowLog Rate Limiter', () => {
533535
);
534536
});
535537

536-
xtest('...allows custom window size and capacity', async () => {
538+
test('...allows custom window size and capacity', async () => {
537539
const customWindow = 500;
538540
const customSizelimiter = new SlidingWindowLog(customWindow, CAPACITY, client);
539541

@@ -551,18 +553,21 @@ describe('SlidingWindowLog Rate Limiter', () => {
551553
.processRequest(user1, timestamp + customWindow, CAPACITY)
552554
.then((res) => res.success);
553555

554-
const customCapacitylimiter = new SlidingWindowLog(WINDOW_SIZE, 5, client);
556+
// Reset the redis store
557+
customSizelimiter.reset();
558+
555559
const customCapacity = 5;
560+
const customCapacitylimiter = new SlidingWindowLog(WINDOW_SIZE, customCapacity, client);
556561

557-
let customWindowSuccess = await customCapacitylimiter
562+
let customCapacitySuccess = await customCapacitylimiter
558563
.processRequest(user1, timestamp, customCapacity + 1)
559564
.then((res) => res.success);
560-
expect(customSizeSuccess).toBe(false);
565+
expect(customCapacitySuccess).toBe(false);
561566

562-
customWindowSuccess = await customCapacitylimiter
567+
customCapacitySuccess = await customCapacitylimiter
563568
.processRequest(user1, timestamp + 100, customCapacity)
564569
.then((res) => res.success);
565-
expect(customWindowSuccess).toBe(true);
570+
expect(customCapacitySuccess).toBe(true);
566571
});
567572
});
568573
});

0 commit comments

Comments
 (0)