From 9557d12253cf2759245fdbbc2f86f6a4974bef3d Mon Sep 17 00:00:00 2001 From: zhengchao <2923289118@qq.com> Date: Thu, 27 Oct 2022 04:12:49 +0800 Subject: [PATCH] Adds sorted filter cache switch. Fixes gh-2760 Fixes gh-2756 --- .../gateway/config/GatewayProperties.java | 17 ++++++++- .../gateway/handler/FilteringWebHandler.java | 35 +++++++++++++++---- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayProperties.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayProperties.java index 53ea714c..bb117219 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayProperties.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayProperties.java @@ -68,6 +68,21 @@ public class GatewayProperties { */ private boolean failOnRouteDefinitionError = true; + /** + * org.springframework.cloud.gateway.handler.FilteringWebHandler#handle add filter cache + * if true then enable filter cache + */ + private boolean filterCache = false; + + public boolean isFilterCache() { + return filterCache; + } + + public void setFilterCache(boolean filterCache) { + this.filterCache = filterCache; + } + + public List getRoutes() { return routes; } @@ -109,7 +124,7 @@ public class GatewayProperties { .append("defaultFilters", defaultFilters) .append("streamingMediaTypes", streamingMediaTypes) .append("failOnRouteDefinitionError", failOnRouteDefinitionError) - .toString(); + .append("filterCache", filterCache).toString(); } diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/FilteringWebHandler.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/FilteringWebHandler.java index 395ee027..92a981f2 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/FilteringWebHandler.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/FilteringWebHandler.java @@ -18,10 +18,12 @@ package org.springframework.cloud.gateway.handler; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.cloud.gateway.config.GatewayProperties; import reactor.core.publisher.Mono; import org.springframework.cloud.gateway.filter.GatewayFilter; @@ -55,8 +57,13 @@ public class FilteringWebHandler implements WebHandler { private final List globalFilters; - public FilteringWebHandler(List globalFilters) { + private ConcurrentHashMap> RouteFilterMap = new ConcurrentHashMap(); + + private GatewayProperties properties; + + public FilteringWebHandler(List globalFilters, GatewayProperties properties) { this.globalFilters = loadFilters(globalFilters); + this.properties = properties; } private static List loadFilters(List filters) { @@ -84,12 +91,7 @@ public class FilteringWebHandler implements WebHandler { @Override public Mono handle(ServerWebExchange exchange) { Route route = exchange.getRequiredAttribute(GATEWAY_ROUTE_ATTR); - List gatewayFilters = route.getFilters(); - - List combined = new ArrayList<>(this.globalFilters); - combined.addAll(gatewayFilters); - // TODO: needed or cached? - AnnotationAwareOrderComparator.sort(combined); + List combined = getCombinedFilters(route); if (logger.isDebugEnabled()) { logger.debug("Sorted gatewayFilterFactories: " + combined); @@ -98,6 +100,25 @@ public class FilteringWebHandler implements WebHandler { return new DefaultGatewayFilterChain(combined).filter(exchange); } + public List getCombinedFilters(Route route){ + if (this.properties.isFilterCache()) { + if (!this.RouteFilterMap.contains(route)) { + RouteFilterMap.put(route,getAllFilters(route)); + } + return RouteFilterMap.get(route); + }else { + return getAllFilters(route); + } + + } + public List getAllFilters(Route route){ + List gatewayFilters = route.getFilters(); + List combined = new ArrayList<>(this.globalFilters); + combined.addAll(gatewayFilters); + AnnotationAwareOrderComparator.sort(combined); + return combined; + } + private static class DefaultGatewayFilterChain implements GatewayFilterChain { private final int index;