mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-06 09:47:43 +02:00
QFIX: Rate limits (#9242)
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
@@ -1083,11 +1083,23 @@ export class TSessionManager implements SessionManager {
|
||||
}
|
||||
|
||||
limitter = new SlidingWindowRateLimitter(
|
||||
parseInt(process.env.RATE_LIMIT_MAX ?? '250'),
|
||||
parseInt(process.env.RATE_LIMIT_MAX ?? '1500'),
|
||||
parseInt(process.env.RATE_LIMIT_WINDOW ?? '30000'),
|
||||
() => Date.now()
|
||||
)
|
||||
|
||||
sysLimitter = new SlidingWindowRateLimitter(
|
||||
parseInt(process.env.RATE_LIMIT_MAX ?? '5000'),
|
||||
parseInt(process.env.RATE_LIMIT_WINDOW ?? '30000'),
|
||||
() => Date.now()
|
||||
)
|
||||
|
||||
checkRate (service: Session): RateLimitInfo {
|
||||
return (service.getUser() === systemAccountUuid ? this.sysLimitter : this.limitter).checkRateLimit(
|
||||
service.getUser() + (service.token.extra?.service ?? '')
|
||||
)
|
||||
}
|
||||
|
||||
async handleRequest<S extends Session>(
|
||||
requestCtx: MeasureContext,
|
||||
service: S,
|
||||
@@ -1099,7 +1111,7 @@ export class TSessionManager implements SessionManager {
|
||||
source: service.token.extra?.service ?? '🤦♂️user',
|
||||
mode: '🧭 handleRequest'
|
||||
})
|
||||
const rateLimit = this.limitter.checkRateLimit(service.getUser() + (service.token.extra?.service ?? ''))
|
||||
const rateLimit = this.checkRate(service)
|
||||
// If remaining is 0, rate limit is exceeded
|
||||
if (rateLimit?.remaining === 0) {
|
||||
void ws.send(
|
||||
@@ -1213,7 +1225,7 @@ export class TSessionManager implements SessionManager {
|
||||
ws: ConnectionSocket,
|
||||
operation: (ctx: ClientSessionCtx, rateLimit: RateLimitInfo | undefined) => Promise<void>
|
||||
): Promise<RateLimitInfo | undefined> {
|
||||
const rateLimitStatus = this.limitter.checkRateLimit(service.getUser() + (service.token.extra?.service ?? ''))
|
||||
const rateLimitStatus = this.checkRate(service)
|
||||
// If remaining is 0, rate limit is exceeded
|
||||
if (rateLimitStatus?.remaining === 0) {
|
||||
return await Promise.resolve(rateLimitStatus)
|
||||
|
||||
Reference in New Issue
Block a user