Simple token bucket implementation used for simple rate limiting
The algorithm will fill the bucket back to it's initial size every second
Based on Token Bucket wiki: wikipedia
Minimum requirements:
- Go 1.17
bucket := tokenbucket.NewBucket("MyBucket", 5)
daemon := tokenbucket.NewDaemon(b, flags)
daemon.Start()
Invoking the Hit()
function on the daemon will return either true
/false
indicating if a token was redeemed or not (ie, the bucket was empty will return false)
daemon := tokenbucket.NewDaemon(b, flags)
daemon.Start()
// code ...
daemon.Stop()
Retriable
This flag will enable a random wait between 0-5 seconds before retying a failed token fetch
Forgiving
If failed to retrieve a token, forgiving will return true if the last token was successful
flags := Forgiving | Retryable
w := NewDaemon(b, flags)
w.Start()