https://github.com/t2bot/go-leaky-bucket
Leaky bucket meter implementation in Go
https://github.com/t2bot/go-leaky-bucket
Last synced: 10 months ago
JSON representation
Leaky bucket meter implementation in Go
- Host: GitHub
- URL: https://github.com/t2bot/go-leaky-bucket
- Owner: t2bot
- License: apache-2.0
- Created: 2024-03-14T22:56:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-01T19:44:42.000Z (about 2 years ago)
- Last Synced: 2025-02-05T06:33:57.453Z (over 1 year ago)
- Language: Go
- Size: 27.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-leaky-bucket
[](https://pkg.go.dev/github.com/t2bot/go-leaky-bucket)
[Leaky bucket meter](https://en.wikipedia.org/wiki/Leaky_bucket#As_a_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`](./examples) for usage and inspiration.