AbstractRateLimitFilter improvements
See original GitHub issue- I suppose it makes sense to extract
applyPolicy
to the separate class, that would contain a list of other filters, not knowing about concrete implementations. This change will give possibility to add custom policy filters. Something like that:
private final List<PolicyFilter> filters;
private final MatchersByTypeIndex matchersIndex;
@Override
public boolean filter(HttpServletRequest request, Route route,
RateLimitProperties.Policy policy) {
return policy.getType().isEmpty() || allFiltersApply(request, route, policy);
}
private boolean allFiltersApply(HttpServletRequest request, Route route,
RateLimitProperties.Policy policy) {
return filters
.stream()
.allMatch(policyFilter -> {
Set<String> matchers = matchersIndex.get(policyFilter.getType());
return policyFilter.filter(matchers, request, route);
});
}
- Consider using
Set
instead ofList
forusers
andorigins
inuserApply
andoriginApply
methods. Complexity of contains operation on set is O(1), on list is O(n). Usage of Set will not degrade app if big amount of matchers is added. getConfiguredType
can be precomputed on startup and instead of list can be saved into map<Type, Set<String>>. So for example instead of:
private boolean urlApply(List<MatchType> types, Route route) {
List<String> urls = getConfiguredType(types, Type.URL);
return urls.isEmpty()
|| route == null
|| urls.stream().anyMatch(url -> route.getPath().startsWith(url));
}
it can be done this way:
private boolean urlApply(List<MatchType> types, Route route) {
Set<String> urls = matchersIndex.getOrDefault(Type.URL, Collections.emptySet());
return urls.isEmpty()
|| route == null
|| urls.stream().anyMatch(url -> route.getPath().startsWith(url));
}
This would decrease complexity of the method.
If you are interested I would like to open PRs with described changes.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Fetching username for type USER through a custom implementation ...
In the method boolean userApply(List types, HttpServletRequest request) in AbstractRateLimitFilter.java. I don't see any way to override this behavior.
Read more >spring-cloud-zuul-ratelimit from marcosbarbero - Giter VIP
AbstractRateLimitFilter improvements. I suppose it makes sense to extract applyPolicy to the separate class, that would contain a list of other filters, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
AbstractRateLimitFilter
knows too much. It does all policies filtering:userApply
,originApply
,urlApply
. I propose to extract these methods to separate simple filters and to use inAbstractRateLimitFilter
only compound policy filter. Instead of:whereas
PolicyFilter
would look like this:It is just an idea, interfaces can be different. The main point is that adding new filter will not require touching
AbstractRateLimitFilter
. Moreover changing default logic behind these filtering logic wight now is impossible. 2,3. these points are actually about one thing – having separate configuration index class instead of iterating on configuration each time, so changing current configuration won’t be needed.Closing due to inactivity.