Search in sources :

Example 1 with FilterConfiguration

use of com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration in project bucket4j-spring-boot-starter by MarcGiffing.

the class Bucket4JAutoConfigurationZuul method createZuulFilter.

private ZuulFilter createZuulFilter(int position) {
    Integer filterCount = 0;
    if (properties.getFilters().size() >= (position + 1)) {
        Bucket4JConfiguration filter = properties.getFilters().get(position);
        filterCount++;
        FilterConfiguration filterConfig = buildFilterConfig(filter, cacheManager, zuulExpressionParser(), beanFactory);
        zuulConfigurationHolder().addFilterConfiguration(filter);
        log.info("create-zuul-filter;{};{};{}", position, filter.getCacheName(), filter.getUrl());
        return new ZuulRateLimitFilter(filterConfig);
    }
    return null;
}
Also used : Bucket4JConfiguration(com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) ZuulRateLimitFilter(com.giffing.bucket4j.spring.boot.starter.zuul.ZuulRateLimitFilter)

Example 2 with FilterConfiguration

use of com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration in project bucket4j-spring-boot-starter by MarcGiffing.

the class ServletRateLimitFilterTest method setup.

@Before
public void setup() {
    consumptionProbe = Mockito.mock(ConsumptionProbe.class);
    when(consumptionProbe.isConsumed()).thenReturn(true);
    rateLimitCheck1 = mock(RateLimitCheck.class);
    rateLimitCheck2 = mock(RateLimitCheck.class);
    rateLimitCheck3 = mock(RateLimitCheck.class);
    configuration = new FilterConfiguration();
    configuration.setRateLimitChecks(Arrays.asList(rateLimitCheck1, rateLimitCheck2, rateLimitCheck3));
    configuration.setUrl("url");
    filter = new ServletRequestFilter(configuration);
}
Also used : ServletRequestFilter(com.giffing.bucket4j.spring.boot.starter.servlet.ServletRequestFilter) RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe) Before(org.junit.Before)

Example 3 with FilterConfiguration

use of com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration in project bucket4j-spring-boot-starter by MarcGiffing.

the class Bucket4JBaseConfiguration method buildFilterConfig.

public FilterConfiguration buildFilterConfig(Bucket4JConfiguration config, CacheManager cacheManager, ExpressionParser expressionParser, BeanFactory beanFactory) {
    FilterConfiguration filterConfig = new FilterConfiguration();
    filterConfig.setUrl(config.getUrl());
    filterConfig.setOrder(config.getFilterOrder());
    filterConfig.setStrategy(config.getStrategy());
    filterConfig.setHttpResponseBody(config.getHttpResponseBody());
    ProxyManager<String> buckets = Bucket4j.extension(JCache.class).proxyManagerForCache(jCache(config.getCacheName(), cacheManager));
    config.getRateLimits().forEach(rl -> {
        ConfigurationBuilder<?> configBuilder = Bucket4j.configurationBuilder();
        for (BandWidthConfig bandWidth : rl.getBandwidths()) {
            configBuilder = configBuilder.addLimit(Bandwidth.simple(bandWidth.getCapacity(), Duration.of(bandWidth.getTime(), bandWidth.getUnit())));
        }
        ;
        final ConfigurationBuilder<?> configBuilderToUse = configBuilder;
        RateLimitCheck rlc = (servletRequest) -> {
            boolean skipRateLimit = false;
            if (rl.getSkipCondition() != null) {
                skipRateLimit = skipCondition(rl, expressionParser, beanFactory).evalute(servletRequest);
            }
            if (rl.getExecuteCondition() != null && !skipRateLimit) {
                skipRateLimit = !executeCondition(rl, expressionParser, beanFactory).evalute(servletRequest);
            }
            if (!skipRateLimit) {
                String key = getKeyFilter(filterConfig.getUrl(), rl, expressionParser, beanFactory).key(servletRequest);
                Bucket bucket = buckets.getProxy(key, () -> configBuilderToUse.buildConfiguration());
                ConsumptionProbe probe = bucket.tryConsumeAndReturnRemaining(1);
                return probe;
            }
            return null;
        };
        filterConfig.getRateLimitChecks().add(rlc);
    });
    return filterConfig;
}
Also used : Bucket4JConfiguration(com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration) KeyFilter(com.giffing.bucket4j.spring.boot.starter.context.KeyFilter) JCacheNotFoundException(com.giffing.bucket4j.spring.boot.starter.exception.JCacheNotFoundException) RateLimit(com.giffing.bucket4j.spring.boot.starter.context.properties.RateLimit) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe) ProxyManager(io.github.bucket4j.grid.ProxyManager) Bucket4JAutoConfigurationZuul(com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul) Condition(com.giffing.bucket4j.spring.boot.starter.context.Condition) MissingKeyFilterExpressionException(com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException) Duration(java.time.Duration) BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) Cache(javax.cache.Cache) Bucket4JAutoConfigurationServletFilter(com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter) GridBucketState(io.github.bucket4j.grid.GridBucketState) ConfigurationBuilder(io.github.bucket4j.ConfigurationBuilder) Bandwidth(io.github.bucket4j.Bandwidth) Bucket(io.github.bucket4j.Bucket) Bucket4j(io.github.bucket4j.Bucket4j) ExpressionParser(org.springframework.expression.ExpressionParser) JCache(io.github.bucket4j.grid.jcache.JCache) BeanFactory(org.springframework.beans.factory.BeanFactory) Expression(org.springframework.expression.Expression) CacheManager(javax.cache.CacheManager) BandWidthConfig(com.giffing.bucket4j.spring.boot.starter.context.BandWidthConfig) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) StringUtils(org.springframework.util.StringUtils) Bucket(io.github.bucket4j.Bucket) BandWidthConfig(com.giffing.bucket4j.spring.boot.starter.context.BandWidthConfig) RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe) JCache(io.github.bucket4j.grid.jcache.JCache)

