Package rate
implements various utilities for rate-limiting and monitoring
rate-limit utilization.
go get github.com/octogo/rate
import (
"fmt"
"time"
"github.com/octogo/rate"
)
func main() {
rateLimit := rate.NewLimit(250 * time.Millisecond)
defer rateLimit.Close()
for i := 0; i < 10; i++ {
rateLimit.Wait()
fmt.Println(i)
}
}
import (
"fmt"
"time"
"github.com/octogo/rate"
)
func main() {
burstLimit := rate.NewBurstLimit(
10, // burst bucket size
4, // regen this many tokens
time.Second, // at this rate
)
defer rateLimit.Close()
// burst through the bucket
for i := 0; i < 10; i++ {
rateLimit.Wait()
fmt.Println(i)
}
// actually wait a tick
rateLimit.Wait()
}
import (
"fmt"
"time"
"github.com/octogo/rate"
)
func main() {
gauge := rate.NewGauge(
10, // burst bucket size
4, // regen this many tokens
time.Second, // at this rate
)
defer rateLimit.Close()
for i := 0; i < 10; i++ {
gauge.Wait()
fmt.Println(i)
}
// after one second, the Gauge should be showing 10/s ticks.
<-time.After(time.Second)
fmt.Println(gauge.Measure())
}