Files
zogo/rate/README.md
T
4566704 45864a61dd feat(rate): 自 go-hua 迁入秒级带宽限速器
- 按秒配额补充令牌, Add 阻塞限速, 配合 conn 使用; 附测试、例程与 README
2026-09-20 12:25:36 +08:00

29 lines
833 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# rate
按秒配额的带宽限速器:为读写字节流提供令牌补充式限速(字节数/秒),
常配合 `conn` 包使用。
> 迁移自 go-hua/rate,代码保持原样。
## 用法
```go
import "git.zeroonesoft.cn/golib/zogo/rate"
r := rate.NewRate(512 * 1024) // 512 KB/s;0 表示不限速
r.Start() // 启动每秒配额补充
defer r.Stop()
r.Add(n) // 每次读/写后上报本秒已消耗字节数,超配额时 Add 阻塞到下个周期
r.SetLimit(1024 * 1024) // 动态调整限速
used := r.GetNow() // 本秒已消耗
```
完整可运行例程:[examples/rate/main.go](../examples/rate/main.go)
## 注意
- `NewRate(0)` 为不限速模式,`Add` 直接放行。
- 限速粒度为秒级令牌桶,短时突发精度有限;逐连接限速在 `conn` 中叠加使用。