Common - Throttle update + Metrics (#15413)

This commit is contained in:
Etienne
2025-10-28 19:15:18 +01:00
committed by GitHub
parent 8d823827da
commit 5ae7674b9d
7 changed files with 59 additions and 45 deletions
@@ -29,4 +29,5 @@ export enum MetricsKeys {
CronJobDeletedWorkspace = 'cron-job/deleted-workspace',
JobWebhookCallCompleted = 'job/webhook-call-completed',
SignUpSuccess = 'sign-up/success',
CommonApiQueryRateLimited = 'common-api-query/rate-limited',
}
@@ -31,7 +31,7 @@ describe('ThrottlerService', () => {
jest.clearAllMocks();
});
describe('tokenBucketThrottle', () => {
describe('tokenBucketThrottleOrThrow', () => {
const key = 'test-throttle-key';
const maxTokens = 100;
const timeWindow = 1000; // 1 second
@@ -39,7 +39,7 @@ describe('ThrottlerService', () => {
it('should allow request when tokens are available (first request)', async () => {
cacheStorageService.get.mockResolvedValue(null);
await service.tokenBucketThrottle(key, 10, maxTokens, timeWindow);
await service.tokenBucketThrottleOrThrow(key, 10, maxTokens, timeWindow);
expect(cacheStorageService.get).toHaveBeenCalledWith(key);
expect(cacheStorageService.set).toHaveBeenCalledWith(
@@ -60,7 +60,7 @@ describe('ThrottlerService', () => {
lastRefillAt: now - 100,
});
await service.tokenBucketThrottle(key, 10, maxTokens, timeWindow);
await service.tokenBucketThrottleOrThrow(key, 10, maxTokens, timeWindow);
expect(cacheStorageService.get).toHaveBeenCalledWith(key);
expect(cacheStorageService.set).toHaveBeenCalledWith(
@@ -82,11 +82,11 @@ describe('ThrottlerService', () => {
});
await expect(
service.tokenBucketThrottle(key, 10, maxTokens, timeWindow),
service.tokenBucketThrottleOrThrow(key, 10, maxTokens, timeWindow),
).rejects.toThrow(ThrottlerException);
await expect(
service.tokenBucketThrottle(key, 10, maxTokens, timeWindow),
service.tokenBucketThrottleOrThrow(key, 10, maxTokens, timeWindow),
).rejects.toThrow('Limit reached');
expect(cacheStorageService.set).not.toHaveBeenCalled();
@@ -105,7 +105,7 @@ describe('ThrottlerService', () => {
jest.spyOn(Date, 'now').mockReturnValue(now);
await service.tokenBucketThrottle(key, 10, maxTokens, timeWindow);
await service.tokenBucketThrottleOrThrow(key, 10, maxTokens, timeWindow);
expect(cacheStorageService.set).toHaveBeenCalledWith(
key,
@@ -130,7 +130,7 @@ describe('ThrottlerService', () => {
jest.spyOn(Date, 'now').mockReturnValue(now);
await service.tokenBucketThrottle(key, 10, maxTokens, timeWindow);
await service.tokenBucketThrottleOrThrow(key, 10, maxTokens, timeWindow);
// Available tokens should be capped at maxTokens (100)
expect(cacheStorageService.set).toHaveBeenCalledWith(
@@ -28,7 +28,7 @@ export class ThrottlerService {
await this.cacheStorage.set(key, currentCount + 1, ttl);
}
async tokenBucketThrottle(
async tokenBucketThrottleOrThrow(
key: string,
tokensToConsume: number,
maxTokens: number,
@@ -975,7 +975,7 @@ export class ConfigVariables {
type: ConfigVariableType.NUMBER,
})
@CastToPositiveNumber()
API_RATE_LIMITING_LONG_LIMIT = 1000;
API_RATE_LIMITING_LONG_LIMIT = 100;
@ConfigVariablesMetadata({
group: ConfigVariablesGroup.SSL,