MCPcopy Create free account
hub / github.com/decaporg/decap-cms / requestWithBackoff

Function requestWithBackoff

packages/decap-cms-lib-util/src/API.ts:63–120  ·  view source on GitHub ↗
(
  api: API,
  req: ApiRequest,
  attempt = 1,
)

Source from the content-addressed store, hash-verified

61}
62
63export async function requestWithBackoff(
64 api: API,
65 req: ApiRequest,
66 attempt = 1,
67): Promise<Response> {
68 if (api.rateLimiter) {
69 await api.rateLimiter.acquire();
70 }
71
72 try {
73 const builtRequest = await api.buildRequest(req);
74 const requestFunction = api.requestFunction || unsentRequest.performRequest;
75 const response: Response = await requestFunction(builtRequest);
76 if (response.status === 429) {
77 // GitLab/Bitbucket too many requests
78 const text = await response.text().catch(() => 'Too many requests');
79 throw new Error(text);
80 } else if (response.status === 403) {
81 // GitHub too many requests
82 const json = await response.json().catch(() => ({ message: '' }));
83 // Only GitHub's rate-limit body has a `message`; any other 403 body must
84 // not throw here, or the catch below retries it as a fetch failure.
85 const message = String(json?.message ?? '');
86 if (message.match('API rate limit exceeded')) {
87 const now = new Date();
88 const nextWindowInSeconds = response.headers.has('X-RateLimit-Reset')
89 ? parseInt(response.headers.get('X-RateLimit-Reset')!)
90 : now.getTime() / 1000 + 60;
91
92 throw new RateLimitError(message, nextWindowInSeconds);
93 }
94 response.json = () => Promise.resolve(json);
95 }
96 return response;
97 } catch (err) {
98 if (attempt > 5 || err.message === "Can't refresh access token when using implicit auth") {
99 throw err;
100 } else {
101 if (!api.rateLimiter) {
102 const timeout = err.resetSeconds || attempt * attempt;
103 console.log(
104 `Pausing requests for ${timeout} ${
105 attempt === 1 ? 'second' : 'seconds'
106 } due to fetch failures:`,
107 err.message,
108 );
109 api.rateLimiter = asyncLock();
110 api.rateLimiter.acquire();
111 setTimeout(() => {
112 api.rateLimiter?.release();
113 api.rateLimiter = undefined;
114 console.log(`Done pausing requests`);
115 }, 1000 * timeout);
116 }
117 return requestWithBackoff(api, req, attempt + 1);
118 }
119 }
120}

Callers 7

APIClass · 0.90
requestMethod · 0.90
requestMethod · 0.90
APIClass · 0.90
requestMethod · 0.90
APIClass · 0.90
apiRequestFunction · 0.85

Calls 5

asyncLockFunction · 0.90
hasMethod · 0.80
getMethod · 0.80
resolveMethod · 0.80
buildRequestMethod · 0.45

Tested by

no test coverage detected