MCPcopy
hub / github.com/scality/cloudserver / getTokenBucket

Function getTokenBucket

lib/api/apiUtils/rateLimit/tokenBucket.js:189–213  ·  view source on GitHub ↗

* Get or create token bucket for a bucket * * @param {string} resourceClass - "bucket" or "account" * @param {string} resourceId - bucket name or account canonicalId * @param {string} measure - measure id e.g. "rps" * @param {object} limitConfig - Rate limit configuration * @param {object} log

(resourceClass, resourceId, measure, limitConfig, log)

Source from the content-addressed store, hash-verified

187 * @returns {WorkerTokenBucket}
188 */
189function getTokenBucket(resourceClass, resourceId, measure, limitConfig, log) {
190 const cacheKey = `${resourceClass}:${resourceId}:${measure}`;
191 let bucket = tokenBuckets.get(cacheKey);
192 if (!bucket) {
193 bucket = new WorkerTokenBucket(resourceClass, resourceId, measure, limitConfig, log);
194 tokenBuckets.set(cacheKey, bucket);
195
196 log.debug('Created token bucket', {
197 cacheKey,
198 bufferSize: bucket.bufferSize,
199 refillThreshold: bucket.refillThreshold,
200 });
201 } else {
202 const { updated, oldConfig } = bucket.updateLimit(limitConfig);
203 if (updated) {
204 log.debug('Updated token bucket limit config', {
205 cacheKey,
206 old: oldConfig,
207 new: limitConfig,
208 });
209 }
210 }
211
212 return bucket;
213}
214
215/**
216 * Get all active token buckets

Callers 1

Calls 2

debugMethod · 0.80
updateLimitMethod · 0.80

Tested by

no test coverage detected