Skip to content

Commit

Permalink
This PR introduces an optional version in RateLimitConfiguration,
Browse files Browse the repository at this point in the history
which allows client code to replace the configuration on the fly.
Bucket4j checks the version against the persisted version, and replace
the configuration dynamically if version > persisted version.
[doc](https://bucket4j.com/8.8.0/toc.html#configuration-replacement)

GitOrigin-RevId: 8662847831bdaba6771062b0ab49699f04bd3bb0
  • Loading branch information
yyuan-squareup authored and svc-squareup-copybara committed Nov 14, 2024
1 parent 64f121e commit bddeae6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ object ExampleRateLimitConfiguration : RateLimitConfiguration {
override val name = "ExpensiveRateLimitedAction"
override val refillAmount = 10L
override val refillPeriod: Duration = Duration.ofMinutes(1L)
override val version = 0L // increment the version when updating the configuration
}
6 changes: 6 additions & 0 deletions wisp/wisp-rate-limiting/api/wisp-rate-limiting.api
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ public abstract interface class wisp/ratelimiting/RateLimitConfiguration {
public abstract fun getName ()Ljava/lang/String;
public abstract fun getRefillAmount ()J
public abstract fun getRefillPeriod ()Ljava/time/Duration;
public abstract fun getVersion ()Ljava/lang/Long;
}

public final class wisp/ratelimiting/RateLimitConfiguration$DefaultImpls {
public static fun getVersion (Lwisp/ratelimiting/RateLimitConfiguration;)Ljava/lang/Long;
}

public abstract interface class wisp/ratelimiting/RateLimitPruner {
Expand Down Expand Up @@ -119,5 +124,6 @@ public final class wisp/ratelimiting/testing/TestRateLimitConfig : wisp/ratelimi
public fun getName ()Ljava/lang/String;
public fun getRefillAmount ()J
public fun getRefillPeriod ()Ljava/time/Duration;
public fun getVersion ()Ljava/lang/Long;
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.github.bucket4j.Bandwidth
import io.github.bucket4j.BucketConfiguration
import io.github.bucket4j.ConsumptionProbe
import io.github.bucket4j.EstimationProbe
import io.github.bucket4j.TokensInheritanceStrategy
import io.github.bucket4j.distributed.BucketProxy
import io.github.bucket4j.distributed.proxy.ProxyManager
import io.micrometer.core.instrument.MeterRegistry
Expand Down Expand Up @@ -106,7 +107,11 @@ class Bucket4jRateLimiter @JvmOverloads constructor(
.addLimit(configuration.toBandwidth())
.build()

return bucketProxy.builder().build(key, bucketConfig)
return bucketProxy.builder().apply {
configuration.version?.let {
this.withImplicitConfigurationReplacement(it, TokensInheritanceStrategy.PROPORTIONALLY)
}
}.build(key, bucketConfig)
}

private fun RateLimitConfiguration.toBandwidth(): Bandwidth {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ interface RateLimitConfiguration {
* The period of time over which [refillAmount] tokens are added back to the bucket
*/
val refillPeriod: Duration

/**
* The version of the configuration. This allows implicit configuration replacement.
* Make sure to increase the version when changing the configuration.
* Desired version should start from 1
*/
val version: Long?
get() = null // returns null to be backward compatible
}

0 comments on commit bddeae6

Please sign in to comment.