-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Description
Spring Cloud Gateway Server version:
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-server</artifactId>
<version>4.0.0-SNAPSHOT</version>
In org.springframework.cloud.gateway.handler.FilteringWebHandler#handle. There is a TODO: needed or cached?
Every time we handle a request, we need to combine the route filters and global filters, and then sort them. Our test results show that caching the filter corresponding to a certain route can reduce unnecessary sorting, thus reducing CPU usage and improving gateway performance under high concurrency
The QPS and RT is shown as below, after we cache the gateway filters, we can improve the QPS of Spring Cloud Gateweay and reduce the RT of request
To solve this problem, We can cache the filters of every single route in a hash map. Every time the routes of Spring Cloud Gateway refreshed, we can update the cached filters of these routes in the hash map we mentioned above.
In Spring Cloud Gateway, we have a listener to monitor the change of routes, which is org.springframework.cloud.gateway.route.RouteRefreshListener. Every time the monitor finds that the routes are changed, it will send an Spring RefreshRoutes Event.
We can solve this problem by following several steps:
- Add a Spring Event Listener to monitor the RefreshRoutes Event
- When the listener in step1 finds that the routes are changed, It will clear the filters cache of every route
- When the request is handled by org.springframework.cloud.gateway.handler.FilteringWebHandler#handle, It will lookup in the filters cache of the route. If the filters of the route are in the cache, we use them directly instead of sorting them again, otherwise, we just sort them and put the sorted filters in the cache.
I've made a pull request to solve the issue


