io.github.dengchen2020.ratelimiter.local.LocalRateLimiterAutoConfiguration Maven / Gradle / Ivy
package io.github.dengchen2020.ratelimiter.local;
import com.github.benmanes.caffeine.cache.Caffeine;
import io.github.dengchen2020.ratelimiter.RateLimiterInterceptor;
import io.github.dengchen2020.ratelimiter.properties.RateLimiterBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.time.Duration;
/**
* 单机限流自动配置
*
* @author dengchen
* @since 2024/7/1
*/
@ConditionalOnClass(Caffeine.class)
@ConditionalOnProperty(value = "dc.ratelimiter.type", matchIfMissing = true, havingValue = "local")
@EnableConfigurationProperties(RateLimiterBuilder.class)
@Configuration(proxyBeanMethods = false)
public class LocalRateLimiterAutoConfiguration {
@ConditionalOnProperty(value = "dc.ratelimiter.enabled", matchIfMissing = true, havingValue = "true")
@Configuration(proxyBeanMethods = false)
static class LocalRateLimiterInterceptorRegister implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new RateLimiterInterceptor(new LocalRateLimiter(Duration.ofSeconds(1)), new LocalRateLimiter(Duration.ofMinutes(1))));
}
}
}