Skip to content

Commit 42fed39

Browse files
authored
Improve concurrent request limiter (#1607)
1 parent 591587c commit 42fed39

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

services/libs/redis/src/rateLimiter.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,19 @@ export class RateLimiter implements IRateLimiter {
3333
export class ConcurrentRequestLimiter implements IConcurrentRequestLimiter {
3434
constructor(
3535
private readonly cache: ICache,
36+
// max concurrent requests per integrationId
3637
private readonly maxConcurrentRequests: number,
3738
private readonly requestKey: string,
39+
// cache key will be deleted after this time since last increment / decrement
40+
private readonly maxLockTimeSeconds = 30,
3841
) {
3942
this.cache = cache
4043
this.maxConcurrentRequests = maxConcurrentRequests
4144
this.requestKey = requestKey
45+
this.maxLockTimeSeconds = maxLockTimeSeconds
4246
}
4347

44-
public async checkConcurrentRequestLimit(integrationId: string, retries = 5, sleepTimeMs = 1000) {
48+
public async checkConcurrentRequestLimit(integrationId: string, retries = 200, sleepTimeMs = 50) {
4549
const key = this.getRequestKey(integrationId)
4650
const value = await this.cache.get(key)
4751
const currentRequests = value === null ? 0 : parseInt(value)
@@ -60,12 +64,12 @@ export class ConcurrentRequestLimiter implements IConcurrentRequestLimiter {
6064

6165
public async incrementConcurrentRequest(integrationId: string) {
6266
const key = this.getRequestKey(integrationId)
63-
await this.cache.increment(key, 1)
67+
await this.cache.increment(key, 1, this.maxLockTimeSeconds)
6468
}
6569

6670
public async decrementConcurrentRequest(integrationId: string) {
6771
const key = this.getRequestKey(integrationId)
68-
await this.cache.decrement(key, 1)
72+
await this.cache.decrement(key, 1, this.maxLockTimeSeconds)
6973
}
7074

7175
public async processWithLimit<T>(integrationId: string, func: () => Promise<T>): Promise<T> {

0 commit comments

Comments
 (0)