Browse Source

Polish "Add ServerWebExchange a new attribute for PathContainer to avoid repeating parsePath in PathRoutePredicateFactory."

See gh-2884
pull/2918/head
spencergibb 2 years ago
parent
commit
5d8c2cf898
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 3
      spring-cloud-gateway-server/pom.xml
  2. 9
      spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/PathRoutePredicateFactory.java
  3. 4
      spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/PathRoutePredicatePathContainerAttrBenchMarkTests.java

3
spring-cloud-gateway-server/pom.xml

@ -174,12 +174,13 @@
<groupId>org.openjdk.jmh</groupId> <groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId> <artifactId>jmh-core</artifactId>
<version>1.20</version> <version>1.20</version>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.openjdk.jmh</groupId> <groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId> <artifactId>jmh-generator-annprocess</artifactId>
<version>1.20</version> <version>1.20</version>
<scope>provided</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>

9
spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/PathRoutePredicateFactory.java

@ -90,12 +90,9 @@ public class PathRoutePredicateFactory extends AbstractRoutePredicateFactory<Pat
return new GatewayPredicate() { return new GatewayPredicate() {
@Override @Override
public boolean test(ServerWebExchange exchange) { public boolean test(ServerWebExchange exchange) {
PathContainer path = (PathContainer) exchange.getAttributes() PathContainer path = (PathContainer) exchange.getAttributes().computeIfAbsent(
.get(GATEWAY_PREDICATE_PATH_CONTAINER_ATTR); GATEWAY_PREDICATE_PATH_CONTAINER_ATTR,
if (path == null) { s -> parsePath(exchange.getRequest().getURI().getRawPath()));
path = parsePath(exchange.getRequest().getURI().getRawPath());
exchange.getAttributes().put(GATEWAY_PREDICATE_PATH_CONTAINER_ATTR, path);
}
PathPattern match = null; PathPattern match = null;
for (int i = 0; i < pathPatterns.size(); i++) { for (int i = 0; i < pathPatterns.size(); i++) {

4
spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/PathRoutePredicatePathContainerAttrBenchMarkTests.java

@ -75,4 +75,8 @@ public class PathRoutePredicatePathContainerAttrBenchMarkTests {
} }
} }
public static void main(String[] args) throws Exception {
org.openjdk.jmh.Main.main(args);
}
} }

Loading…
Cancel
Save