Open david8z opened 1 month ago
Ended up implementing this but unsure if there is a better way:
export const rateLimit = async (domain: string, maxRetries = 3) => {
for (let i = 0; i < maxRetries; i++) {
try {
const { success, pending, reset } =
await ratelimit.handinger.blockUntilReady("api", 180_000);
const msToReset = Math.max(0, reset - Date.now());
const { success: successDomain, pending: pendingDomain } =
await ratelimit.handingerDomain.blockUntilReady(domain, msToReset);
waitUntil(Promise.all([pending, pendingDomain]));
if (success && successDomain) {
return;
}
throw new Error("Handinger Ratelimit exceeded");
} catch (error) {
if (i === maxRetries - 1) throw error;
console.warn(
`Rate limit retry ${i + 1}/${maxRetries} failed. Retrying...`,
);
await new Promise((resolve) => setTimeout(resolve, 10000));
}
}
throw new Error("Handinger Ratelimit exceeded");
};
Hey @david8z, what is it that you're trying to achieve here? If you could provide more details about the logic/purpose, I could come up with something.
Hey @fahreddinozcan basically the idea is finding an efficient way of combining two limits.
What is the optimal way to combine two rate limits together?
We could define:
And then await both:
But this doesn't assures that once resolved none are blocked as one could resolve much faster than the other one.