org.springframework.cloud.gateway.filter.ratelimit.RateLimiter Java Examples

The following examples show how to use org.springframework.cloud.gateway.filter.ratelimit.RateLimiter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GatewayApplication.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
@Bean
public RateLimiter<?> getLocalMemoryLimiter() {
    return new LocalMemoryLimiter();
}
 
Example #2
Source File: GatewayAutoConfiguration.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Bean(name = PrincipalNameKeyResolver.BEAN_NAME)
@ConditionalOnBean(RateLimiter.class)
@ConditionalOnMissingBean(KeyResolver.class)
public PrincipalNameKeyResolver principalNameKeyResolver() {
	return new PrincipalNameKeyResolver();
}
 
Example #3
Source File: GatewayAutoConfiguration.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnBean({ RateLimiter.class, KeyResolver.class })
public RequestRateLimiterGatewayFilterFactory requestRateLimiterGatewayFilterFactory(
		RateLimiter rateLimiter, KeyResolver resolver) {
	return new RequestRateLimiterGatewayFilterFactory(rateLimiter, resolver);
}
 
Example #4
Source File: RequestRateLimiterGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public RequestRateLimiterGatewayFilterFactory(RateLimiter defaultRateLimiter,
		KeyResolver defaultKeyResolver) {
	super(Config.class);
	this.defaultRateLimiter = defaultRateLimiter;
	this.defaultKeyResolver = defaultKeyResolver;
}
 
Example #5
Source File: RequestRateLimiterGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public RateLimiter getDefaultRateLimiter() {
	return defaultRateLimiter;
}
 
Example #6
Source File: RequestRateLimiterGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public GatewayFilter apply(Config config) {
	KeyResolver resolver = getOrDefault(config.keyResolver, defaultKeyResolver);
	RateLimiter<Object> limiter = getOrDefault(config.rateLimiter,
			defaultRateLimiter);
	boolean denyEmpty = getOrDefault(config.denyEmptyKey, this.denyEmptyKey);
	HttpStatusHolder emptyKeyStatus = HttpStatusHolder
			.parse(getOrDefault(config.emptyKeyStatus, this.emptyKeyStatusCode));

	return (exchange, chain) -> resolver.resolve(exchange).defaultIfEmpty(EMPTY_KEY)
			.flatMap(key -> {
				if (EMPTY_KEY.equals(key)) {
					if (denyEmpty) {
						setResponseStatus(exchange, emptyKeyStatus);
						return exchange.getResponse().setComplete();
					}
					return chain.filter(exchange);
				}
				String routeId = config.getRouteId();
				if (routeId == null) {
					Route route = exchange
							.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
					routeId = route.getId();
				}
				return limiter.isAllowed(routeId, key).flatMap(response -> {

					for (Map.Entry<String, String> header : response.getHeaders()
							.entrySet()) {
						exchange.getResponse().getHeaders().add(header.getKey(),
								header.getValue());
					}

					if (response.isAllowed()) {
						return chain.filter(exchange);
					}

					setResponseStatus(exchange, config.getStatusCode());
					return exchange.getResponse().setComplete();
				});
			});
}
 
Example #7
Source File: RequestRateLimiterGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public RateLimiter getRateLimiter() {
	return rateLimiter;
}
 
Example #8
Source File: RequestRateLimiterGatewayFilterFactory.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public Config setRateLimiter(RateLimiter rateLimiter) {
	this.rateLimiter = rateLimiter;
	return this;
}