From 9b1308b2e63c81cfd15e8e95ce540d400ec2f324 Mon Sep 17 00:00:00 2001 From: Nevo David Date: Mon, 7 Jul 2025 17:20:44 +0700 Subject: [PATCH] feat: better rate limiting for instagram --- .../src/integrations/social.abstract.ts | 14 ++++++++++++-- .../src/integrations/social/instagram.provider.ts | 4 ++-- package.json | 1 + pnpm-lock.yaml | 9 +++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/libraries/nestjs-libraries/src/integrations/social.abstract.ts b/libraries/nestjs-libraries/src/integrations/social.abstract.ts index 8761446f..e3fe7ba0 100644 --- a/libraries/nestjs-libraries/src/integrations/social.abstract.ts +++ b/libraries/nestjs-libraries/src/integrations/social.abstract.ts @@ -1,4 +1,5 @@ import { timer } from '@gitroom/helpers/utils/timer'; +import pThrottle from 'p-throttle'; export class RefreshToken { constructor( @@ -19,15 +20,24 @@ export class NotEnoughScopes { constructor(public message = 'Not enough scopes') {} } +const pThrottleInstance = pThrottle({ + limit: 1, + interval: 2000 +}); + export abstract class SocialAbstract { + private fetchInstance = pThrottleInstance( + (url: RequestInfo, options?: RequestInit) => fetch(url, options) + ); + async fetch( url: string, options: RequestInit = {}, identifier = '', totalRetries = 0 ): Promise { - const request = await fetch(url, options); -`` + const request = await this.fetchInstance(url, options); + if (request.status === 200 || request.status === 201) { return request; } diff --git a/libraries/nestjs-libraries/src/integrations/social/instagram.provider.ts b/libraries/nestjs-libraries/src/integrations/social/instagram.provider.ts index f43536c3..98456714 100644 --- a/libraries/nestjs-libraries/src/integrations/social/instagram.provider.ts +++ b/libraries/nestjs-libraries/src/integrations/social/instagram.provider.ts @@ -260,7 +260,7 @@ export class InstagramProvider `https://${type}/v20.0/${photoId}?access_token=${accessToken}&fields=status_code` ) ).json(); - await timer(3000); + await timer(10000); status = status_code; } console.log('in progress3'); @@ -320,7 +320,7 @@ export class InstagramProvider `https://${type}/v20.0/${containerId}?fields=status_code&access_token=${accessToken}` ) ).json(); - await timer(3000); + await timer(10000); status = status_code; } diff --git a/package.json b/package.json index 8944898d..254d20c3 100644 --- a/package.json +++ b/package.json @@ -171,6 +171,7 @@ "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 26e43dac..30e081bc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -390,6 +390,9 @@ 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 version: 2.25.1(@types/react@18.3.1)(@types/sortablejs@1.15.8)(react-dom@18.3.1(react@18.3.1))(react-native@0.80.1(@babel/core@7.28.0)(@types/react@18.3.1)(bufferutil@4.0.9)(react@18.3.1)(utf-8-validate@5.0.10))(react@18.3.1) @@ -12367,6 +12370,10 @@ packages: resolution: {integrity: sha512-hEt02O4hUct5wtwg4H4KcWgDdm+l1bOaEy/hWzd8xtXB9BqxTWBBhb+2ImAtH4Cv4rPjV76xN3Zumqk3k3AhhQ==} engines: {node: '>=16.17'} + p-throttle@4.1.1: + resolution: {integrity: sha512-TuU8Ato+pRTPJoDzYD4s7ocJYcNSEZRvlxoq3hcPI2kZDZ49IQ1Wkj7/gDJc3X7XiEAAvRGtDzdXJI0tC3IL1g==} + engines: {node: '>=10'} + p-timeout@3.2.0: resolution: {integrity: sha512-rhIwUycgwwKcP9yTOOFK/AKsAopjjCakVqLHePO3CC6Mir1Z99xT+R63jZxAT5lFZLa2inS5h+ZS2GvR99/FBg==} engines: {node: '>=8'} @@ -31944,6 +31951,8 @@ snapshots: is-network-error: 1.1.0 retry: 0.13.1 + p-throttle@4.1.1: {} + p-timeout@3.2.0: dependencies: p-finally: 1.0.0