利用Redis和Golang实现分布式令牌桶算法
利用Redis和Golang实现分布式令牌桶算法,令牌桶算法对速率限制和网络拥塞控制非常有用
Go 数据结构和算法
共34Star
详细介绍
Features
- In-Memory or Redis based storage
- Concurrency-safe
- Put, Take, Count, DynamicFill, and many more primitives
- Designed for distributed systems
- Well tested
- Well documented
Install
go get github.com/b3ntly/bucket
Documentation
Can be found here
Notes
- Test coverage badge is stuck in some cache and is out of date, click the badge to see the actual current coverage
Changelog for version 0.2
- Abstracted storage to its own interface, see storage.go and redis.go for examples
- Added in-memory storage option
- Replaced storageOptions parameter with IStorage, allowing a single storage option to be shared between multiple buckets. This should make it much more efficient to have a large number of buckets, i.e. per logged in user.
Changelog for version 0.3
- Renamed the repository from distributed-token-bucket to bucket
- Moved storage interface and providers to the /storage subpackage
- Added unit testing to the /storage subpackage
- Added watchable.go and changed signatures of all async functions to return a watchable
- Fixed examples
- Added more documentation and comments
Changelog for version 0.4
- Shortened "constructor" names
- Default options
- Better "constructor" signatures
- bucket.DynamicFill()
- bucket.TakeAll()
Changelog for version 0.5
- Added full documentation via Hugo
Benchmarks
go test -bench .
Benchmarks were done on a 2.2GHz quad-core Intel Core i7 with 16GB of RAM.
Version 0.4
Memory
Benchmark | Operations | ns/op |
---|---|---|
BenchmarkBucket_Create-8 | 10000 | 715 ns/op |
BenchmarkBucket_Take-8 | 30000 | 132 ns/op |
BenchmarkBucket_Put-8 | 50000 | 142 ns/op |
Redis
Benchmark | Operations | ns/op |
---|---|---|
BenchmarkBucket_Create-8 | 10000 | 98582 ns/op |
BenchmarkBucket_Take-8 | 30000 | 47716 ns/op |
BenchmarkBucket_Put-8 | 50000 | 31350 ns/op |