Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/giant-humans-say.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/service-utils": patch
---

fix incrby
4 changes: 2 additions & 2 deletions packages/service-utils/src/core/rateLimit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const RATE_LIMIT_WINDOW_SECONDS = 10;
type IRedis = {
get: (key: string) => Promise<string | null>;
expire(key: string, seconds: number): Promise<number>;
incrBy(key: string, value: number): Promise<number>;
incrby(key: string, value: number): Promise<number>;
};

export async function rateLimit(args: {
Expand Down Expand Up @@ -83,7 +83,7 @@ export async function rateLimit(args: {
// do not await this, it just needs to execute at all
(async () =>
// always incrementBy the amount specified for the key
await redis.incrBy(key, increment).then(async () => {
await redis.incrby(key, increment).then(async () => {
// if the initial request count was 0, set the key to expire in the future
if (requestCount === 0) {
await redis.expire(key, RATE_LIMIT_WINDOW_SECONDS);
Expand Down
20 changes: 10 additions & 10 deletions packages/service-utils/src/core/rateLimit/rateLimit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { rateLimit } from "./index.js";
const mockRedis = {
get: vi.fn(),
expire: vi.fn(),
incrBy: vi.fn(),
incrby: vi.fn(),
};

describe("rateLimit", () => {
Expand All @@ -14,7 +14,7 @@ describe("rateLimit", () => {
vi.clearAllMocks();
mockRedis.get.mockReset();
mockRedis.expire.mockReset();
mockRedis.incrBy.mockReset();
mockRedis.incrby.mockReset();
});

afterEach(() => {
Expand Down Expand Up @@ -52,7 +52,7 @@ describe("rateLimit", () => {
rateLimit: 50,
});

expect(mockRedis.incrBy).toHaveBeenCalledTimes(1);
expect(mockRedis.incrby).toHaveBeenCalledTimes(1);
});

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

expect(mockRedis.incrBy).not.toHaveBeenCalled();
expect(mockRedis.incrby).not.toHaveBeenCalled();
});

it("expires on the first incr request only", async () => {
Expand All @@ -92,7 +92,7 @@ describe("rateLimit", () => {
requestCount: 2,
rateLimit: 50,
});
expect(mockRedis.incrBy).toHaveBeenCalled();
expect(mockRedis.incrby).toHaveBeenCalled();
});

it("enforces rate limit if sampled (hit)", async () => {
Expand Down Expand Up @@ -169,7 +169,7 @@ describe("rateLimit", () => {
requestCount: 1,
rateLimit: 50,
});
expect(mockRedis.incrBy).toHaveBeenCalledWith(expect.any(String), 1);
expect(mockRedis.incrby).toHaveBeenCalledWith(expect.any(String), 1);
});

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

// Mock redis.set to have 100ms delay
mockRedis.incrBy.mockImplementation(
mockRedis.incrby.mockImplementation(
() =>
new Promise((resolve) => {
setTimeout(() => resolve(1), 100);
Expand Down Expand Up @@ -256,13 +256,13 @@ describe("rateLimit", () => {
}

// Redis set should be called 3 times
expect(mockRedis.incrBy).toHaveBeenCalledTimes(3);
expect(mockRedis.incrby).toHaveBeenCalledTimes(3);
});

it("should handle custom increment values", async () => {
// Mock initial state
mockRedis.get.mockResolvedValue("5");
mockRedis.incrBy.mockResolvedValue(10);
mockRedis.incrby.mockResolvedValue(10);

const result = await rateLimit({
team: validTeamResponse,
Expand All @@ -279,7 +279,7 @@ describe("rateLimit", () => {
});

// Verify redis was called with correct increment
expect(mockRedis.incrBy).toHaveBeenCalledWith(
expect(mockRedis.incrby).toHaveBeenCalledWith(
expect.stringContaining("rate-limit"),
5,
);
Expand Down
Loading