Skip to content
Simple rate limiting primitives for Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
LICENSE
README.md
bucket.go
bucket_test.go
infinite_bucket.go
infinite_bucket_test.go
ticket_bucket.go
ticket_bucket_test.go

README.md

Rates

Simple rate limiting primitives for Go

This package provides a couple of basic rate limiting primitives which can be used in Go applications to restrict the rate at which certain tasks are performed.

package main

import (
    "log"
    "time"
    
    "github.com/SierraSoftworks/rates"
)

func main() {

    // Buckets are a useful primitive which allow a constant rate of
    // execution while still allowing well behaved clients to burst
    // past that rate for short periods.
    bucket := rates.NewBucket(&rates.BucketConfig{
        MaxSize: 10,
        FillRate: 1,
    })

    for i := 0; i < 20; i++ {
        if bucket.Take() {
            log.Println("Executed task", i)
        } else {
            log.Println("Rate limited task", i)
        }
    }

    select {
        case <-bucket.TakeWhenAvailable():
            log.Println("Waited until we had an available task before running this")
        case <-time.After(time.Second):
            log.Println("We decided not to wait any longer")
    }
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.