question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to set RequestRateLimiter as default filters via configuration properties

See original GitHub issue

in Finchley.RC1, I found that I can’t configure defaultFilters with RequestRateLimiter like this

default-filters:
- name: RequestRateLimiter
  args:
    burstCapacity: 2
    replenishRate: 2
    keyResolver: '#{@userKeyResolver}'

because AbstractRateLimiter#onApplicationEvent would not put the routeId and routeConfig to the config map.

	@Override
	public void onApplicationEvent(FilterArgsEvent event) {
		Map<String, Object> args = event.getArgs();

		// only defaultFilters is ok, others will return
		if (args.isEmpty() || !hasRelevantKey(args)) {
			return;
		}

		String routeId = event.getRouteId();
		C routeConfig = newConfig();
		ConfigurationUtils.bind(routeConfig, args,
				configurationPropertyName, configurationPropertyName, validator);
		getConfig().put(routeId, routeConfig);
	}
	private boolean hasRelevantKey(Map<String, Object> args) {
		return args.keySet().stream()
				.anyMatch(key -> key.startsWith(configurationPropertyName + "."));
	}

if only add RequestRateLimiter to defaultFilters, only the defaultFilters contains the key configurationPropertyName, others don’t have the key so they are not in config map.

but RedisRateLimiter#isAllowed can’t get the routeConfig through the specified routeId so we will get an exception.

	public Mono<Response> isAllowed(String routeId, String id) {
		if (!this.initialized.get()) {
			throw new IllegalStateException("RedisRateLimiter is not initialized");
		}
                 // the routeConfig can only be obtained when the routeId is "defaultFilters"
		Config routeConfig = getConfig().get(routeId);

		if (routeConfig == null) {
			if (defaultConfig == null) {
				throw new IllegalArgumentException("No Configuration found for route " + routeId);
			}
			routeConfig = defaultConfig;
		}
        
		// ...
	}

exception

java.lang.IllegalArgumentException: No Configuration found for route service_customer
	at org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter.isAllowed(RedisRateLimiter.java:93) ~[spring-cloud-gateway-core-2.0.0.RC1.jar:2.0.0.RC1]

is it a bug or the designer deliberately did so?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
zhaoyibocommented, May 17, 2018

@spencergibb But it doesn’t work, cannot find configuration for route.

1reaction
zhaoyibocommented, May 17, 2018

@spencergibb Yes, I have read this configuration. But this configuration is for the specified route, this can work. However, what I said is that the default-filters are not for the specified route.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't enable RequestRateLimiter filter in Spring Cloud Gateway
I think I know what your problem is. Did you configure a key resolver? If not, that filter will return 403 by default...
Read more >
5. GatewayFilter Factories - Spring Cloud
Route filters allow the modification of the incoming HTTP request or outgoing HTTP response in some manner. Route filters are scoped to a...
Read more >
Spring Cloud Gateway Custom Api limiter - Medium
To set a new KeyResolver, we need to create a bean of KeyResolver class. Here's the example. ApiLimiterConfiguration.javapackage com.faza.
Read more >
Rate Limiting In Spring Cloud Gateway With Redis
When using RequestRateLimiter with Spring Cloud Gateway we may leverage ... .filter.request-rate-limiter.empty-key-status-code properties.
Read more >
Spring Cloud Gateway WebFilter Factories - Baeldung
In this sense, it offers a set of interesting functionalities to apply ... This filter can only be configured using the Java DSL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found