Example 4 with FilterConfiguration

use of com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration in project bucket4j-spring-boot-starter by MarcGiffing.

the class Bucket4JAutoConfigurationServletFilter method getFilterRegistrationBean.

private FilterRegistrationBean getFilterRegistrationBean(int position) {
    Integer filterCount = 0;
    if (properties.getFilters().size() >= (position + 1)) {
        Bucket4JConfiguration filter = properties.getFilters().get(position);
        filterCount++;
        FilterConfiguration filterConfig = buildFilterConfig(filter, cacheManager, servletFilterExpressionParser(), beanFactory);
        servletConfigurationHolder().addFilterConfiguration(filter);
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setName("bucket4JRequestFilter" + position);
        registration.setFilter(new ServletRequestFilter(filterConfig));
        registration.addUrlPatterns(filter.getUrl());
        registration.setOrder(filter.getFilterOrder());
        log.info("create-servlet-filter;{};{};{}", position, filter.getCacheName(), filter.getUrl());
        return registration;
    }
    return null;
}
Also used : ServletRequestFilter(com.giffing.bucket4j.spring.boot.starter.servlet.ServletRequestFilter) Bucket4JConfiguration(com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) FilterRegistrationBean(org.springframework.boot.web.servlet.FilterRegistrationBean)

Example 5 with FilterConfiguration

use of com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration in project bucket4j-spring-boot-starter by MarcGiffing.

the class ZuulRateLimitFilterTest method setup.

@Before
public void setup() {
    consumptionProbe = Mockito.mock(ConsumptionProbe.class);
    when(consumptionProbe.isConsumed()).thenReturn(true);
    rateLimitCheck1 = mock(RateLimitCheck.class);
    rateLimitCheck2 = mock(RateLimitCheck.class);
    rateLimitCheck3 = mock(RateLimitCheck.class);
    configuration = new FilterConfiguration();
    configuration.setRateLimitChecks(Arrays.asList(rateLimitCheck1, rateLimitCheck2, rateLimitCheck3));
    configuration.setUrl("url");
    filter = new ZuulRateLimitFilter(configuration) {

        @Override
        protected RequestContext getCurrentRequestContext() {
            return Mockito.mock(RequestContext.class);
        }
    };
}
Also used : RateLimitCheck(com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck) FilterConfiguration(com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration) ConsumptionProbe(io.github.bucket4j.ConsumptionProbe) RequestContext(com.netflix.zuul.context.RequestContext) Before(org.junit.Before)

Aggregations

FilterConfiguration (com.giffing.bucket4j.spring.boot.starter.context.FilterConfiguration)5 RateLimitCheck (com.giffing.bucket4j.spring.boot.starter.context.RateLimitCheck)3 Bucket4JConfiguration (com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration)3 ConsumptionProbe (io.github.bucket4j.ConsumptionProbe)3 ServletRequestFilter (com.giffing.bucket4j.spring.boot.starter.servlet.ServletRequestFilter)2 Before (org.junit.Before)2 Bucket4JAutoConfigurationServletFilter (com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter)1 Bucket4JAutoConfigurationZuul (com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul)1 BandWidthConfig (com.giffing.bucket4j.spring.boot.starter.context.BandWidthConfig)1 Condition (com.giffing.bucket4j.spring.boot.starter.context.Condition)1 KeyFilter (com.giffing.bucket4j.spring.boot.starter.context.KeyFilter)1 RateLimit (com.giffing.bucket4j.spring.boot.starter.context.properties.RateLimit)1 JCacheNotFoundException (com.giffing.bucket4j.spring.boot.starter.exception.JCacheNotFoundException)1 MissingKeyFilterExpressionException (com.giffing.bucket4j.spring.boot.starter.exception.MissingKeyFilterExpressionException)1 ZuulRateLimitFilter (com.giffing.bucket4j.spring.boot.starter.zuul.ZuulRateLimitFilter)1 RequestContext (com.netflix.zuul.context.RequestContext)1 Bandwidth (io.github.bucket4j.Bandwidth)1 Bucket (io.github.bucket4j.Bucket)1 Bucket4j (io.github.bucket4j.Bucket4j)1 ConfigurationBuilder (io.github.bucket4j.ConfigurationBuilder)1