Coder Social home page Coder Social logo

reugn / equalizer Goto Github PK

View Code? Open in Web Editor NEW
79.0 3.0 2.0 38 KB

A set of performant rate limiters for Go

Home Page: https://pkg.go.dev/github.com/reugn/equalizer

License: MIT License

Go 100.00%
api rate-limiter rate quotas rate-limiting throttler throttling throttle tokenbucket sliding-window

equalizer's People

Contributors

reugn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

equalizer's Issues

tokenBucket: incorrect count of issued when refill

// refill refills the token bucket.
func (tb *tokenBucket) refill() {
issued := atomic.LoadInt32(&tb.issued)
atomic.StoreInt32(&tb.issued, 0)
tb.fillTokenBucket(int(issued))
}
// Ask requires a permit.
// It is a non blocking call, returns true or false.
func (tb *TokenBucket) Ask() bool {
select {
case <-tb.t.permits:
atomic.AddInt32(&tb.t.issued, 1)
return true

A token can be issued between line 68 and 69 and then it will lead to a incorrect issued count and decrease the bucket real capacity.

Example:
refill() and Ask() executes concurrently then:
line 68 executes
line 77 and 78 executes
line 69 and 70 executes

Solution:
issued := atomic.SwapInt32(&tb.issued, 0)

Maybe consider context.Context for blocking Take()

Thanks for providing this simple library!
One thing I noted is that Take() cannot be interrupted if it takes very long (some api limits are very strict), so for example it will considerably delay aborting an action. Could you maybe consider to provide a Take operation which can be interrupted through a context object? That would be a great improvement!

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.