-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support for Method level @ratelimiting annoation #250
- Loading branch information
1 parent
7d4cbd9
commit 819217c
Showing
47 changed files
with
1,738 additions
and
1,072 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...text/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/ExpressionParams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.giffing.bucket4j.spring.boot.starter.context; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.expression.Expression; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Parameter information for the evaluation of a Spring {@link Expression} | ||
* | ||
* @param <R> the type of the root object which us used for the SpEl expression. | ||
*/ | ||
@RequiredArgsConstructor | ||
public class ExpressionParams<R> { | ||
|
||
@Getter | ||
private final R rootObject; | ||
|
||
@Getter | ||
private final Map<String, Object> params = new HashMap<>(); | ||
|
||
public void addParam(String name, Object value) { | ||
params.put(name, value); | ||
} | ||
|
||
public ExpressionParams<R> addParams(Map<String, Object> params) { | ||
this.params.putAll(params); | ||
return this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 9 additions & 9 deletions
18
...ontext/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/RateLimitCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
package com.giffing.bucket4j.spring.boot.starter.context; | ||
|
||
|
||
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 from the servlet|webflux|gateway request filter | ||
*/ | ||
@FunctionalInterface | ||
public interface RateLimitCheck<R> { | ||
|
||
/** | ||
* @param request the request information object | ||
* | ||
* @return null if no rate limit should be performed. (maybe skipped or shouldn't be executed). | ||
*/ | ||
RateLimitResultWrapper rateLimit(R request); | ||
/** | ||
* @param params parameter information | ||
* @param mainRateLimit overwrites the rate limit configuration from the properties | ||
* @return null if no rate limit should be performed. (maybe skipped or shouldn't be executed). | ||
*/ | ||
RateLimitResultWrapper rateLimit(ExpressionParams<R> params, RateLimit mainRateLimit); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...xt/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/RateLimitException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.giffing.bucket4j.spring.boot.starter.context; | ||
|
||
/** | ||
* This exception is thrown when the rate limit is reached in the context of a method level when using the | ||
* {@link RateLimiting} annotation. | ||
*/ | ||
public class RateLimitException extends RuntimeException { | ||
} |
55 changes: 55 additions & 0 deletions
55
...-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/RateLimiting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.giffing.bucket4j.spring.boot.starter.context; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface RateLimiting { | ||
|
||
/** | ||
* @return The name of the rate limit configuration as a reference to the property file | ||
*/ | ||
String name(); | ||
|
||
/** | ||
* The cache key which is mayby modified the e.g. the method name {@link RateLimiting#ratePerMethod()} | ||
* | ||
* @return the cache key. | ||
*/ | ||
String cacheKey() default ""; | ||
|
||
/** | ||
* An optional execute condition which overrides the execute condition from the property file | ||
* | ||
* @return the expression in the Spring Expression Language format. | ||
*/ | ||
String executeCondition() default ""; | ||
|
||
/** | ||
* An optional execute condition which overrides the execute condition from the property file | ||
* | ||
* @return the expression in the Spring Expression Language format. | ||
*/ | ||
String skipCondition() default ""; | ||
|
||
/** | ||
* The Name of the annotated method will be added to the cache key. | ||
* It's maybe a problem | ||
* | ||
* @return true if the method name should be added to the cache key. | ||
*/ | ||
boolean ratePerMethod() default false; | ||
|
||
/** | ||
* An optional fall back method when the rate limit occurs instead of throwing an exception. | ||
* The return type must be the same... | ||
* | ||
* TODO | ||
* | ||
* @return the name of the public method which resists in the same class. | ||
*/ | ||
String fallbackMethodName() default ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...in/java/com/giffing/bucket4j/spring/boot/starter/context/properties/MethodProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.giffing.bucket4j.spring.boot.starter.context.properties; | ||
|
||
import com.giffing.bucket4j.spring.boot.starter.context.RateLimiting; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Data; | ||
import lombok.ToString; | ||
|
||
@Data | ||
@ToString | ||
public class MethodProperties { | ||
|
||
/** | ||
* The name of the configuration to reference in the {@link RateLimiting} annotation. | ||
*/ | ||
@NotBlank | ||
private String name; | ||
|
||
/** | ||
* The name of the cache. | ||
*/ | ||
@NotBlank | ||
private String cacheName; | ||
|
||
/** | ||
* The rate limit configuration | ||
*/ | ||
@NotNull | ||
private RateLimit rateLimit; | ||
|
||
} |
Oops, something went wrong.