3030import io .github .bucket4j .distributed .proxy .AsyncProxyManager ;
3131
3232import org .springframework .cloud .gateway .server .mvc .common .MvcUtils ;
33+ import org .springframework .core .task .TaskExecutor ;
3334import org .springframework .http .HttpStatus ;
3435import org .springframework .http .HttpStatusCode ;
36+ import org .springframework .scheduling .TaskScheduler ;
3537import org .springframework .util .Assert ;
3638import org .springframework .util .StringUtils ;
3739import org .springframework .web .servlet .function .HandlerFilterFunction ;
3840import org .springframework .web .servlet .function .ServerRequest ;
3941import org .springframework .web .servlet .function .ServerResponse ;
4042
43+ import static org .springframework .boot .autoconfigure .task .TaskExecutionAutoConfiguration .APPLICATION_TASK_EXECUTOR_BEAN_NAME ;
44+
4145public abstract class Bucket4jFilterFunctions {
4246
4347 /**
@@ -66,8 +70,7 @@ public static HandlerFilterFunction<ServerResponse, ServerResponse> rateLimit(
6670 Consumer <RateLimitConfig > configConsumer ) {
6771 RateLimitConfig config = new RateLimitConfig ();
6872 configConsumer .accept (config );
69- Supplier <CompletableFuture <BucketConfiguration >> configSupplier = () -> CompletableFuture
70- .supplyAsync (() -> config .configurationBuilder .apply (config ));
73+ Supplier <BucketConfiguration > bucketConfigurationSupplier = () -> config .configurationBuilder .apply (config );
7174
7275 return (request , next ) -> {
7376 AsyncProxyManager proxyManager = MvcUtils .getApplicationContext (request ).getBean (AsyncProxyManager .class );
@@ -76,6 +79,9 @@ public static HandlerFilterFunction<ServerResponse, ServerResponse> rateLimit(
7679 // TODO: configurable empty key status code
7780 return ServerResponse .status (HttpStatus .FORBIDDEN ).build ();
7881 }
82+ TaskExecutor taskExecutor = MvcUtils .getApplicationContext (request ).getBean (APPLICATION_TASK_EXECUTOR_BEAN_NAME , TaskExecutor .class );
83+ Supplier <CompletableFuture <BucketConfiguration >> configSupplier = () -> CompletableFuture
84+ .supplyAsync (bucketConfigurationSupplier , taskExecutor );
7985 AsyncBucketProxy bucket = proxyManager .builder ().build (key , configSupplier );
8086 CompletableFuture <ConsumptionProbe > bucketFuture = bucket .tryConsumeAndReturnRemaining (config .getTokens ());
8187 ConsumptionProbe consumptionProbe ;
0 commit comments