Skip to content

Commit 8c69131

Browse files
fix: rate-limiter fixed
1 parent b5615ac commit 8c69131

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
1313
uploadPlugin: AdminForthPlugin;
1414
totalCalls: number;
1515
totalDuration: number;
16+
rateLimiters: Record<string, RateLimiter> = {};
1617

1718
constructor(options: PluginOptions) {
1819
super(options, import.meta.url);
@@ -54,16 +55,18 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
5455
return this.compileTemplates(this.options.generateImages, record, v => String(v.prompt));
5556
}
5657

57-
private checkRateLimit(field: string,fieldNameRateLimit: string | undefined, headers: Record<string, string | string[] | undefined>): { error?: string } | void {
58+
private checkRateLimit(field: string, fieldNameRateLimit: string | undefined, headers: Record<string, string | string[] | undefined>): { error?: string } | void {
5859
if (fieldNameRateLimit) {
5960
// rate limit
6061
// const { error } = RateLimiter.checkRateLimit(
6162
// field,
6263
// fieldNameRateLimit,
6364
// this.adminforth.auth.getClientIp(headers),
6465
// );
65-
const rateLimiter = new RateLimiter(fieldNameRateLimit);
66-
if (!rateLimiter.consume(`${field}-${this.adminforth.auth.getClientIp(headers)}`)) {
66+
if (!this.rateLimiters[field]) {
67+
this.rateLimiters[field] = new RateLimiter(fieldNameRateLimit);
68+
}
69+
if (!await this.rateLimiters[field].consume(`${field}-${this.adminforth.auth.getClientIp(headers)}`)) {
6770
return { error: "Rate limit exceeded" };
6871
}
6972
}

0 commit comments

Comments
 (0)