Skip to content

Commit

Permalink
make the number of buckets used in rate limiting configurable #117
Browse files Browse the repository at this point in the history
- rename cost variable to numTokens
- add readme hint
  • Loading branch information
MarcGiffing committed Dec 22, 2022
1 parent 068c0a8 commit cacef54
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ bucket4j.filters[0].metrics.tags[2].key=USERNAME
bucket4j.filters[0].metrics.tags[2].expression=@securityService.username() != null ? @securityService.username() : 'anonym'
bucket4j.filters[0].strategy=first # [first, all] if multiple rate limits configured the 'first' strategy stops the processing after the first matching
bucket4j.filters[0].rate-limits[0].expression=getRemoteAddr()
bucket4j.filters[0].rate-limits[0].num-tokens=1 # The number of tokens to consume
bucket4j.filters[0].rate-limits[0].execute-condition=1==1 # an optional SpEl expression to decide to execute the rate limit or not
bucket4j.filters[0].rate-limits[0].skip-condition=1==1 # an optional SpEl expression to skip the rate limit
bucket4j.filters[0].rate-limits[0].bandwidths[0].capacity=10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ public class RateLimit {
*/
private String skipCondition;

/**
/**
* SPEL expression to dynamic evaluate filter key
*/
private String expression = "1";

private Integer cost = 1;
/**
* The number of tokens that should be consumed
*/
private Integer numTokens = 1;

private List<BandWidth> bandwidths = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public FilterConfiguration<R> buildFilterConfig(
filterConfig.getMetrics().getTypes(),
metricTagResults);

return proxyWrapper.tryConsumeAndReturnRemaining(key, rl.getCost(), bucketConfiguration, metricBucketListener);
return proxyWrapper.tryConsumeAndReturnRemaining(key, rl.getNumTokens(), bucketConfiguration, metricBucketListener);

}
return null;
Expand Down

0 comments on commit cacef54

Please sign in to comment.