MCPcopy
hub / github.com/uber-go/ratelimit

github.com/uber-go/ratelimit @v0.3.1 sqlite

repository ↗ · DeepWiki ↗ · release v0.3.1 ↗
60 symbols 197 edges 8 files 30 documented · 50%
README

Go rate limiter GoDoc 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
}

FAQ:

  • What's the major diff v.s. https://pkg.go.dev/golang.org/x/time/rate? (based on #77)

This ratelimiter was meant to have a (1) simple API and (2) minimal overhead. For more complex use-cases x/time/rate is a great choice. See here for historical context, and here for benchmarks (from 2016).

  • Why does example_test.go fail when I run it locally on Windows? (based on #80)

Windows has some known issues with timers precision. See golang/go#44343. We don't expect to work around it.

Extension points exported contracts — how you extend this code

Limiter (Interface)
Note: This file is inspired by: https://github.com/prashantv/go-bench/blob/master/ratelimit Limiter is used to rate-limi [4 …
ratelimit.go
Option (Interface)
Option configures a Limiter. [1 implementers]
ratelimit.go
Clock (Interface)
Clock is the minimum necessary interface to instantiate a rate limiter with a clock or mock clock, compatible with clock
ratelimit.go

Core symbols most depended-on inside this repo

startTaking
called by 14
ratelimit_test.go
assertCountAt
called by 11
ratelimit_test.go
Now
called by 9
ratelimit.go
createLimiter
called by 8
ratelimit_test.go
Take
called by 8
ratelimit.go
WithSlack
called by 7
ratelimit.go
takeOnceAfter
called by 5
ratelimit_test.go
Per
called by 5
ratelimit.go

Shape

Method 24
Function 22
Struct 8
Interface 4
TypeAlias 2

Languages

Go100%

Modules by API surface

ratelimit_test.go23 symbols
ratelimit.go22 symbols
limiter_atomic.go4 symbols
ratelimit_bench_test.go3 symbols
limiter_mutexbased.go3 symbols
limiter_atomic_int64.go3 symbols
example_test.go2 symbols

Dependencies from manifests, versioned

github.com/aclements/go-moremathv0.0.0-2021011215023 · 1×
github.com/benbjohnson/clockv1.3.0 · 1×
github.com/pmezard/go-difflibv1.0.0 · 1×
github.com/storozhukBM/benchartv1.0.0 · 1×
go.uber.org/atomicv1.7.0 · 1×
golang.org/x/exp/typeparamsv0.0.0-2023062621255 · 1×
golang.org/x/lintv0.0.0-2021050822211 · 1×
golang.org/x/modv0.12.0 · 1×
golang.org/x/perfv0.0.0-2023042722152 · 1×

For agents

$ claude mcp add ratelimit \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact