diff --git a/libraries/helpers/src/utils/concurrency.service.ts b/libraries/helpers/src/utils/concurrency.service.ts index 1e57ea57..36c0f3fd 100644 --- a/libraries/helpers/src/utils/concurrency.service.ts +++ b/libraries/helpers/src/utils/concurrency.service.ts @@ -1,36 +1,28 @@ import { ioRedis } from '@gitroom/nestjs-libraries/redis/redis.service'; -import { timer } from '@gitroom/helpers/utils/timer'; +import Bottleneck from 'bottleneck'; + +const connection = new Bottleneck.IORedisConnection({ + client: ioRedis, +}); + +const bottleneck = new Bottleneck.Group({ + maxConcurrent: 1, + datastore: 'ioredis', + connection, +}); export async function concurrencyService( identifier: string, func: (...args: any[]) => Promise ): Promise { - const key = `throttle:${identifier.split('-')[0]}`; - const expirationSeconds = 180; - - while (true) { - const setLock = await ioRedis.set( - key, - 'locked', - 'EX', - expirationSeconds, - 'NX' - ); - - if (setLock) { - break; - } - - // Wait before trying again - await timer(1000); - } - let load: T; try { - load = await func(); + load = await bottleneck + .key(identifier.split('-')[0]) + .schedule({ expiration: 60_000 }, async () => { + return await func(); + }); } catch (err) {} - await timer(2000); - await ioRedis.del(key); return load; } diff --git a/package.json b/package.json index 359982df..37bf62bf 100644 --- a/package.json +++ b/package.json @@ -127,6 +127,7 @@ "array-move": "^4.0.0", "axios": "^1.7.7", "bcrypt": "^5.1.1", + "bottleneck": "^2.19.5", "bs58": "^6.0.0", "bufferutil": "^4.0.8", "bullmq": "^5.12.12", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 213dc49b..3912539f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -261,6 +261,9 @@ importers: bcrypt: specifier: ^5.1.1 version: 5.1.1 + bottleneck: + specifier: ^2.19.5 + version: 2.19.5 bs58: specifier: ^6.0.0 version: 6.0.0 @@ -7518,6 +7521,9 @@ packages: borsh@0.7.0: resolution: {integrity: sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==} + bottleneck@2.19.5: + resolution: {integrity: sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==} + bowser@2.11.0: resolution: {integrity: sha512-AlcaJBi/pqqJBIQ8U9Mcpc9i8Aqxn88Skv5d+xBX006BY5u8N3mGLHa5Lgppa7L/HfwgwLgZ6NYs+Ag6uUmJRA==} @@ -24715,6 +24721,8 @@ snapshots: bs58: 4.0.1 text-encoding-utf-8: 1.0.2 + bottleneck@2.19.5: {} + bowser@2.11.0: {} brace-expansion@1.1.12: