Git Product home page Git Product logo

leocolman / simplefeaturethrottler Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 0.0 72 KB

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 Throttle you can control this.

License: MIT License

Kotlin 100.00%
throttle feature kotlin throttling throttler rate-limit rate-limiter rate-limiting percentage-throttle hacktoberfest

simplefeaturethrottler's Introduction

Simple Feature Throttler

Build Status GitHub Maven Central

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.

Using in your project

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}")

Available API

To keep it simple, this library provides two ways to throttle your feature: Throttler and FeatureThrottler.

Using Throttler:

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()

}

Using Feature Throttler

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:

  1. By Environment Variables, with the key being your feature and the value being the throttle percentage, for example: my.feature=33.3
  2. By the System Properties, in the same fashion as the Environment Variables: my.feature=33.3
  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")

simplefeaturethrottler's People

Contributors

leocolman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

simplefeaturethrottler's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.