A lightweight, zero-dependency Go library designed for precise request-per-second (RPS) control. Perfect for load testing, API clients, and rate-limited workloads.
- 🚀 Microsecond Precision: Leverages monotonic clock for accurate scheduling
- ⚡ Dynamic Rate Recovery: Auto-switches to accelerated mode when falling behind
- 🧩 Minimalist API: Single struct with one core method
- 🛡️ Concurrency-Ready: Lock-free design for goroutine-local usage
- 📦 Zero Dependencies: Pure Go standard library implementation
go get github.com/cocotyty/throttlepackage main
import (
"time"
"github.com/cocotyty/throttle"
)
func main() {
controller := throttle.New(1000) // Target: 1000 requests/sec
for {
wait := controller.NextWait()
time.Sleep(wait)
// Execute your rate-limited operation
performTask()
}
}Creates a rate controller instance:
targetRPS: Desired requests per second
Returns the duration to wait before next request. Updates internal state atomically.
Base Timeline:
Expected(n) = StartTime + n*(1s/targetRPS)
Recovery Mode (when behind schedule):
Expected(m) = RecoveryStart + m*(1s/catchupRPS)
Contributions are welcome! Please follow standard workflow:
- Fork the repository
- Create a feature branch
- Commit changes
- Push to the branch
- Open a Pull Request
Distributed under MIT License. See LICENSE for full text.