Skip to content

Commit 43ae3ab

Browse files
committed
pass team instead of project to rateLimit
1 parent 1cff849 commit 43ae3ab

File tree

3 files changed

+16
-32
lines changed

3 files changed

+16
-32
lines changed

.changeset/large-beds-move.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+
pass `team` instead of `project` to `rateLimit`

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

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
type CoreServiceConfig,
3-
type ProjectResponse,
4-
updateRateLimitedAt,
5-
} from "../api.js";
1+
import type { CoreServiceConfig, TeamResponse } from "../api.js";
62
import type { RateLimitResult } from "./types.js";
73

84
const RATE_LIMIT_WINDOW_SECONDS = 10;
@@ -14,7 +10,7 @@ type IRedis = {
1410
};
1511

1612
export async function rateLimit(args: {
17-
project?: ProjectResponse;
13+
team: TeamResponse;
1814
limitPerSecond: number;
1915
serviceConfig: CoreServiceConfig;
2016
redis: IRedis;
@@ -25,13 +21,7 @@ export async function rateLimit(args: {
2521
*/
2622
sampleRate?: number;
2723
}): Promise<RateLimitResult> {
28-
const {
29-
project,
30-
limitPerSecond,
31-
serviceConfig,
32-
redis,
33-
sampleRate = 1.0,
34-
} = args;
24+
const { team, limitPerSecond, serviceConfig, redis, sampleRate = 1.0 } = args;
3525

3626
const shouldSampleRequest = Math.random() < sampleRate;
3727
if (!shouldSampleRequest) {
@@ -57,7 +47,7 @@ export async function rateLimit(args: {
5747
const timestampWindow =
5848
Math.floor(Date.now() / (1000 * RATE_LIMIT_WINDOW_SECONDS)) *
5949
RATE_LIMIT_WINDOW_SECONDS;
60-
const key = `rate-limit:${serviceScope}:${project?.id}:${timestampWindow}`;
50+
const key = `rate-limit:${serviceScope}:${team.id}:${timestampWindow}`;
6151

6252
// Increment and get the current request count in this window.
6353
const requestCount = await redis.incr(key);
@@ -71,17 +61,6 @@ export async function rateLimit(args: {
7161
limitPerSecond * sampleRate * RATE_LIMIT_WINDOW_SECONDS;
7262

7363
if (requestCount > limitPerWindow) {
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-
});
83-
}
84-
8564
return {
8665
rateLimited: true,
8766
requestCount,

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2-
import { validProjectResponse, validServiceConfig } from "../../mocks.js";
2+
import { validServiceConfig, validTeamResponse } from "../../mocks.js";
33
import { updateRateLimitedAt } from "../api.js";
44
import { rateLimit } from "./index.js";
55

@@ -27,7 +27,7 @@ describe("rateLimit", () => {
2727

2828
it("should not rate limit if service scope is not in rate limits", async () => {
2929
const result = await rateLimit({
30-
project: validProjectResponse,
30+
team: validTeamResponse,
3131
limitPerSecond: 0,
3232
serviceConfig: validServiceConfig,
3333
redis: mockRedis,
@@ -44,7 +44,7 @@ describe("rateLimit", () => {
4444
mockRedis.incr.mockResolvedValue(50); // Current count is 50 requests in 10 seconds.
4545

4646
const result = await rateLimit({
47-
project: validProjectResponse,
47+
team: validTeamResponse,
4848
limitPerSecond: 5,
4949
serviceConfig: validServiceConfig,
5050
redis: mockRedis,
@@ -63,7 +63,7 @@ describe("rateLimit", () => {
6363
mockRedis.incr.mockResolvedValue(51);
6464

6565
const result = await rateLimit({
66-
project: validProjectResponse,
66+
team: validTeamResponse,
6767
limitPerSecond: 5,
6868
serviceConfig: validServiceConfig,
6969
redis: mockRedis,
@@ -85,7 +85,7 @@ describe("rateLimit", () => {
8585
mockRedis.incr.mockResolvedValue(1);
8686

8787
const result = await rateLimit({
88-
project: validProjectResponse,
88+
team: validTeamResponse,
8989
limitPerSecond: 5,
9090
serviceConfig: validServiceConfig,
9191
redis: mockRedis,
@@ -104,7 +104,7 @@ describe("rateLimit", () => {
104104
vi.spyOn(global.Math, "random").mockReturnValue(0.08);
105105

106106
const result = await rateLimit({
107-
project: validProjectResponse,
107+
team: validTeamResponse,
108108
limitPerSecond: 5,
109109
serviceConfig: validServiceConfig,
110110
redis: mockRedis,
@@ -127,7 +127,7 @@ describe("rateLimit", () => {
127127
vi.spyOn(global.Math, "random").mockReturnValue(0.15);
128128

129129
const result = await rateLimit({
130-
project: validProjectResponse,
130+
team: validTeamResponse,
131131
limitPerSecond: 5,
132132
serviceConfig: validServiceConfig,
133133
redis: mockRedis,

0 commit comments

Comments
 (0)