Skip to content

Commit

Permalink
remove ConditionalOnWebApplication from cache configurations #260 (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcGiffing authored Mar 22, 2024
1 parent 2693ae9 commit 3836c61
Show file tree
Hide file tree
Showing 29 changed files with 221 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


/**
* The skip condition is used to skip or execute a rate limit check.
* This condition is used to skip or execute a rate limit check.
*/
@FunctionalInterface
public interface Condition<R> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ExecutePredicateDefinition(String name) {
"Unable to parse ExecutePredicateDefinition text '" + name + "'" + ", must be of the form name=value");
}
this.name = name.substring(0, eqIdx);
var result = name.substring(eqIdx + 1, name.length());
var result = name.substring(eqIdx + 1);
this.args.put(SIMPLE_CONFIG_KEY, result);
log.debug("execute-predicate-simple-config;name:{};value:{}", this.name, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

/**
* Functional interface to retrieve the Bucket4j key. The key is used to identify the Bucket4j storage.
*
*/
@FunctionalInterface
public interface KeyFilter<R> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

/**
* Used to check if the rate limit should be performed independently from the servlet|webflux|gateway request filter
*
*/
@FunctionalInterface
public interface PostRateLimitCheck<R, P> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.giffing.bucket4j.spring.boot.starter.context.properties.RateLimit;

/**
* Used to check if the rate limit should be performed independently from the servlet|webflux|gateway request filter
* Used to check if the rate limit should be performed independently of the servlet|webflux|gateway request filter
*/
@FunctionalInterface
public interface RateLimitCheck<R> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,42 @@
import lombok.Data;
import lombok.NonNull;

/**
* This data class holds the information of a rate limit check.
*/
@Data
@Builder
public class RateLimitResult {

/**
* If the request was only for estimation without consuming any tokens.
*/
@NonNull
private final boolean estimation;

/**
* The tokens that are consumed.
* <p>
* If {@link #estimation} is true no tokens are consumed.
*/
@NonNull
private final boolean consumed;

/**
* The number of tokens that remains until rate limit is executed.
*/
@NonNull
private final long remainingTokens;

/**
* The time in nanoseconds until the next tokens will be refilled.
*/
@NonNull
private final long nanosToWaitForRefill;

/**
* The time in nanoseconds until the tokens are refilled to its maximum.
*/
@NonNull
private final long nanosToWaitForReset;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

import java.util.concurrent.CompletableFuture;


/**
* A wrapper class to distinguish the {@link RateLimiting} result between a synchronous and asynchronous call.
* <p>
* If possible we should get rid of it...
*/
@Data
public class RateLimitResultWrapper {

private RateLimitResult rateLimitResult;
private CompletableFuture<RateLimitResult> rateLimitResultCompletableFuture;
public RateLimitResultWrapper(RateLimitResult rateLimitResult) {
this.rateLimitResult = rateLimitResult;
}
public RateLimitResultWrapper(CompletableFuture<RateLimitResult> rateLimitResultCompletableFuture) {
this.rateLimitResultCompletableFuture = rateLimitResultCompletableFuture;
}
private RateLimitResult rateLimitResult;

private CompletableFuture<RateLimitResult> rateLimitResultCompletableFuture;

public RateLimitResultWrapper(RateLimitResult rateLimitResult) {
this.rateLimitResult = rateLimitResult;
}

public RateLimitResultWrapper(CompletableFuture<RateLimitResult> rateLimitResultCompletableFuture) {
this.rateLimitResultCompletableFuture = rateLimitResultCompletableFuture;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

public enum RefillSpeed {

/**
* Greedily regenerates tokens
*/
GREEDY,

/**
* Regenerates tokens in an interval manner
*/
INTERVAL,

/**
* Greedily regenerates tokens.
* <p>
* The tokens are refilled as soon as possible.
*/
GREEDY,

/**
* Regenerates tokens in an interval manner.
* <p>
* The tokens refilled on the specific defined interval.
*/
INTERVAL,

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.time.temporal.ChronoUnit;

/**
* Configures the rate of data which should be transfered
* Configures the rate of data which should be transferred.
*
*/
@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import java.util.List;
import java.util.Map;

/**
* Creates an Aspect around methods annotated with @{@link RateLimiting}. It prevents the execution of the method
* if rate limit should be executed.
*/
@Aspect
@Component
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager;
import com.giffing.bucket4j.spring.boot.starter.config.cache.jcache.JCacheBucket4jConfiguration;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnAsynchronousPropertyCondition;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
Expand All @@ -20,7 +21,7 @@
* to access the {@link HazelcastInstance} to retrieve the cache.
*/
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@ConditionalOnAsynchronousPropertyCondition
@ConditionalOnClass({HazelcastInstance.class})
@ConditionalOnBean(HazelcastInstance.class)
@ConditionalOnCache("hazelcast-reactive")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.giffing.bucket4j.spring.boot.starter.config.cache.jcache.JCacheBucket4jConfiguration;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnSynchronousPropertyCondition;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import com.hazelcast.core.HazelcastInstance;
Expand All @@ -22,7 +23,7 @@
* to access the {@link HazelcastInstance} to retrieve the cache.
*/
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnSynchronousPropertyCondition
@ConditionalOnClass({HazelcastCacheManager.class})
@ConditionalOnBean(HazelcastCacheManager.class)
@ConditionalOnCache("hazelcast-spring")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnAsynchronousPropertyCondition;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
Expand All @@ -13,7 +14,7 @@
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@ConditionalOnAsynchronousPropertyCondition
@ConditionalOnClass({ Ignite.class })
@ConditionalOnBean(Ignite.class)
@ConditionalOnCache("ignite")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnAsynchronousPropertyCondition;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
Expand All @@ -13,7 +14,7 @@
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@ConditionalOnAsynchronousPropertyCondition
@ConditionalOnClass({CacheContainer.class})
@ConditionalOnBean(CacheContainer.class)
@ConditionalOnMissingBean(AsyncCacheResolver.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.giffing.bucket4j.spring.boot.starter.config.cache.infinispan.InfinispanCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnSynchronousPropertyCondition;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import org.infinispan.manager.CacheContainer;
Expand All @@ -23,7 +24,7 @@
* bucket4j. See {@link InfinispanCacheResolver} for more informations.
*/
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnSynchronousPropertyCondition
@ConditionalOnClass({CacheContainer.class, Caching.class, JCacheCacheManager.class})
@ConditionalOnBean(CacheContainer.class)
@ConditionalOnCache("jcache-ignite")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnSynchronousPropertyCondition;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import org.springframework.boot.autoconfigure.condition.*;
Expand All @@ -15,7 +16,7 @@
import javax.cache.Caching;

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnSynchronousPropertyCondition
@ConditionalOnClass({Caching.class, JCacheCacheManager.class})
@ConditionalOnBean(CacheManager.class)
@ConditionalOnCache("jcache")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnSynchronousPropertyCondition;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import io.github.bucket4j.redis.jedis.cas.JedisBasedProxyManager.JedisBasedProxyManagerBuilder;
Expand All @@ -14,7 +15,7 @@
import redis.clients.jedis.JedisPool;

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnSynchronousPropertyCondition
@ConditionalOnClass(JedisBasedProxyManagerBuilder.class)
@ConditionalOnBean(JedisPool.class)
@ConditionalOnCache("redis-jedis")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnAsynchronousPropertyCondition;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
Expand All @@ -14,7 +15,7 @@
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@ConditionalOnAsynchronousPropertyCondition
@ConditionalOnClass(LettuceBasedProxyManager.class)
@ConditionalOnBean(RedisClient.class)
@ConditionalOnCache("redis-lettuce")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnAsynchronousPropertyCondition;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
Expand All @@ -17,7 +18,7 @@
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@ConditionalOnAsynchronousPropertyCondition
@ConditionalOnClass(RedissonBasedProxyManager.class)
@ConditionalOnBean(CommandAsyncExecutor.class)
@ConditionalOnCache("redis-redisson")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.giffing.bucket4j.spring.boot.starter.config.condition;

import org.springframework.context.annotation.Conditional;

import java.lang.annotation.*;

/**
* {@link Conditional @Conditional} that only matches when in the Bucket4j properties
* asynchronous configuration exists. E.g. there are reactive filters registered (WEBFLUX, GATEWAY).
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnAsynchronousPropertyCondition.class)
public @interface ConditionalOnAsynchronousPropertyCondition {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Conditional;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* {@link Conditional @Conditional} that matches under the following conditions.
* <ul>
* <li>The 'bucket4j.enabled' is not set.</li>
* <li>The 'bucket4j.enabled' is set to true.</li>
* </ul>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, value = { "enabled" }, matchIfMissing = true)
@Target({ElementType.TYPE, ElementType.METHOD})
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, value = {"enabled"}, matchIfMissing = true)
public @interface ConditionalOnBucket4jEnabled {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* {@link Conditional @Conditional} that matches under the following conditions.
* <ul>
* <li>The 'bucket4j.cache-to-use' is not set.</li>
* <li>The 'bucket4j.cache-to-use' property matches the given {@link #value()}.</li>
* </ul>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", matchIfMissing = true)
Expand Down
Loading

0 comments on commit 3836c61

Please sign in to comment.