Code Monkey home page Code Monkey logo

hotstreak's Introduction

Codeship Status for Roverr/hotstreak Go Report Card codecov

Hotstreak is lightweight library for creating a certain type of rate limiting solution.

It provides the tools needed for rate limiting, but does not try to solve everything.

How

Hotstreak uses 2 terms Active and Hot.
While it's active, you can call Hit to increase the inner counter.
After you call Hit for a configurable amount of times, the streak will become Hot.
Hot means that only deactivation can stop the service from being Active for a configurable amount of time. (It also helps with handling hits as fast as possible)
After a configurable time, if no Hit were made at all, it deactivates.

Config

  • Limit - int - Describes how many times we have to hit before a streak becomes hot
  • HotWait - time.Duration - Describes the amount of time we are waiting before declaring a cool down
  • ActiveWait - time.Duration - Describes the amount of time we are waiting to check on a streak being active
  • AlwaysActive - boolean - Describes if the streak can deactivate or should stay active forever

Chainability

Most commands are chainable to allow easier handling.

    streak := hotstreak.New(hotstreak.Config{
        Limit: 20, // Hit 20 times before getting hot
        HotWait: time.Minute * 5, // Wait 5 minutes before cooling down
        ActiveWait:  time.Minute * 10, // Wait 10 minutes before deactivation
    })
    streak.Activate().Hit()

    // do things

    if streak.Hit().IsHot() {
        // Hit and do other things if the streak became hot
    }

See docs for more info.

Example

Make certain number of requests in given time period

    streak := hotstreak.New(hotstreak.Config{
        Limit: 20,
        HotWait: time.Minute * 5,
        ActiveWait:  time.Minute * 10,
    })

    streak.Activate()
    for _, request := range requests {
        streak.Hit()
        // If we are hitting it too hard, try slowing down
        if streak.IsHot() {
            <-time.After(time.Second * 5)
        }
        // .. logic
    }

hotstreak's People

Contributors

roverr avatar

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.