* 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)
| 187 | * @returns {WorkerTokenBucket} |
| 188 | */ |
| 189 | function 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 |
no test coverage detected