Skip to content

Commit 3ab31c8

Browse files
authored
Fix incrBy to incrby in rate limit implementation (#6704)
1 parent 8eb5d8c commit 3ab31c8

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

.changeset/giant-humans-say.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": patch
3+
---
4+
5+
fix incrby

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const RATE_LIMIT_WINDOW_SECONDS = 10;
77
type IRedis = {
88
get: (key: string) => Promise<string | null>;
99
expire(key: string, seconds: number): Promise<number>;
10-
incrBy(key: string, value: number): Promise<number>;
10+
incrby(key: string, value: number): Promise<number>;
1111
};
1212

1313
export async function rateLimit(args: {
@@ -83,7 +83,7 @@ export async function rateLimit(args: {
8383
// do not await this, it just needs to execute at all
8484
(async () =>
8585
// always incrementBy the amount specified for the key
86-
await redis.incrBy(key, increment).then(async () => {
86+
await redis.incrby(key, increment).then(async () => {
8787
// if the initial request count was 0, set the key to expire in the future
8888
if (requestCount === 0) {
8989
await redis.expire(key, RATE_LIMIT_WINDOW_SECONDS);

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { rateLimit } from "./index.js";
55
const mockRedis = {
66
get: vi.fn(),
77
expire: vi.fn(),
8-
incrBy: vi.fn(),
8+
incrby: vi.fn(),
99
};
1010

1111
describe("rateLimit", () => {
@@ -14,7 +14,7 @@ describe("rateLimit", () => {
1414
vi.clearAllMocks();
1515
mockRedis.get.mockReset();
1616
mockRedis.expire.mockReset();
17-
mockRedis.incrBy.mockReset();
17+
mockRedis.incrby.mockReset();
1818
});
1919

2020
afterEach(() => {
@@ -52,7 +52,7 @@ describe("rateLimit", () => {
5252
rateLimit: 50,
5353
});
5454

55-
expect(mockRedis.incrBy).toHaveBeenCalledTimes(1);
55+
expect(mockRedis.incrby).toHaveBeenCalledTimes(1);
5656
});
5757

5858
it("should rate limit if exceeded hard limit", async () => {
@@ -74,7 +74,7 @@ describe("rateLimit", () => {
7474
errorCode: "RATE_LIMIT_EXCEEDED",
7575
});
7676

77-
expect(mockRedis.incrBy).not.toHaveBeenCalled();
77+
expect(mockRedis.incrby).not.toHaveBeenCalled();
7878
});
7979

8080
it("expires on the first incr request only", async () => {
@@ -92,7 +92,7 @@ describe("rateLimit", () => {
9292
requestCount: 2,
9393
rateLimit: 50,
9494
});
95-
expect(mockRedis.incrBy).toHaveBeenCalled();
95+
expect(mockRedis.incrby).toHaveBeenCalled();
9696
});
9797

9898
it("enforces rate limit if sampled (hit)", async () => {
@@ -169,7 +169,7 @@ describe("rateLimit", () => {
169169
requestCount: 1,
170170
rateLimit: 50,
171171
});
172-
expect(mockRedis.incrBy).toHaveBeenCalledWith(expect.any(String), 1);
172+
expect(mockRedis.incrby).toHaveBeenCalledWith(expect.any(String), 1);
173173
});
174174

175175
it("should handle null response from redis", async () => {
@@ -216,7 +216,7 @@ describe("rateLimit", () => {
216216
mockRedis.get.mockResolvedValue("0");
217217

218218
// Mock redis.set to have 100ms delay
219-
mockRedis.incrBy.mockImplementation(
219+
mockRedis.incrby.mockImplementation(
220220
() =>
221221
new Promise((resolve) => {
222222
setTimeout(() => resolve(1), 100);
@@ -256,13 +256,13 @@ describe("rateLimit", () => {
256256
}
257257

258258
// Redis set should be called 3 times
259-
expect(mockRedis.incrBy).toHaveBeenCalledTimes(3);
259+
expect(mockRedis.incrby).toHaveBeenCalledTimes(3);
260260
});
261261

262262
it("should handle custom increment values", async () => {
263263
// Mock initial state
264264
mockRedis.get.mockResolvedValue("5");
265-
mockRedis.incrBy.mockResolvedValue(10);
265+
mockRedis.incrby.mockResolvedValue(10);
266266

267267
const result = await rateLimit({
268268
team: validTeamResponse,
@@ -279,7 +279,7 @@ describe("rateLimit", () => {
279279
});
280280

281281
// Verify redis was called with correct increment
282-
expect(mockRedis.incrBy).toHaveBeenCalledWith(
282+
expect(mockRedis.incrby).toHaveBeenCalledWith(
283283
expect.stringContaining("rate-limit"),
284284
5,
285285
);

0 commit comments

Comments
 (0)