feat: better concurrency with reddit
This commit is contained in:
parent
490bd74aec
commit
d5a53d8041
|
|
@ -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<T>(
|
||||
identifier: string,
|
||||
func: (...args: any[]) => Promise<T>
|
||||
): Promise<T> {
|
||||
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<T>({ expiration: 60_000 }, async () => {
|
||||
return await func();
|
||||
});
|
||||
} catch (err) {}
|
||||
await timer(2000);
|
||||
await ioRedis.del(key);
|
||||
|
||||
return load;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Reference in New Issue