io.github.bucket4j.grid.ProxyManager Java Examples

The following examples show how to use io.github.bucket4j.grid.ProxyManager. 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: InfinispanJCacheCacheResolver.java    From bucket4j-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
public ProxyManager<String> resolve(String cacheName) {
	Cache<Object, Object> cache = cacheContainer.getCache(cacheName);
	if (cache == null) {
		throw new JCacheNotFoundException(cacheName);
	}

	FunctionalMapImpl functionalMap = FunctionalMapImpl.create(cache.getAdvancedCache());
	return Bucket4j.extension(Infinispan.class).proxyManagerForMap(ReadWriteMapImpl.create(functionalMap));
}
 
Example #2
Source File: JCacheCacheResolver.java    From bucket4j-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
public ProxyManager<String>  resolve(String cacheName) {
	Cache springCache = cacheManager.getCache(cacheName);
	if (springCache == null) {
		throw new JCacheNotFoundException(cacheName);
	}

	return Bucket4j.extension(JCache.class).proxyManagerForCache(springCache);
}
 
Example #3
Source File: AbstractDistributedBucketTest.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testBucketRegistryWithKeyIndependentConfiguration() {
    BucketConfiguration configuration = Bucket4j.configurationBuilder()
            .addLimit(Bandwidth.simple(10, Duration.ofDays(1)))
            .build();

    ProxyManager<String> registry = newProxyManager();
    Bucket bucket1 = registry.getProxy(key, () -> configuration);
    assertTrue(bucket1.tryConsume(10));
    assertFalse(bucket1.tryConsume(1));

    Bucket bucket2 = registry.getProxy(anotherKey, () -> configuration);
    assertTrue(bucket2.tryConsume(10));
    assertFalse(bucket2.tryConsume(1));
}
 
Example #4
Source File: AbstractDistributedBucketTest.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testBucketWithNotLazyConfiguration() {
    BucketConfiguration configuration = Bucket4j.configurationBuilder()
            .addLimit(Bandwidth.simple(10, Duration.ofDays(1)))
            .build();

    ProxyManager<String> registry = newProxyManager();
    Bucket bucket = registry.getProxy(key, configuration);
    assertTrue(bucket.tryConsume(10));
    assertFalse(bucket.tryConsume(1));
}
 
Example #5
Source File: CoherenceWithJdkSerializationTest.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> newProxyManager() {
    return Bucket4j.extension(getExtensionClass()).proxyManagerForCache(cache);
}
 
Example #6
Source File: IgniteTest.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> newProxyManager() {
    return Bucket4j.extension(getExtensionClass()).proxyManagerForCache(cache);
}
 
Example #7
Source File: AbstractJCacheTest.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> newProxyManager() {
    return Bucket4j.extension(JCache.class).proxyManagerForCache(getCache());
}
 
Example #8
Source File: InfinispanTest.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> newProxyManager() {
    return Bucket4j.extension(Infinispan.class).proxyManagerForMap(readWriteMap);
}
 
Example #9
Source File: InfinispanTest.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> newProxyManager() {
    return Bucket4j.extension(Infinispan.class).proxyManagerForMap(readWriteMap);
}
 
Example #10
Source File: HazelcastTest.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> newProxyManager() {
    return Bucket4j.extension(getExtensionClass()).proxyManagerForMap(map);
}
 
Example #11
Source File: HazelcastWithCustomSerializersTest.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> newProxyManager() {
    return Bucket4j.extension(getExtensionClass()).proxyManagerForMap(map);
}
 
Example #12
Source File: HazelcastTest.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> newProxyManager() {
    return Bucket4j.extension(getExtensionClass()).proxyManagerForMap(map);
}
 
Example #13
Source File: HazelcastWithCustomSerializersTest.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> newProxyManager() {
    return Bucket4j.extension(getExtensionClass()).proxyManagerForMap(map);
}
 
Example #14
Source File: CoherenceWithPofSerializationTest.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> newProxyManager() {
    return Bucket4j.extension(getExtensionClass()).proxyManagerForCache(cache);
}
 
