fix: ThrottlerStorage v6 interface again
All checks were successful
Deploy API Server / build-and-deploy (push) Successful in 37s
All checks were successful
Deploy API Server / build-and-deploy (push) Successful in 37s
This commit is contained in:
parent
178225f212
commit
c7052ee48e
@ -1,19 +1,20 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { ThrottlerStorage } from '@nestjs/throttler';
|
import { ThrottlerStorage } from '@nestjs/throttler';
|
||||||
|
import { ThrottlerStorageRecord } from '@nestjs/throttler/dist/throttler-storage-record.interface';
|
||||||
import { RedisService } from '../../infrastructure/redis/redis.service';
|
import { RedisService } from '../../infrastructure/redis/redis.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RedisThrottlerStorage implements ThrottlerStorage {
|
export class RedisThrottlerStorage implements ThrottlerStorage {
|
||||||
constructor(private readonly redis: RedisService) {}
|
constructor(private readonly redis: RedisService) {}
|
||||||
|
|
||||||
async increment(key: string, ttl: number): Promise<{ totalHits: number; timeToExpire: number }> {
|
async increment(key: string, ttl: number, limit: number, blockDuration: number, throttlerName: string): Promise<ThrottlerStorageRecord> {
|
||||||
const redisKey = `throttle:${key}`;
|
const redisKey = `throttle:${throttlerName}:${key}`;
|
||||||
try {
|
try {
|
||||||
const result = await this.redis.incr(redisKey);
|
const hits = await this.redis.incr(redisKey);
|
||||||
await this.redis.expire(redisKey, Math.ceil(ttl / 1000));
|
await this.redis.expire(redisKey, Math.ceil(ttl / 1000));
|
||||||
return { totalHits: result, timeToExpire: ttl };
|
return { totalHits: hits, timeToExpire: ttl, isBlocked: hits > limit, timeToBlockExpire: hits > limit ? blockDuration : 0 };
|
||||||
} catch {
|
} catch {
|
||||||
return { totalHits: 1, timeToExpire: ttl };
|
return { totalHits: 1, timeToExpire: ttl, isBlocked: false, timeToBlockExpire: 0 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user