gofaquan / ratelimit

a ratelimit middleware, from "github.com/juju/ratelimit" & "go.uber.org/ratelimit" ,and makes them together

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go rate limiter Coverage Status test

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
}

About

a ratelimit middleware, from "github.com/juju/ratelimit" & "go.uber.org/ratelimit" ,and makes them together


Languages

Language:Go 100.0%