Skip to content

t2bot/go-leaky-bucket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-leaky-bucket

GoDoc

Leaky bucket meter implementation in Go.

This implementation atomically drains when its value is being mutated rather than using a timer or continual drain. Before mutation, the bucket will drain the supplied number of units as many times as necessary to match the precision of the supplied interval.

For example, if a bucket is created which drains 5 units every 2 minutes, then after 2.5 minutes only 5 units will be drained. However, the 30 seconds of "unused" drain time will be accounted for to ensure future drains are kept accurate. If another 1.5 minutes were to pass, the bucket will drain by another 5 units because the unused time was recorded.

As a bonus, this implementation supports encoding and decoding the bucket in binary, allowing it to be persisted across application restarts or shared among processes as needed. Synchronization logic is left as an exercise for the consumer.

See ./examples for usage and inspiration.