Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache gateway filters to avoid sorting in every single request #2756

Closed
123liuziming opened this issue Oct 21, 2022 · 0 comments
Closed

Cache gateway filters to avoid sorting in every single request #2756

123liuziming opened this issue Oct 21, 2022 · 0 comments
Milestone

Comments

@123liuziming
Copy link

123liuziming commented Oct 21, 2022

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?

image

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

image

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.

image

We can solve this problem by following several steps:

  1. Add a Spring Event Listener to monitor the RefreshRoutes Event
  2. When the listener in step1 finds that the routes are changed, It will clear the filters cache of every route
  3. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants