remove concatMap in lookupRoute to improve throughput when routes number increase (#2977)

This commit is contained in:
LiRuihaoA
2024-03-09 03:42:20 +08:00
committed by GitHub
parent 9e42ce6254
commit 074de09767
2 changed files with 13 additions and 10 deletions

View File

@@ -126,17 +126,16 @@ public class RoutePredicateHandlerMapping extends AbstractHandlerMapping {
protected Mono<Route> lookupRoute(ServerWebExchange exchange) {
return this.routeLocator.getRoutes()
// individually filter routes so that filterWhen error delaying is not a
// problem
.concatMap(route -> Mono.just(route).filterWhen(r -> {
.filterWhen(route -> {
// add the current route we are testing
exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, r.getId());
return r.getPredicate().apply(exchange);
exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, route.getId());
try {
return route.getPredicate().apply(exchange);
} catch (Exception e) {
logger.error("Error applying predicate for route: " + route.getId(), e);
}
return Mono.just(false);
})
// instead of immediately stopping main flux due to error, log and
// swallow it
.doOnError(e -> logger.error("Error applying predicate for route: " + route.getId(), e))
.onErrorResume(e -> Mono.empty()))
// .defaultIfEmpty() put a static Route not found
// or .switchIfEmpty()
// .switchIfEmpty(Mono.<Route>empty().log("noroute"))

View File

@@ -66,7 +66,7 @@ public class RoutePredicateHandlerMappingTests {
Route routeFalse = Route.async().id("routeFalse").uri("http://localhost")
.asyncPredicate(swe -> Mono.just(false)).build();
Route routeError = Route.async().id("routeError").uri("http://localhost")
.asyncPredicate(swe -> Mono.error(new IllegalStateException("boom1"))).build();
.asyncPredicate(swe -> Mono.just(test())).build();
Route routeFail = Route.async().id("routeFail").uri("http://localhost").asyncPredicate(swe -> {
throw new IllegalStateException("boom2");
}).build();
@@ -87,4 +87,8 @@ public class RoutePredicateHandlerMappingTests {
Assertions.assertTrue(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom2"));
}
boolean test() {
throw new IllegalStateException("boom1");
}
}