Skip to content

Commit

Permalink
limit api request concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Jan 10, 2025
1 parent 9f2c6a5 commit ae93873
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 12 deletions.
168 changes: 157 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"next-pwa": "^5.6.0",
"node-forge": "^1.3.1",
"normalize.css": "^8.0.1",
"p-limit": "^6.2.0",
"p-memoize": "^7.1.1",
"p-throttle": "^7.0.0",
"p-timeout": "^6.1.4",
Expand Down
4 changes: 3 additions & 1 deletion src/lib/api/fetchApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pLimit from 'p-limit';
import pMemoize from 'p-memoize';
import pThrottle from 'p-throttle';
import pTimeout from 'p-timeout';
Expand All @@ -10,6 +11,7 @@ const API_URL =
'https://graphql-staging.audioverse.org/graphql';

const throttle = pThrottle({ limit: 10, interval: 1000 });
const limit = pLimit(1);

const getResponse = throttle(
async (headers: HeadersInit, query: string, variables: unknown) => {
Expand Down Expand Up @@ -42,7 +44,7 @@ const fetchJson = pMemoize(
query: string;
variables: unknown;
}) => {
const res = await getResponse(headers, query, variables);
const res = await limit(() => getResponse(headers, query, variables));

if (!res.ok) {
console.error({ text: await res.text(), res, query, variables, headers });
Expand Down

0 comments on commit ae93873

Please sign in to comment.