Ratelimit implements a token bucket based rate limiter.
The library presents a minimal interface to use.
go get github.com/vivangkumar/ratelimit
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/vivangkumar/ratelimit"
)
func main() {
// Creates a rate limiter with a maximum burst of 100 tokens
// allowing 100 tokens per second to be acquired.
rl := ratelimit.New(100, 100, 1*time.Second)
// Get one token.
if rl.Add() {
fmt.Println("yay")
}
// Get multiple tokens.
if rl.AddN(20) {
fmt.Println("woop")
}
// Wait for a single token (or context cancellation)
ctx, cancelFn := context.WithDeadline(context.Background(), 3*time.Second)
defer cancelFn()
err := rl.Wait(ctx)
if err != nil {
log.Fatal(err.Error())
}
}
make fmt
make test
make build
Please add changes between release to the Changelog.