Skip to content

Commit b5f5ff6

Browse files
committed
Moves creation of Supplier<CompletableFuture<BucketConfiguration>> to runtime.
This allows the configured application TaskExecutor bean to be passed to CompletableFuture.supplyAsync() See gh-3983
1 parent 8393a23 commit b5f5ff6

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

spring-cloud-gateway-server-webmvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/Bucket4jFilterFunctions.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@
3030
import io.github.bucket4j.distributed.proxy.AsyncProxyManager;
3131

3232
import org.springframework.cloud.gateway.server.mvc.common.MvcUtils;
33+
import org.springframework.core.task.TaskExecutor;
3334
import org.springframework.http.HttpStatus;
3435
import org.springframework.http.HttpStatusCode;
36+
import org.springframework.scheduling.TaskScheduler;
3537
import org.springframework.util.Assert;
3638
import org.springframework.util.StringUtils;
3739
import org.springframework.web.servlet.function.HandlerFilterFunction;
3840
import org.springframework.web.servlet.function.ServerRequest;
3941
import org.springframework.web.servlet.function.ServerResponse;
4042

43+
import static org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME;
44+
4145
public 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

Comments
 (0)