Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: move throttle to reddit, so it can be used in multiple servers

Nevo David6785a9ec6dd2
diff --git a/apps/workers/src/main.ts b/apps/workers/src/main.ts
index 42289c15b..f93b7e4eb 100644
--- a/apps/workers/src/main.ts
+++ b/apps/workers/src/main.ts
@@ -5,6 +5,8 @@ import { MicroserviceOptions } from '@nestjs/microservices';
import { BullMqServer } from '@gitroom/nestjs-libraries/bull-mq-transport-new/strategy';
async function bootstrap() {
+ process.env.IS_WORKER = 'true';
+
// some comment again
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule,
diff --git a/libraries/helpers/src/utils/concurrency.service.ts b/libraries/helpers/src/utils/concurrency.service.ts
new file mode 100644
index 000000000..1e57ea577
--- /dev/null
+++ b/libraries/helpers/src/utils/concurrency.service.ts
@@ -0,0 +1,36 @@
+import { ioRedis } from '@gitroom/nestjs-libraries/redis/redis.service';
+import { timer } from '@gitroom/helpers/utils/timer';
+
+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();
+ } catch (err) {}
+ await timer(2000);
+ await ioRedis.del(key);
+
+ return load;
+}
diff --git a/libraries/nestjs-libraries/src/integrations/integration.manager.ts b/libraries/nestjs-libraries/src/integrations/integration.manager.ts
index 95f257af2..29d18f0c1 100644
--- a/libraries/nestjs-libraries/src/integrations/integration.manager.ts
+++ b/libraries/nestjs-libraries/src/integrations/integration.manager.ts
@@ -1,6 +1,10 @@
import 'reflect-metadata';
-import { Injectable } from '@nestjs/common';
+import {
+ Injectable,
+ OnModuleInit,
+ OnModuleDestroy,
+} from '@nestjs/common';
import { XProvider } from '@gitroom/nestjs-libraries/integrations/social/x.provider';
import { SocialProvider } from '@gitroom/nestjs-libraries/integrations/social/social.integrations.interface';
import { LinkedinProvider } from '@gitroom/nestjs-libraries/integrations/social/linkedin.provider';
diff --git a/libraries/nestjs-libraries/src/integrations/social.abstract.ts b/libraries/nestjs-libraries/src/integrations/social.abstract.ts
index 250197d69..3d4e98020 100644
--- a/libraries/nestjs-libraries/src/integrations/social.abstract.ts
+++ b/libraries/nestjs-libraries/src/integrations/social.abstract.ts
@@ -1,5 +1,5 @@
import { timer } from '@gitroom/helpers/utils/timer';
-import pThrottle from 'p-throttle';
+import { concurrencyService } from '@gitroom/helpers/utils/concurrency.service';
export class RefreshToken {
constructor(
@@ -22,15 +22,8 @@ export class NotEnoughScopes {
constructor(public message = 'Not enough scopes') {}
}
-const pThrottleInstance = pThrottle({
- limit: 1,
- interval: 5000,
-});
-
export abstract class SocialAbstract {
- private fetchInstance = pThrottleInstance(
- (url: RequestInfo, options?: RequestInit) => fetch(url, options)
- );
+ abstract identifier: string;
public handleErrors(
body: string
@@ -44,7 +37,7 @@ export abstract class SocialAbstract {
identifier = '',
totalRetries = 0
): Promise<Response> {
- const request = await this.fetchInstance(url, options);
+ const request = await concurrencyService(this.identifier.split('-')[0], () => fetch(url, options));
if (request.status === 200 || request.status === 201) {
return request;
diff --git a/package.json b/package.json
index afe74c910..0d9f36842 100644
--- a/package.json
+++ b/package.json
@@ -172,7 +172,6 @@
"nx": "19.7.2",
"openai": "^4.47.1",
"p-limit": "^3.1.0",
- "p-throttle": "4.1.1",
"polotno": "^2.10.5",
"posthog-js": "^1.178.0",
"react": "18.3.1",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 559da6f8a..5b6bafb73 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -390,9 +390,6 @@ importers:
p-limit:
specifier: ^3.1.0
version: 3.1.0
- p-throttle:
- specifier: 4.1.1
- version: 4.1.1
polotno:
specifier: ^2.10.5
@@ -12031,6 +12028,7 @@ packages:
resolution: {integrity: sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==}
engines: {node: '>=10.5.0'}
+ deprecated: Use your platform's native DOMException instead
resolution: {integrity: sha512-wo2DpQkQp7Sjm2A0cq+sN7EHKO6Sl0ctXeBdFZrL9T9+UywORbufTcTZxom8YqpLQt/FqNMUkOpkZrJVYSKD3A==}
@@ -12401,10 +12399,6 @@ packages:
resolution: {integrity: sha512-hEt02O4hUct5wtwg4H4KcWgDdm+l1bOaEy/hWzd8xtXB9BqxTWBBhb+2ImAtH4Cv4rPjV76xN3Zumqk3k3AhhQ==}
engines: {node: '>=16.17'}
- resolution: {integrity: sha512-TuU8Ato+pRTPJoDzYD4s7ocJYcNSEZRvlxoq3hcPI2kZDZ49IQ1Wkj7/gDJc3X7XiEAAvRGtDzdXJI0tC3IL1g==}
- engines: {node: '>=10'}
-
resolution: {integrity: sha512-rhIwUycgwwKcP9yTOOFK/AKsAopjjCakVqLHePO3CC6Mir1Z99xT+R63jZxAT5lFZLa2inS5h+ZS2GvR99/FBg==}
engines: {node: '>=8'}
@@ -20055,21 +20049,6 @@ snapshots:
- typescript
- verdaccio
- dependencies:
- transitivePeerDependencies:
- - '@babel/traverse'
- - '@swc-node/register'
- - '@swc/core'
- - '@swc/wasm'
- - '@types/node'
- - debug
- - nx
- - supports-color
- - typescript
- - verdaccio
-
dependencies:
@@ -20416,7 +20395,7 @@ snapshots:
'@babel/preset-env': 7.28.0(@babel/[email protected])
'@babel/preset-typescript': 7.27.1(@babel/[email protected])
'@babel/runtime': 7.27.6
babel-plugin-const-enum: 1.2.0(@babel/[email protected])
@@ -32060,8 +32039,6 @@ snapshots:
is-network-error: 1.1.0
retry: 0.13.1
-
dependencies:
p-finally: 1.0.0