Skip to content

Commit ab20b5f

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

File tree

2 files changed

+9
-25
lines changed

2 files changed

+9
-25
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,

0 commit comments

Comments
 (0)