Skip to content

Commit 1c5bd33

Browse files
fix: rate-limiter
1 parent 6eb4a72 commit 1c5bd33

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ export default class TextCompletePlugin extends AdminForthPlugin {
1212

1313
adminforth!: IAdminForth;
1414

15+
rateLimiter?: RateLimiter;
16+
1517
constructor(options: PluginOptions) {
1618
super(options, import.meta.url);
1719
this.options = options;
20+
if (options.rateLimit?.limit) {
21+
this.rateLimiter = new RateLimiter(options.rateLimit?.limit);
22+
}
1823
}
1924

2025
instanceUniqueRepresentation(pluginOptions: any) : string {
@@ -78,15 +83,14 @@ export default class TextCompletePlugin extends AdminForthPlugin {
7883
method: 'POST',
7984
path: `/plugin/${this.pluginInstanceId}/doComplete`,
8085
handler: async ({ body, headers }) => {
81-
if (this.options.rateLimit?.limit) {
86+
if (this.rateLimiter) {
8287
// rate limit
8388
// const { error } = RateLimiter.checkRateLimit(
8489
// this.pluginInstanceId,
8590
// this.options.rateLimit?.limit,
8691
// this.adminforth.auth.getClientIp(headers),
8792
// );
88-
const rateLimiter = new RateLimiter(this.options.rateLimit?.limit);
89-
if (!rateLimiter.consume(`${this.pluginInstanceId}-${this.adminforth.auth.getClientIp(headers)}`)) {
93+
if (!await this.rateLimiter.consume(`${this.pluginInstanceId}-${this.adminforth.auth.getClientIp(headers)}`)) {
9094
return {
9195
completion: [],
9296
}

0 commit comments

Comments
 (0)