Skip to content

Commit c290997

Browse files
authored
track ratelimit only on creation (#5950)
1 parent 81b146a commit c290997

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

.changeset/green-tips-beam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@thirdweb-dev/service-utils": minor
3+
---
4+
5+
track usage call once per ratelimit window

packages/service-utils/src/core/rateLimit/index.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,25 @@ export async function rateLimit(args: {
7171
limitPerSecond * sampleRate * RATE_LIMIT_WINDOW_SECONDS;
7272

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

79-
// Reject requests when they've exceeded 2x the rate limit.
80-
if (requestCount > 2 * limitPerWindow) {
81-
return {
82-
rateLimited: true,
83-
requestCount,
84-
rateLimit: limitPerWindow,
85-
status: 429,
86-
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.`,
87-
errorCode: "RATE_LIMIT_EXCEEDED",
88-
};
89-
}
85+
return {
86+
rateLimited: true,
87+
requestCount,
88+
rateLimit: limitPerWindow,
89+
status: 429,
90+
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.`,
91+
errorCode: "RATE_LIMIT_EXCEEDED",
92+
};
9093
}
9194

9295
return {

packages/service-utils/src/core/rateLimit/rateLimit.test.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const mockRedis = {
1010

1111
// Mocking the updateRateLimitedAt function
1212
vi.mock("../../../src/core/api", () => ({
13-
updateRateLimitedAt: vi.fn(),
13+
updateRateLimitedAt: vi.fn().mockResolvedValue({}),
1414
}));
1515

1616
describe("rateLimit", () => {
@@ -59,27 +59,8 @@ describe("rateLimit", () => {
5959
expect(mockRedis.expire).not.toHaveBeenCalled();
6060
});
6161

62-
it("should report rate limit if exceeded but not block", async () => {
63-
mockRedis.incr.mockResolvedValue(51); // Current count is 51 requests in 10 seconds.
64-
65-
const result = await rateLimit({
66-
project: validProjectResponse,
67-
limitPerSecond: 5,
68-
serviceConfig: validServiceConfig,
69-
redis: mockRedis,
70-
});
71-
72-
expect(result).toEqual({
73-
rateLimited: false,
74-
requestCount: 51,
75-
rateLimit: 50,
76-
});
77-
expect(updateRateLimitedAt).toHaveBeenCalled();
78-
expect(mockRedis.expire).not.toHaveBeenCalled();
79-
});
80-
8162
it("should rate limit if exceeded hard limit", async () => {
82-
mockRedis.incr.mockResolvedValue(101);
63+
mockRedis.incr.mockResolvedValue(51);
8364

8465
const result = await rateLimit({
8566
project: validProjectResponse,
@@ -90,7 +71,7 @@ describe("rateLimit", () => {
9071

9172
expect(result).toEqual({
9273
rateLimited: true,
93-
requestCount: 101,
74+
requestCount: 51,
9475
rateLimit: 50,
9576
status: 429,
9677
errorMessage: `You've exceeded your storage rate limit at 5 reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.`,
@@ -131,9 +112,13 @@ describe("rateLimit", () => {
131112
});
132113

133114
expect(result).toEqual({
134-
rateLimited: false,
115+
rateLimited: true,
135116
requestCount: 10,
136117
rateLimit: 5,
118+
status: 429,
119+
errorMessage:
120+
"You've exceeded your storage rate limit at 5 reqs/sec. To get higher rate limits, contact us at https://thirdweb.com/contact-us.",
121+
errorCode: "RATE_LIMIT_EXCEEDED",
137122
});
138123
});
139124

0 commit comments

Comments
 (0)