Skip to content

track ratelimit only on creation #5950

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

Merged
merged 7 commits into from
Jan 15, 2025
Merged
Changes from 3 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
31 changes: 17 additions & 14 deletions packages/service-utils/src/core/rateLimit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,25 @@ export async function rateLimit(args: {
limitPerSecond * sampleRate * RATE_LIMIT_WINDOW_SECONDS;

if (requestCount > limitPerWindow) {
// Report rate limit hits.
if (project?.id) {
await updateRateLimitedAt(project.id, serviceConfig);
/**
* Report rate limit hits.
* Only track rate limit when its hit for the first time.
* Not waiting for tracking to complete as user doesn't need to wait.
*/
if (requestCount <= limitPerWindow + 1 && project?.id) {
updateRateLimitedAt(project.id, serviceConfig).catch(() => {
// no-op
});
}

// Reject requests when they've exceeded 2x the rate limit.
if (requestCount > 2 * limitPerWindow) {
return {
rateLimited: true,
requestCount,
rateLimit: limitPerWindow,
status: 429,
errorMessage: `You've exceeded your ${serviceScope} rate limit at ${limitPerSecond} reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.`,
errorCode: "RATE_LIMIT_EXCEEDED",
};
}
return {
rateLimited: true,
requestCount,
rateLimit: limitPerWindow,
status: 429,
errorMessage: `You've exceeded your ${serviceScope} rate limit at ${limitPerSecond} reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.`,
errorCode: "RATE_LIMIT_EXCEEDED",
};
}

return {
Expand Down
Loading