Skip to content
This repository was archived by the owner on Jul 28, 2026. It is now read-only.

Commit 7bfca31

Browse files
committed
fix: add exponential backoff retry on HTTP 429 rate limit in pteroFetch
1 parent 649904b commit 7bfca31

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

services/pyrodactyl.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,26 @@ const headers = {
2323

2424
async function pteroFetch(path, options = {}) {
2525
const url = `${PTERO_URL}/api/application${path}`;
26-
const res = await fetchWithTimeout(url, {
27-
...options,
28-
headers: { ...headers, ...options.headers },
29-
});
30-
if (res.status === 204) return null;
31-
const data = await res.json();
32-
if (!res.ok) {
33-
const detail = data?.errors?.[0]?.detail || JSON.stringify(data);
34-
throw new Error(detail);
26+
const maxRetries = 3;
27+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
28+
const res = await fetchWithTimeout(url, {
29+
...options,
30+
headers: { ...headers, ...options.headers },
31+
});
32+
if (res.status === 204) return null;
33+
if (res.status === 429 && attempt < maxRetries) {
34+
const wait = Math.min(1000 * Math.pow(2, attempt - 1), 8000);
35+
console.warn(`pteroFetch rate limited (429), retry ${attempt}/${maxRetries} after ${wait}ms`);
36+
await new Promise(r => setTimeout(r, wait));
37+
continue;
38+
}
39+
const data = await res.json();
40+
if (!res.ok) {
41+
const detail = data?.errors?.[0]?.detail || JSON.stringify(data);
42+
throw new Error(detail);
43+
}
44+
return data;
3545
}
36-
return data;
3746
}
3847

3948
export async function createPteroUser({ email, username, firstName, lastName, password }) {

0 commit comments

Comments
 (0)