KARTIKrocks/go-ratelimit 是一个托管在 GitHub 上的开源项目,主要使用 Go 编写,采用 MIT License 许可证,目前获得 3 个 Star、0 次 Fork。
仓库全名
KARTIKrocks/go-ratelimit
README 摘要
go-ratelimit A production-ready rate limiting library for Go with five algorithms, per-key limits, Redis-backed distributed limiters, and HTTP middleware. Documentation · API Reference · Changelog Why go-ratelimit? golang.org/x/time/rate gives you one token bucket. Rate limiting a real service needs more than that: Capability | go-ratelimit | x/time/rate | Token bucket | ✓ | ✓ | Leaky bucket, fixed window, sliding…
Flexible and extensible rate limiting library for Go with multiple algorithms, keyed limiters, distributed Redis support, middleware integration, and observability features.
Once a key exists, every in-memory limiter decides a request without
allocating; a keyed limiter allocates one entry the first time it sees a key. Reproduce with
go test -bench=. -benchmem ./... — full numbers in Benchmarks
below.
Note: behind a load balancer or CDN, RemoteAddr is the proxy, so every
client shares one limit. Use TrustedProxiesKeyFunc with your proxies’
networks — see
Key Functions → Behind a reverse proxy.
Exact type signatures are generated from source on
pkg.go.dev.
Runnable programs are in examples/ — basic, multitier, redis,
and prometheus.
Benchmarks
Measured on an Intel i5-11400H @ 2.70GHz (12 cores), Go 1.27, Linux; median of
three runs.
go test -bench=. -benchmem ./...
Single limiters
Benchmark
Single goroutine
12 goroutines, one limiter
Allocs
TokenBucket.Allow
49.7 ns/op
103.5 ns/op
0
LeakyBucket.Allow
50.8 ns/op
103.4 ns/op
0
FixedWindow.Allow
52.7 ns/op
105.3 ns/op
0
SlidingWindow.Allow (log)
46.7 ns/op
—
0
SlidingWindowCounter.Allow
73.8 ns/op
125.0 ns/op
0
Keyed limiters
Benchmark
ns/op
Allocs
KeyedTokenBucket.Allow
74.1
0
KeyedTokenBucket.Allow, 12 goroutines
143.3
0
KeyedTokenBucket.Allow, many keys
73.6
0
KeyedLeakyBucket.Allow
74.6
0
KeyedFixedWindow.Allow
81.4
0
KeyedSlidingWindow.Allow
101.8
0
HTTP middleware
Benchmark
ns/op
B/op
Allocs
Middleware, full request through ServeHTTP
~1,100
1,056
14
The middleware figure includes allocating an httptest.ResponseRecorder and
its header map for every request, which a real server does in net/http
regardless of rate limiting.
Thread Safety
Every limiter is safe for concurrent use. The in-memory limiters guard their
state with a mutex; the Redis limiters run each decision as one atomic Lua
script, so instances never race each other.
Security
A rate limiter is a security control, so its defaults are chosen to be safe
rather than convenient:
Client IPs come from RemoteAddr by default. Proxy headers are opt-in,
and TrustedProxiesKeyFunc only honours them on requests that actually come
from a proxy you listed.
IPv6 clients are keyed by their /64 network (next release; v1.1.0 keys
each address separately), so a client cannot rotate through its own address
block to reset its limit.
Key-set growth can be bounded with SetMaxKeys, so a flood of new keys
cannot exhaust memory. It is unlimited by default; set it on any limiter keyed
by client-controlled values.
Redis failures are observable, and can fail closed for limits that guard
against abuse.
Every push and pull request to main is scanned by
CodeQL,
and govulncheck gates every merge on advisories reachable from this code’s
call graph — run separately against the root module, redisstore, and
metrics, because a scan started from the root stops at nested go.mod
boundaries.
See SECURITY.md for supported versions and how to report a
vulnerability privately.
评论