Skip to content
A Golang blocking leaky-bucket rate limit implementation
Go Python Other
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
internal/clock Crib ratelimit from yab and add a test Oct 25, 2016
scripts Remove yab coverage script Oct 25, 2016
.gitignore Crib ratelimit from yab and add a test Oct 25, 2016
CHANGELOG.md v0.1.0 Jul 29, 2019
LICENSE Crib ratelimit from yab and add a test Oct 25, 2016
Makefile Atomic based limiter (#15) Dec 5, 2019
README.md Crib ratelimit from yab and add a test Oct 25, 2016
glide.lock Fix import path for atomic test dependency (#19) Jul 29, 2019
glide.yaml
mutexbased.go Atomic based limiter (#15) Dec 5, 2019
ratelimit.go Atomic based limiter (#15) Dec 5, 2019
ratelimit_bench_test.go Atomic based limiter (#15) Dec 5, 2019
ratelimit_test.go Fix import path for atomic test dependency (#19) Jul 29, 2019

README.md

Go rate limiter

This package provides a Golang implementation of the leaky-bucket rate limit algorithm. This implementation refills the bucket based on the time elapsed between requests instead of requiring an interval clock to fill the bucket discretely.

Create a rate limiter with a maximum number of operations to perform per second. Call Take() before each operation. Take will sleep until you can continue.

import (
	"fmt"
	"time"

	"go.uber.org/ratelimit"
)

func main() {
    rl := ratelimit.New(100) // per second

    prev := time.Now()
    for i := 0; i < 10; i++ {
        now := rl.Take()
        fmt.Println(i, now.Sub(prev))
        prev = now
    }

    // Output:
    // 0 0
    // 1 10ms
    // 2 10ms
    // 3 10ms
    // 4 10ms
    // 5 10ms
    // 6 10ms
    // 7 10ms
    // 8 10ms
    // 9 10ms
}
You can’t perform that action at this time.