mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-11-04 15:34:13 +00:00 
			
		
		
		
	fix NaN from extremely high rate limits
This commit is contained in:
		
							parent
							
								
									2781f53d6b
								
							
						
					
					
						commit
						a7a1edc92e
					
				
					 2 changed files with 15 additions and 1 deletions
				
			
		| 
						 | 
					@ -155,7 +155,7 @@ export class SkRateLimiterService {
 | 
				
			||||||
					type: 'bucket',
 | 
										type: 'bucket',
 | 
				
			||||||
					key: limit.key,
 | 
										key: limit.key,
 | 
				
			||||||
					size: limit.max,
 | 
										size: limit.max,
 | 
				
			||||||
					dripRate: Math.round(limit.duration / limit.max),
 | 
										dripRate: Math.max(Math.round(limit.duration / limit.max), 1),
 | 
				
			||||||
				}, actor, factor),
 | 
									}, actor, factor),
 | 
				
			||||||
			);
 | 
								);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -564,6 +564,20 @@ describe(SkRateLimiterService, () => {
 | 
				
			||||||
				expect(counter?.c).toBe(1);
 | 
									expect(counter?.c).toBe(1);
 | 
				
			||||||
				expect(counter?.t).toBe(0);
 | 
									expect(counter?.t).toBe(0);
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								it('should not allow dripRate to be lower than 0', async () => {
 | 
				
			||||||
 | 
									// real-world case; taken from StreamingApiServerService
 | 
				
			||||||
 | 
									limit.max = 4096;
 | 
				
			||||||
 | 
									limit.duration = 2000;
 | 
				
			||||||
 | 
									counter = { c: 4096, t: 0 };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									const i1 = await serviceUnderTest().limit(limit, actor);
 | 
				
			||||||
 | 
									mockTimeService.now = 1;
 | 
				
			||||||
 | 
									const i2 = await serviceUnderTest().limit(limit, actor);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									expect(i1.blocked).toBeTruthy();
 | 
				
			||||||
 | 
									expect(i2.blocked).toBeFalsy();
 | 
				
			||||||
 | 
								});
 | 
				
			||||||
		});
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		describe('with legacy limit and min interval', () => {
 | 
							describe('with legacy limit and min interval', () => {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue