Skip to content
/ rate Public

A Golang package for limitng rate and measuring utilization.

Notifications You must be signed in to change notification settings

octogo/rate

Repository files navigation

License Build Status GoDoc

OctoRate

Package rate implements various utilities for rate-limiting and monitoring rate-limit utilization.

Getting Started

Installation

go get github.com/octogo/rate

Usage

Primitive Rate-Limiter

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)
    }
}

Burst Bucket Rate-Limiter

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()
}

Gauge

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())
}

About

A Golang package for limitng rate and measuring utilization.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages