GithubHelp home page GithubHelp logo

go-ratelimiter's Introduction

go-ratelimiter

This is a negroni middleware that offers request rate limiting using token bucket approach.

It can apply a global rate limit or per key (where you can specify how to obtain the key from the request).

Additionally you can apply a different rate limit per key.

Getting Started

Global rate limiting

To add a global rate limit across all your request with a rate of 1 request per second.

m := NewGlobal().
        WithDefaultQuota(1, time.Second).
        Middleware()

n := negroni.Classic(m)
ts := httptest.NewServer(n)
defer ts.Close()

Changing status code

By default the status code 429 Too Many Requests will be returned when the specified rate limit is exceeded. In you want to change this behavior and return a different status code, just do:

m := NewGlobal().
        WithDefaultQuota(1, time.Second).
        WithStatusCode(400).
        Middleware()

In this case instead of the 429, a 400 Bad Request will be returned.

Rate limiting for different kind of request

Sometimes you want to apply a different rate for different kind of request. Lets take as an example that an account_id is being passed as a query parameter, and we want to apply the limit of 1 request per second but for each account individually instead of globally. In this case you can achieve it by doing:

getKey := func(req *http.Request) string {
    return req.URL.Query().Get("account_id")
}

m := NewLimitByKeys(getKey).
        WithDefaultQuota(1, time.Second).
        Middleware()
...

Different rate limits for different requests

Using the same example as previously, you can additionally specify a different rate limit for different accounts.

getKey := func(req *http.Request) string {
    return req.URL.Query().Get("account_id")
}
getQuota := func(key string) Quota {
    if key == "2" {
        return NewQuota(1, time.Minute)
    }
    return NewQuota(2, time.Second)
}

m := NewLimitByKeys(getKey).
        WithQuotaByKeys(getQuota).
        Middleware()
...

Headers

This middleware will return the following headers:

  • X-RateLimit-Limit With the set request limit in RPM (taking in account if you specified a key and different quotas per key)
  • Retry-After With the time in seconds the client should wait until he can issue another request. This is only sent if the limit has been reached

go-ratelimiter's People

Contributors

xetorthio avatar

Stargazers

 avatar hwsdien avatar  avatar Sandeep Sangamreddi avatar Mehdi Maache avatar Jason.J avatar  avatar  avatar

Watchers

 avatar James Cloos avatar Salah Rifai avatar Marcos Nils avatar  avatar  avatar

go-ratelimiter'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.