When adding a new feature to an unstable production environment, a service might want to limit the amount of calls made to external services or procedures. With Simple Feature Throttler you can control this.
To use Simple Feature Throttler in your project, include the dependency in your build.gradle
, and you're good to go!
implementation("com.kerooker.simplefeaturethrottler:simple-feature-throttler:{currentVersion}")
To keep it simple, this library provides two ways to throttle your feature: Throttler
and FeatureThrottler
.
The Throttler
class draws number from a Random pool and checks if the next execution of your feature should be denied or not, based on a throttle percentage:
val throttler = Throttler(90.0) // I'll deny 90% of the executions! Only 10% will go through
fun myFeatureThatShouldBeThrottled() {
if (throttler.shouldThrottle()) return
callOtherSystem()
}
The Throttler
also contains a utility Companion Object, with a function that mimics its behavior:
fun myFeatureThatShouldBeThrottled() {
if (Throttler.shouldThrottle(33.3)) return
callOtherSystem()
}
The FeatureThrottler
may be useful if you have more than one feature currently being throttled, and you want more configuration on it. You can configure a feature in 3 different ways:
- By Environment Variables, with the key being your feature and the value being the throttle percentage, for example:
my.feature=33.3
- By the System Properties, in the same fashion as the Environment Variables:
my.feature=33.3
- By calling the function
FeatureThrottler.setThrottlePercentage("my.feature", 33.3)
To use the value you set anywhere in the program, you can simply use FeatureThrottler.shouldThrottle("my.feature")
So, for example:
fun myFeatureThatShouldBeThrottled() {
if (FeatureThrottler.shouldThrottle("my.feature")) return
callBackend()
}
If for any reason you need to know what is the current throttling for a given feature, you can use FeatureThrottler.getThrottlePercentage("my.feature")