Skip to content

Commit d210a6a

Browse files
committed
CLDSRV-781: Add ratelimiting pre MD fetch
1 parent b511b78 commit d210a6a

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

lib/api/api.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ const checkHttpHeadersSize = require('./apiUtils/object/checkHttpHeadersSize');
8282
const constants = require('../../constants');
8383
const { config } = require('../Config.js');
8484
const { validateMethodChecksumNoChunking } = require('./apiUtils/integrity/validateChecksums');
85+
const {
86+
getRateLimitFromCache,
87+
checkRateLimitWithConfig,
88+
} = require('./apiUtils/rateLimit/helpers');
8589

8690
const monitoringMap = policies.actionMaps.actionMonitoringMapS3;
8791

@@ -178,7 +182,12 @@ const api = {
178182
}
179183
let returnTagCount = true;
180184

181-
const validationRes = validateQueryAndHeaders(request, log);
185+
// Initialize rate limit tracker flag
186+
request.rateLimitAlreadyChecked = false;
187+
188+
// Process the request with validation, authentication, and execution
189+
const processRequest = () => {
190+
const validationRes = validateQueryAndHeaders(request, log);
182191
if (validationRes.error) {
183192
log.debug('request query / header validation failed', {
184193
error: validationRes.error,
@@ -335,6 +344,48 @@ const api = {
335344
}
336345
return this[apiMethod](userInfo, request, log, methodCallback);
337346
});
347+
}; // End of processRequest helper function
348+
349+
// Cache-only rate limit check (fast path, no metadata fetch)
350+
// If config is cached, apply rate limiting now
351+
// If not cached, metadata validation functions will handle it
352+
if (request.bucketName && config.rateLimiting?.enabled) {
353+
const cachedConfig = getRateLimitFromCache(request.bucketName);
354+
355+
if (cachedConfig !== undefined) {
356+
// Cache hit - apply rate limiting NOW (fast path)
357+
return checkRateLimitWithConfig(
358+
request.bucketName,
359+
cachedConfig,
360+
log,
361+
(rateLimitErr, rateLimited) => {
362+
if (rateLimitErr) {
363+
log.error('Rate limit check error in api.js', {
364+
error: rateLimitErr,
365+
});
366+
}
367+
368+
if (rateLimited) {
369+
log.addDefaultFields({
370+
rateLimited: true,
371+
rateLimitSource: cachedConfig.source,
372+
});
373+
return callback(config.rateLimiting.error);
374+
}
375+
376+
// Set tracker - rate limiting already applied
377+
request.rateLimitAlreadyChecked = true;
378+
379+
// Continue with normal flow
380+
return processRequest();
381+
}
382+
);
383+
}
384+
}
385+
386+
// Cache miss or no bucket name - continue to metadata validation
387+
// Rate limiting will happen there after metadata fetch
388+
return processRequest();
338389
},
339390
bucketDelete,
340391
bucketDeleteCors,

0 commit comments

Comments
 (0)