fix: ThrottlerStorage v6 interface
All checks were successful
Deploy API Server / build-and-deploy (push) Successful in 38s

This commit is contained in:
WangDL 2026-05-22 23:09:37 +08:00
parent 28d68675b1
commit 4d977d2a85

View File

@ -1,13 +1,22 @@
import { Injectable } from '@nestjs/common';
import { ThrottlerStorage } from '@nestjs/throttler';
import { ThrottlerStorageRecord } from '@nestjs/throttler/dist/throttler-storage-record.interface';
import { RedisService } from '../../infrastructure/redis/redis.service';
@Injectable()
export class RedisThrottlerStorage implements ThrottlerStorage {
constructor(private readonly redis: RedisService) {}
async increment(key: string, ttl: number) {
const redisKey = `throttle:${key}`;
try { const hits = await this.redis.incr(redisKey); await this.redis.expire(redisKey, Math.ceil(ttl / 1000)); return { totalHits: hits, timeToExpire: ttl }; }
catch { return { totalHits: 1, timeToExpire: ttl }; }
async increment(key: string, ttl: number, limit: number, blockDuration: number, throttlerName: string): Promise<ThrottlerStorageRecord> {
const redisKey = `throttle:${throttlerName}:${key}`;
try {
const hits = await this.redis.incr(redisKey);
await this.redis.expire(redisKey, Math.ceil(ttl / 1000));
const isBlocked = hits > limit;
const timeToBlockExpire = isBlocked ? blockDuration : 0;
return { totalHits: hits, timeToExpire: ttl, isBlocked, timeToBlockExpire };
} catch {
return { totalHits: 1, timeToExpire: ttl, isBlocked: false, timeToBlockExpire: 0 };
}
}
}