Example #15
Source File: IgniteCacheResolver.java    From bucket4j-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
@Override
public ProxyManager<String> resolve(String cacheName) {
	org.apache.ignite.IgniteCache<String, GridBucketState> cache = ignite.cache(cacheName);
	return Bucket4j.extension(io.github.bucket4j.grid.ignite.Ignite.class).proxyManagerForCache(cache);
}
 
Example #16
Source File: Bucket4jIgniteRateLimiter.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> getProxyManager(io.github.bucket4j.grid.ignite.Ignite extension) {
    return extension.proxyManagerForCache(cache);
}
 
Example #17
Source File: Bucket4jJCacheRateLimiter.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> getProxyManager(io.github.bucket4j.grid.jcache.JCache extension) {
    return extension.proxyManagerForCache(cache);
}
 
Example #18
Source File: Bucket4jInfinispanRateLimiter.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> getProxyManager(io.github.bucket4j.grid.infinispan.Infinispan extension) {
    return extension.proxyManagerForMap(readWriteMap);
}
 
Example #19
Source File: Bucket4jHazelcastRateLimiter.java    From spring-cloud-zuul-ratelimit with Apache License 2.0 4 votes vote down vote up
@Override
protected ProxyManager<String> getProxyManager(io.github.bucket4j.grid.hazelcast.Hazelcast extension) {
    return extension.proxyManagerForMap(rateLimit);
}
 
Example #20
Source File: Bucket4JBaseConfiguration.java    From bucket4j-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
public FilterConfiguration<R> buildFilterConfig(Bucket4JConfiguration config, 
		ProxyManager<String> buckets, 
		ExpressionParser expressionParser, 
		ConfigurableBeanFactory  beanFactory) {
	
	config.getRateLimits().forEach(r -> {
		if(r.getFilterKeyType() != null) {
			throw new FilterKeyTypeDeprectatedException();
		}
	});
	
	FilterConfiguration<R> filterConfig = new FilterConfiguration<>();
	filterConfig.setUrl(config.getUrl());
	filterConfig.setOrder(config.getFilterOrder());
	filterConfig.setStrategy(config.getStrategy());
	filterConfig.setHttpResponseBody(config.getHttpResponseBody());
	filterConfig.setMetrics(config.getMetrics());
	
	throwExceptionOnInvalidFilterUrl(filterConfig);
	
	config.getRateLimits().forEach(rl -> {
		
		MetricHandler metricHandler = beanFactory.getBean(MetricHandler.class);
		final ConfigurationBuilder configurationBuilder = prepareBucket4jConfigurationBuilder(rl);
		
		RateLimitCheck<R> rlc = (servletRequest, async) -> {
			
	        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);
	        	BucketConfiguration bucketConfiguration = configurationBuilder.build();
	        	
	        	List<MetricTagResult> metricTagResults = getMetricTags(expressionParser, beanFactory, filterConfig,
						servletRequest);

	        	MetricBucketListener metricBucketListener = new MetricBucketListener(
						config.getCacheName(),
						metricHandler, 
						filterConfig.getMetrics().getTypes(),
						metricTagResults);
				
				Bucket bucket = buckets.getProxy(key, bucketConfiguration).toListenable( metricBucketListener);
	        	if(async) {
	        		return new ConsumptionProbeHolder(bucket.asAsync().tryConsumeAndReturnRemaining(1));
	        	} else {
	        		return new ConsumptionProbeHolder(bucket.tryConsumeAndReturnRemaining(1));
	        	}
	        	
	        }
			return null;
			
		};
		filterConfig.getRateLimitChecks().add(rlc);
		
	});
	
	return filterConfig;
}
 
Example #21
Source File: HazelcastCacheResolver.java    From bucket4j-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
@Override
public ProxyManager<String> resolve(String cacheName) {
	com.hazelcast.core.IMap<String, GridBucketState> map = hazelcastInstance.getMap(cacheName);
	return Bucket4j.extension(Hazelcast.class).proxyManagerForMap(map);
}
 
Example #22
Source File: InfinispanCacheResolver.java    From bucket4j-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
@Override
public ProxyManager<String> resolve(String cacheName) {
	Cache<Object, Object> cache = cacheContainer.getCache(cacheName);
	// TODO how to create an instance of ReadWriteMap
	return Bucket4j.extension(io.github.bucket4j.grid.infinispan.Infinispan.class).proxyManagerForMap(null);
}
 
Example #23
Source File: Coherence.java    From bucket4j with Apache License 2.0 2 votes vote down vote up
/**
 * Creates {@link CoherenceProxyManager} for specified cache.
 *
 * @param cache cache for storing state of buckets
 * @param <T> type of keys in the cache
 * @return {@link ProxyManager} for specified cache.
 */
public <T extends Serializable> ProxyManager<T> proxyManagerForCache(NamedCache<T, GridBucketState> cache) {
    return new CoherenceProxyManager<>(cache);
}
 
Example #24
Source File: Hazelcast.java    From bucket4j with Apache License 2.0 2 votes vote down vote up
/**
 * Creates {@link HazelcastProxyManager} for specified map.
 *
 * @param map map for storing state of buckets
 * @param <T> type of keys in the map
 * @return {@link ProxyManager} for specified map.
 */
public <T extends Serializable> ProxyManager<T> proxyManagerForMap(IMap<T, GridBucketState> map) {
    return new HazelcastProxyManager<>(map);
}
 
Example #25
Source File: Ignite.java    From bucket4j with Apache License 2.0 2 votes vote down vote up
/**
 * Creates {@link IgniteProxyManager} for specified cache.
 *
 * @param cache cache for storing state of buckets
 * @param <T> type of keys in the cache
 * @return {@link ProxyManager} for specified cache.
 */
public <T extends Serializable> ProxyManager<T> proxyManagerForCache(IgniteCache<T, GridBucketState> cache) {
    return new IgniteProxyManager<>(cache);
}
 
Example #26
Source File: Hazelcast.java    From bucket4j with Apache License 2.0 2 votes vote down vote up
/**
 * Creates {@link HazelcastProxyManager} for specified map.
 *
 * @param map map for storing state of buckets
 * @param <T> type of keys in the map
 * @return {@link ProxyManager} for specified map.
 */
public <T extends Serializable> ProxyManager<T> proxyManagerForMap(IMap<T, GridBucketState> map) {
    return new HazelcastProxyManager<>(map);
}
 
Example #27
Source File: Infinispan.java    From bucket4j with Apache License 2.0 2 votes vote down vote up
/**
 * Creates {@link InfinispanProxyManager} for specified cache.
 *
 * @param readWriteMap cache for storing state of buckets
 * @param <K> type of keys in the cache
 * @return {@link ProxyManager} for specified cache.
 */
public <K extends Serializable> ProxyManager<K> proxyManagerForMap(FunctionalMap.ReadWriteMap<K, GridBucketState> readWriteMap) {
    return new InfinispanProxyManager<>(readWriteMap);
}
 
Example #28
Source File: Infinispan.java    From bucket4j with Apache License 2.0 2 votes vote down vote up
/**
 * Creates {@link InfinispanProxyManager} for specified cache.
 *
 * @param readWriteMap cache for storing state of buckets
 * @param <K> type of keys in the cache
 * @return {@link ProxyManager} for specified cache.
 */
public <K extends Serializable> ProxyManager<K> proxyManagerForMap(ReadWriteMap<K, GridBucketState> readWriteMap) {
    return new InfinispanProxyManager<>(readWriteMap);
}
 
Example #29
Source File: JCache.java    From bucket4j with Apache License 2.0 2 votes vote down vote up
/**
 * Creates {@link JCacheProxyManager} for specified cache.
 *
 * @param cache cache for storing state of buckets
 * @param <T> type of keys in the cache
 * @return {@link ProxyManager} for specified cache.
 */
public <T extends Serializable> ProxyManager<T> proxyManagerForCache(Cache<T, GridBucketState> cache) {
    return new JCacheProxyManager<>(cache);
}
 
Example #30
Source File: CacheResolver.java    From bucket4j-spring-boot-starter with Apache License 2.0 votes vote down vote up
ProxyManager<String> resolve(String cacheName);