@@ -35,7 +35,7 @@ async function setTokenCountInClient(
3535 await redisClient . set ( uuid , JSON . stringify ( value ) ) ;
3636}
3737
38- describe ( 'Test TokenBucket Rate Limiter' , ( ) => {
38+ describe ( 'Test SlidingWindowCounter Rate Limiter' , ( ) => {
3939 beforeEach ( async ( ) => {
4040 // init a mock redis cache
4141 client = new RedisMock ( ) ;
@@ -341,88 +341,80 @@ describe('Test TokenBucket Rate Limiter', () => {
341341 expect ( count1 . currentTokens ) . toBe ( 0 ) ;
342342 expect ( count1 . previousTokens ) . toBe ( initRequest ) ;
343343 } ) ;
344+ } ) ;
344345
345- test ( 'rolling window at 50% blocks requests over allowed limit set by formula' , async ( ) => {
346- // 50% of rolling window present in previous fixed window
347- // 1.5*60000 = 90000 (time after initial fixedWindowStart
348- // to set rolling window at 50% of previous fixed window)
346+ test ( 'rolling window at 50% blocks requests over allowed limit set by formula' , async ( ) => {
347+ // 50% of rolling window present in previous fixed window
348+ // 1.5*60000 = 90000 (time after initial fixedWindowStart
349+ // to set rolling window at 50% of previous fixed window)
349350
350- // to set initial fixedWindowStart
351- await setTokenCountInClient ( client , user4 , 0 , 0 , timestamp ) ;
351+ // to set initial fixedWindowStart
352+ await setTokenCountInClient ( client , user4 , 0 , 0 , timestamp ) ;
352353
353- const initRequest = 8 ;
354+ const initRequest = 8 ;
354355
355- // large request at very end of first fixed window
356- await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE - 1 , initRequest ) ;
356+ // large request at very end of first fixed window
357+ await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE - 1 , initRequest ) ;
357358
358- // 7 + 8 * .5 = 11, over capacity (request should be blocked)
359- const result = await limiter . processRequest (
360- user4 ,
361- timestamp + WINDOW_SIZE * 1.5 ,
362- 7
363- ) ;
364- expect ( result . tokens ) . toBe ( 6 ) ;
365- expect ( result . success ) . toBe ( false ) ;
359+ // 7 + 8 * .5 = 11, over capacity (request should be blocked)
360+ const result = await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE * 1.5 , 7 ) ;
361+ expect ( result . tokens ) . toBe ( 6 ) ;
362+ expect ( result . success ) . toBe ( false ) ;
366363
367- // currentTokens (in current fixed window): 0
368- // previousTokens (in previous fixed window): 8
369- const count = await getWindowFromClient ( client , user4 ) ;
370- expect ( count . currentTokens ) . toBe ( 0 ) ;
371- expect ( count . previousTokens ) . toBe ( initRequest ) ;
372- } ) ;
364+ // currentTokens (in current fixed window): 0
365+ // previousTokens (in previous fixed window): 8
366+ const count = await getWindowFromClient ( client , user4 ) ;
367+ expect ( count . currentTokens ) . toBe ( 0 ) ;
368+ expect ( count . previousTokens ) . toBe ( initRequest ) ;
369+ } ) ;
373370
374- test ( 'rolling window at 25% blocks requests over allowed limit set by formula' , async ( ) => {
375- // 25% of rolling window present in previous fixed window
376- // 1.75*60000 = 105000 (time after initial fixedWindowStart
377- // to set rolling window at 25% of previous fixed window)
371+ test ( 'rolling window at 25% blocks requests over allowed limit set by formula' , async ( ) => {
372+ // 25% of rolling window present in previous fixed window
373+ // 1.75*60000 = 105000 (time after initial fixedWindowStart
374+ // to set rolling window at 25% of previous fixed window)
378375
379- // to set initial fixedWindowStart
380- await setTokenCountInClient ( client , user4 , 0 , 0 , timestamp ) ;
376+ // to set initial fixedWindowStart
377+ await setTokenCountInClient ( client , user4 , 0 , 0 , timestamp ) ;
381378
382- const initRequest = 8 ;
379+ const initRequest = 8 ;
383380
384- // large request at very end of first fixed window
385- await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE - 1 , initRequest ) ;
381+ // large request at very end of first fixed window
382+ await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE - 1 , initRequest ) ;
386383
387- // 9 + 8 * .25 = 11, over capacity (request should be blocked)
388- const result = await limiter . processRequest (
389- user4 ,
390- timestamp + WINDOW_SIZE * 1.75 ,
391- 9
392- ) ;
393- expect ( result . tokens ) . toBe ( 8 ) ;
394- expect ( result . success ) . toBe ( false ) ;
384+ // 9 + 8 * .25 = 11, over capacity (request should be blocked)
385+ const result = await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE * 1.75 , 9 ) ;
386+ expect ( result . tokens ) . toBe ( 8 ) ;
387+ expect ( result . success ) . toBe ( false ) ;
395388
396- // currentTokens (in current fixed window): 0
397- // previousTokens (in previous fixed window): 8
398- const count = await getWindowFromClient ( client , user4 ) ;
399- expect ( count . currentTokens ) . toBe ( 0 ) ;
400- expect ( count . previousTokens ) . toBe ( initRequest ) ;
401- } ) ;
402- test ( 'rolling window at 100% blocks requests over allowed limit set by formula' , async ( ) => {
403- // 1% of rolling window present in previous fixed window
404- // .01*60000 = 600 (time after initial fixedWindowStart
405- // to set rolling window at 100% of previous fixed window)
389+ // currentTokens (in current fixed window): 0
390+ // previousTokens (in previous fixed window): 8
391+ const count = await getWindowFromClient ( client , user4 ) ;
392+ expect ( count . currentTokens ) . toBe ( 0 ) ;
393+ expect ( count . previousTokens ) . toBe ( initRequest ) ;
394+ } ) ;
395+ test ( 'rolling window at 100% blocks requests over allowed limit set by formula' , async ( ) => {
396+ // 1% of rolling window present in previous fixed window
397+ // .01*60000 = 600 (time after initial fixedWindowStart
398+ // to set rolling window at 100% of previous fixed window)
406399
407- // to set initial fixedWindowStart
408- await setTokenCountInClient ( client , user4 , 0 , 0 , timestamp ) ;
400+ // to set initial fixedWindowStart
401+ await setTokenCountInClient ( client , user4 , 0 , 0 , timestamp ) ;
409402
410- const initRequest = 8 ;
403+ const initRequest = 8 ;
411404
412- // large request at very end of first fixed window
413- await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE - 1 , initRequest ) ;
405+ // large request at very end of first fixed window
406+ await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE - 1 , initRequest ) ;
414407
415- // 11 + 8 * .01 = 11, above capacity (request should be blocked)
416- const result = await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE , 11 ) ;
417- expect ( result . tokens ) . toBe ( 2 ) ;
418- expect ( result . success ) . toBe ( false ) ;
408+ // 11 + 8 * .01 = 11, above capacity (request should be blocked)
409+ const result = await limiter . processRequest ( user4 , timestamp + WINDOW_SIZE , 11 ) ;
410+ expect ( result . tokens ) . toBe ( 2 ) ;
411+ expect ( result . success ) . toBe ( false ) ;
419412
420- // currentTokens (in current fixed window): 0
421- // previousTokens (in previous fixed window): 8
422- const count1 = await getWindowFromClient ( client , user4 ) ;
423- expect ( count1 . currentTokens ) . toBe ( 0 ) ;
424- expect ( count1 . previousTokens ) . toBe ( initRequest ) ;
425- } ) ;
413+ // currentTokens (in current fixed window): 0
414+ // previousTokens (in previous fixed window): 8
415+ const count1 = await getWindowFromClient ( client , user4 ) ;
416+ expect ( count1 . currentTokens ) . toBe ( 0 ) ;
417+ expect ( count1 . previousTokens ) . toBe ( initRequest ) ;
426418 } ) ;
427419 } ) ;
428420
0 commit comments