Skip to content

Commit 3f62607

Browse files
committed
Remove concatMap in lookupRoute to improve throughput
Fixes gh-3291
1 parent 4cbd756 commit 3f62607

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMapping.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,17 @@ private String getExchangeDesc(ServerWebExchange exchange) {
128128
}
129129

130130
protected Mono<Route> lookupRoute(ServerWebExchange exchange) {
131-
return this.routeLocator.getRoutes()
132-
// individually filter routes so that filterWhen error delaying is not a
133-
// problem
134-
.concatMap(route -> Mono.just(route).filterWhen(r -> {
135-
// add the current route we are testing
136-
exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, r.getId());
137-
return r.getPredicate().apply(exchange);
138-
})
139-
// instead of immediately stopping main flux due to error, log and
140-
// swallow it
141-
.doOnError(e -> logger.error("Error applying predicate for route: " + route.getId(), e))
142-
.onErrorResume(e -> Mono.empty()))
143-
// .defaultIfEmpty() put a static Route not found
144-
// or .switchIfEmpty()
145-
// .switchIfEmpty(Mono.<Route>empty().log("noroute"))
131+
return this.routeLocator.getRoutes().filterWhen(route -> {
132+
// add the current route we are testing
133+
exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, route.getId());
134+
try {
135+
return route.getPredicate().apply(exchange);
136+
}
137+
catch (Exception e) {
138+
logger.error("Error applying predicate for route: " + route.getId(), e);
139+
}
140+
return Mono.just(false);
141+
})
146142
.next()
147143
// TODO: error handling
148144
.map(route -> {

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/RoutePredicateHandlerMappingTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void lookupRouteFromAsyncPredicates(CapturedOutput capturedOutput) {
7171
Route routeError = Route.async()
7272
.id("routeError")
7373
.uri("http://localhost")
74-
.asyncPredicate(swe -> Mono.error(new IllegalStateException("boom1")))
74+
.asyncPredicate(swe -> Mono.just(boom1()))
7575
.build();
7676
Route routeFail = Route.async().id("routeFail").uri("http://localhost").asyncPredicate(swe -> {
7777
throw new IllegalStateException("boom2");
@@ -96,4 +96,8 @@ public void lookupRouteFromAsyncPredicates(CapturedOutput capturedOutput) {
9696
Assertions.assertTrue(capturedOutput.getOut().contains("java.lang.IllegalStateException: boom2"));
9797
}
9898

99+
boolean boom1() {
100+
throw new IllegalStateException("boom1");
101+
}
102+
99103
}

0 commit comments

Comments
 (0)