Skip to content

Commit 2a48bd0

Browse files
authored
Adds support for non-blocking route predicates. (#350)
It does so by using a new AsyncPredicate<T> interface, which extends java.util.function.Function<T, Publisher<Boolean>> which is the signature required by the reactor filterWhen() method. Simple filters can still code to the java.util.function.Predicate interface, a default method will delegate. fixes gh-349
1 parent 75d13a5 commit 2a48bd0

19 files changed

Lines changed: 268 additions & 109 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2013-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.springframework.cloud.gateway.handler;
19+
20+
import java.util.Objects;
21+
import java.util.function.Function;
22+
23+
import org.reactivestreams.Publisher;
24+
import reactor.core.publisher.Flux;
25+
import reactor.core.publisher.Mono;
26+
27+
/**
28+
* @author Ben Hale
29+
*/
30+
public interface AsyncPredicate<T> extends Function<T, Publisher<Boolean>> {
31+
32+
default AsyncPredicate<T> and(AsyncPredicate<? super T> other) {
33+
Objects.requireNonNull(other, "other must not be null");
34+
35+
return t -> Flux.zip(apply(t), other.apply(t))
36+
.map(tuple -> tuple.getT1() && tuple.getT2());
37+
}
38+
39+
default AsyncPredicate<T> negate() {
40+
return t -> Mono.from(apply(t)).map(b -> !b);
41+
}
42+
43+
default AsyncPredicate<T> or(AsyncPredicate<? super T> other) {
44+
Objects.requireNonNull(other, "other must not be null");
45+
46+
return t -> Flux.zip(apply(t), other.apply(t))
47+
.map(tuple -> tuple.getT1() || tuple.getT2());
48+
}
49+
50+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ private String getExchangeDesc(ServerWebExchange exchange) {
8989

9090
protected Mono<Route> lookupRoute(ServerWebExchange exchange) {
9191
return this.routeLocator.getRoutes()
92-
.filter(route -> {
92+
.filterWhen(route -> {
9393
// add the current route we are testing
9494
exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, route.getId());
95-
return route.getPredicate().test(exchange);
95+
return route.getPredicate().apply(exchange);
9696
})
9797
// .defaultIfEmpty() put a static Route not found
9898
// or .switchIfEmpty()

‎spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RoutePredicateFactory.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
import java.util.function.Consumer;
2121
import java.util.function.Predicate;
2222

23+
import org.springframework.cloud.gateway.handler.AsyncPredicate;
2324
import org.springframework.cloud.gateway.support.Configurable;
2425
import org.springframework.cloud.gateway.support.NameUtils;
2526
import org.springframework.cloud.gateway.support.ShortcutConfigurable;
2627
import org.springframework.web.server.ServerWebExchange;
2728

29+
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.toAsyncPredicate;
30+
2831
/**
2932
* @author Spencer Gibb
3033
*/
@@ -40,6 +43,13 @@ default Predicate<ServerWebExchange> apply(Consumer<C> consumer) {
4043
return apply(config);
4144
}
4245

46+
default AsyncPredicate<ServerWebExchange> applyAsync(Consumer<C> consumer) {
47+
C config = newConfig();
48+
consumer.accept(config);
49+
beforeApply(config);
50+
return applyAsync(config);
51+
}
52+
4353
default Class<C> getConfigClass() {
4454
throw new UnsupportedOperationException("getConfigClass() not implemented");
4555
}
@@ -53,6 +63,10 @@ default void beforeApply(C config) {}
5363

5464
Predicate<ServerWebExchange> apply(C config);
5565

66+
default AsyncPredicate<ServerWebExchange> applyAsync(C config) {
67+
return toAsyncPredicate(apply(config));
68+
}
69+
5670
default String name() {
5771
return NameUtils.normalizeRoutePredicateName(getClass());
5872
}

‎spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/Route.java‎

Lines changed: 105 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@
2626
import java.util.Objects;
2727
import java.util.function.Predicate;
2828

29+
import org.springframework.cloud.gateway.filter.GatewayFilter;
30+
import org.springframework.cloud.gateway.handler.AsyncPredicate;
31+
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
2932
import org.springframework.core.Ordered;
3033
import org.springframework.util.Assert;
3134
import org.springframework.web.server.ServerWebExchange;
32-
import org.springframework.cloud.gateway.filter.GatewayFilter;
3335
import org.springframework.web.util.UriComponentsBuilder;
3436

37+
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.toAsyncPredicate;
38+
3539
/**
3640
* @author Spencer Gibb
3741
*/
@@ -43,7 +47,7 @@ public class Route implements Ordered {
4347

4448
private final int order;
4549

46-
private final Predicate<ServerWebExchange> predicate;
50+
private final AsyncPredicate<ServerWebExchange> predicate;
4751

4852
private final List<GatewayFilter> gatewayFilters;
4953

@@ -58,46 +62,57 @@ public static Builder builder(RouteDefinition routeDefinition) {
5862
.order(routeDefinition.getOrder());
5963
}
6064

61-
private Route(String id, URI uri, int order, Predicate<ServerWebExchange> predicate, List<GatewayFilter> gatewayFilters) {
65+
public static AsyncBuilder async() {
66+
return new AsyncBuilder();
67+
}
68+
69+
public static AsyncBuilder async(RouteDefinition routeDefinition) {
70+
return new AsyncBuilder()
71+
.id(routeDefinition.getId())
72+
.uri(routeDefinition.getUri())
73+
.order(routeDefinition.getOrder());
74+
}
75+
76+
private Route(String id, URI uri, int order, AsyncPredicate<ServerWebExchange> predicate, List<GatewayFilter> gatewayFilters) {
6277
this.id = id;
6378
this.uri = uri;
6479
this.order = order;
6580
this.predicate = predicate;
6681
this.gatewayFilters = gatewayFilters;
6782
}
6883

69-
public static class Builder {
70-
private String id;
84+
public abstract static class AbstractBuilder<B extends AbstractBuilder<B>> {
85+
protected String id;
7186

72-
private URI uri;
87+
protected URI uri;
7388

74-
private int order = 0;
89+
protected int order = 0;
7590

76-
private Predicate<ServerWebExchange> predicate;
91+
protected List<GatewayFilter> gatewayFilters = new ArrayList<>();
7792

78-
private List<GatewayFilter> gatewayFilters = new ArrayList<>();
93+
protected AbstractBuilder() {}
7994

80-
private Builder() {}
95+
protected abstract B getThis();
8196

82-
public Builder id(String id) {
97+
public B id(String id) {
8398
this.id = id;
84-
return this;
99+
return getThis();
85100
}
86101

87102
public String getId() {
88103
return id;
89104
}
90105

91-
public Builder order(int order) {
106+
public B order(int order) {
92107
this.order = order;
93-
return this;
108+
return getThis();
94109
}
95110

96-
public Builder uri(String uri) {
111+
public B uri(String uri) {
97112
return uri(URI.create(uri));
98113
}
99114

100-
public Builder uri(URI uri) {
115+
public B uri(URI uri) {
101116
this.uri = uri;
102117
if (this.uri.getPort() < 0 && this.uri.getScheme().startsWith("http")) {
103118
// default known http ports
@@ -107,62 +122,115 @@ public Builder uri(URI uri) {
107122
.build(false)
108123
.toUri();
109124
}
125+
return getThis();
126+
}
127+
128+
public abstract AsyncPredicate<ServerWebExchange> getPredicate();
129+
130+
131+
132+
public B replaceFilters(List<GatewayFilter> gatewayFilters) {
133+
this.gatewayFilters = gatewayFilters;
134+
return getThis();
135+
}
136+
137+
public B filter(GatewayFilter gatewayFilter) {
138+
this.gatewayFilters.add(gatewayFilter);
139+
return getThis();
140+
}
141+
142+
public B filters(Collection<GatewayFilter> gatewayFilters) {
143+
this.gatewayFilters.addAll(gatewayFilters);
144+
return getThis();
145+
}
146+
147+
public B filters(GatewayFilter... gatewayFilters) {
148+
return filters(Arrays.asList(gatewayFilters));
149+
}
150+
151+
public Route build() {
152+
Assert.notNull(this.id, "id can not be null");
153+
Assert.notNull(this.uri, "uri can not be null");
154+
AsyncPredicate<ServerWebExchange> predicate = getPredicate();
155+
Assert.notNull(predicate, "predicate can not be null");
156+
157+
return new Route(this.id, this.uri, this.order, predicate, this.gatewayFilters);
158+
}
159+
}
160+
161+
public static class AsyncBuilder extends AbstractBuilder<AsyncBuilder> {
162+
163+
protected AsyncPredicate<ServerWebExchange> predicate;
164+
165+
@Override
166+
protected AsyncBuilder getThis() {
110167
return this;
111168
}
112169

113-
public Predicate<ServerWebExchange> getPredicate() {
170+
@Override
171+
public AsyncPredicate<ServerWebExchange> getPredicate() {
114172
return this.predicate;
115173
}
116174

117-
public Builder predicate(Predicate<ServerWebExchange> predicate) {
175+
public AsyncBuilder predicate(Predicate<ServerWebExchange> predicate) {
176+
return asyncPredicate(toAsyncPredicate(predicate));
177+
}
178+
179+
public AsyncBuilder asyncPredicate(AsyncPredicate<ServerWebExchange> predicate) {
118180
this.predicate = predicate;
119181
return this;
120182
}
121183

122-
public Builder and(Predicate<ServerWebExchange> predicate) {
184+
public AsyncBuilder and(AsyncPredicate<ServerWebExchange> predicate) {
123185
Assert.notNull(this.predicate, "can not call and() on null predicate");
124186
this.predicate = this.predicate.and(predicate);
125187
return this;
126188
}
127189

128-
public Builder or(Predicate<ServerWebExchange> predicate) {
190+
public AsyncBuilder or(AsyncPredicate<ServerWebExchange> predicate) {
129191
Assert.notNull(this.predicate, "can not call or() on null predicate");
130192
this.predicate = this.predicate.or(predicate);
131193
return this;
132194
}
133195

134-
public Builder negate() {
196+
public AsyncBuilder negate() {
135197
Assert.notNull(this.predicate, "can not call negate() on null predicate");
136198
this.predicate = this.predicate.negate();
137199
return this;
138200
}
201+
}
139202

140-
public Builder replaceFilters(List<GatewayFilter> gatewayFilters) {
141-
this.gatewayFilters = gatewayFilters;
203+
public static class Builder extends AbstractBuilder<Builder> {
204+
protected Predicate<ServerWebExchange> predicate;
205+
206+
@Override
207+
protected Builder getThis() {
142208
return this;
143209
}
144210

145-
public Builder filter(GatewayFilter gatewayFilter) {
146-
this.gatewayFilters.add(gatewayFilter);
147-
return this;
211+
@Override
212+
public AsyncPredicate<ServerWebExchange> getPredicate() {
213+
return ServerWebExchangeUtils.toAsyncPredicate(this.predicate);
148214
}
149215

150-
public Builder filters(Collection<GatewayFilter> gatewayFilters) {
151-
this.gatewayFilters.addAll(gatewayFilters);
216+
public Builder and(Predicate<ServerWebExchange> predicate) {
217+
Assert.notNull(this.predicate, "can not call and() on null predicate");
218+
this.predicate = this.predicate.and(predicate);
152219
return this;
153220
}
154221

155-
public Builder filters(GatewayFilter... gatewayFilters) {
156-
return filters(Arrays.asList(gatewayFilters));
222+
public Builder or(Predicate<ServerWebExchange> predicate) {
223+
Assert.notNull(this.predicate, "can not call or() on null predicate");
224+
this.predicate = this.predicate.or(predicate);
225+
return this;
157226
}
158227

159-
public Route build() {
160-
Assert.notNull(this.id, "id can not be null");
161-
Assert.notNull(this.uri, "uri can not be null");
162-
Assert.notNull(this.predicate, "predicate can not be null");
163-
164-
return new Route(this.id, this.uri, this.order, this.predicate, this.gatewayFilters);
228+
public Builder negate() {
229+
Assert.notNull(this.predicate, "can not call negate() on null predicate");
230+
this.predicate = this.predicate.negate();
231+
return this;
165232
}
233+
166234
}
167235

168236
public String getId() {
@@ -177,7 +245,7 @@ public int getOrder() {
177245
return order;
178246
}
179247

180-
public Predicate<ServerWebExchange> getPredicate() {
248+
public AsyncPredicate<ServerWebExchange> getPredicate() {
181249
return this.predicate;
182250
}
183251

0 commit comments

Comments
 (0)
⌂ 换网址