1- import {
2- type CoreServiceConfig ,
3- type ProjectResponse ,
4- updateRateLimitedAt ,
5- } from "../api.js" ;
1+ import type { CoreServiceConfig , TeamResponse } from "../api.js" ;
62import type { RateLimitResult } from "./types.js" ;
73
84const RATE_LIMIT_WINDOW_SECONDS = 10 ;
@@ -14,7 +10,7 @@ type IRedis = {
1410} ;
1511
1612export 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