Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Cloudflare rate limiting #114

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ export const createZeroTrustListsOneByOne = async (items) => {
totalListNumber--;
listNumber++;
console.log(`Created "${listName}" list - ${totalListNumber} left`);
/*
Sleep for 4 seconds to work around an undocumented Cloudflare API rate limit.
This should be removed once the issue is fixed on their side.
*/
await new Promise((r) => setTimeout(r, 4 * 1000));
} catch (err) {
console.error(`Could not create "${listName}" - ${err.toString()}`);
throw err;
Expand Down Expand Up @@ -111,11 +106,6 @@ export const deleteZeroTrustListsOneByOne = async (lists) => {
await deleteZeroTrustList(id);
totalListNumber--;
console.log(`Deleted ${name} list - ${totalListNumber} left`);
/*
Sleep for 4 seconds to work around an undocumented Cloudflare API rate limit.
This should be removed once the issue is fixed on their side.
*/
await new Promise((r) => setTimeout(r, 4 * 1000));
} catch (err) {
console.error(`Could not delete ${name} - ${err.toString()}`);
throw err;
Expand Down
50 changes: 34 additions & 16 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,41 @@ const request = async (url, options) => {
"X-Auth-Key": API_KEY,
};

const response = await fetch(url, {
...options,
headers: {
...options.headers,
...headers,
},
});

const data = await response.json();

if (!response.ok) {
// Send a message to the Discord webhook if it exists
await notifyWebhook(`An HTTP error has occurred (${response.status}) while making a web request. Please check the logs for further details.`);
throw new Error(`HTTP error! Status: ${response.status} - ${ 'errors' in data ? data.errors[0].message : JSON.stringify(data) }`);
}
/*
Due to the undocumented Cloudflare API rate limit, the request will be retried until Cloudflare accepts it.
*/
const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));

let response;
let data;
let attempts = 0;

return data;
while(attempts < 50) {
try {
response = await fetch(url, {
...options,
headers: {
...options.headers,
...headers,
},
});

data = await response.json();

if (!response.ok) {
throw new Error('Response not OK');
}
return data;
} catch (error) {
attempts++;
if(attempts >= 50) {
// Send a message to the Discord webhook if it exists
await notifyWebhook(`An HTTP error has occurred (${response.status}) while making a web request. Please check the logs for further details.`);
throw new Error(`HTTP error! Status: ${response.status} - ${ 'errors' in data ? data.errors[0].message : JSON.stringify(data) }`);
}
await wait(5000);
}
}
};

/**
Expand Down