@@ -16,13 +16,18 @@ export default class UploadPlugin extends AdminForthPlugin {
1616
1717 resourceConfig : AdminForthResource ;
1818
19+ rateLimiter : RateLimiter ;
20+
1921 constructor ( options : PluginOptions ) {
2022 super ( options , import . meta. url ) ;
2123 this . options = options ;
2224
2325 // for calcualting average time
2426 this . totalCalls = 0 ;
2527 this . totalDuration = 0 ;
28+ if ( this . options . generation ?. rateLimit ?. limit ) {
29+ this . rateLimiter = new RateLimiter ( this . options . generation . rateLimit ?. limit )
30+ }
2631 }
2732
2833 instanceUniqueRepresentation ( pluginOptions : any ) : string {
@@ -344,15 +349,14 @@ export default class UploadPlugin extends AdminForthPlugin {
344349 path : `/plugin/${ this . pluginInstanceId } /generate_images` ,
345350 handler : async ( { body, adminUser, headers } ) => {
346351 const { prompt, recordId } = body ;
347- if ( this . options . generation . rateLimit ?. limit ) {
352+ if ( this . rateLimiter ) {
348353 // rate limit
349354 // const { error } = RateLimiter.checkRateLimit(
350355 // this.pluginInstanceId,
351356 // this.options.generation.rateLimit?.limit,
352357 // this.adminforth.auth.getClientIp(headers),
353358 // );
354- const rateLimiter = new RateLimiter ( this . options . generation . rateLimit ?. limit ) ;
355- if ( ! rateLimiter . consume ( `${ this . pluginInstanceId } -${ this . adminforth . auth . getClientIp ( headers ) } ` ) ) {
359+ if ( ! await this . rateLimiter . consume ( `${ this . pluginInstanceId } -${ this . adminforth . auth . getClientIp ( headers ) } ` ) ) {
356360 return { error : this . options . generation . rateLimit . errorMessage } ;
357361 }
358362 }
0 commit comments