-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rate limiter for postUserMessageWithPubsub() (#2449)
* Rate limiter for postUserMessageWithPubsub() * No relative import * Return 1 in case of failure + add monitoring * Adjust rate limit error message * Remove remaining distribution and add exeeded count
- Loading branch information
Showing
3 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import StatsD from "hot-shots"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
import { redisClient } from "@app/lib/redis"; | ||
import logger from "@app/logger/logger"; | ||
|
||
export const statsDClient = new StatsD(); | ||
export async function rateLimiter( | ||
key: string, | ||
maxPerTimeframe: number, | ||
timeframeSeconds: number | ||
): Promise<number> { | ||
let redis: undefined | Awaited<ReturnType<typeof redisClient>> = undefined; | ||
const now = new Date(); | ||
const tags = [`rate_limiter:${key}`]; | ||
try { | ||
redis = await redisClient(); | ||
const redisKey = `rate_limiter:${key}`; | ||
|
||
const zcountRes = await redis.zCount( | ||
redisKey, | ||
new Date().getTime() - timeframeSeconds * 1000, | ||
"+inf" | ||
); | ||
const remaining = maxPerTimeframe - zcountRes; | ||
if (remaining > 0) { | ||
await redis.zAdd(redisKey, { | ||
score: new Date().getTime(), | ||
value: uuidv4(), | ||
}); | ||
await redis.expire(redisKey, timeframeSeconds * 2); | ||
} else { | ||
statsDClient.increment("ratelimiter.exceeded.count", 1, tags); | ||
} | ||
const totalTimeMs = new Date().getTime() - now.getTime(); | ||
|
||
statsDClient.distribution( | ||
"ratelimiter.latency.distribution", | ||
totalTimeMs, | ||
tags | ||
); | ||
|
||
return remaining; | ||
} catch (e) { | ||
statsDClient.increment("ratelimiter.error.count", 1, tags); | ||
logger.error( | ||
{ | ||
key, | ||
maxPerTimeframe, | ||
timeframeSeconds, | ||
error: e, | ||
}, | ||
`RateLimiter error` | ||
); | ||
|
||
// in case of error on our side, we allow the request. | ||
return 1; | ||
} finally { | ||
if (redis) { | ||
await redis.quit(); | ||
} | ||
} | ||
} |