Documentation
¶
Overview ¶
Package peerlimit is a distributed rate limiter that runs as an embedded library rather than a separate service. Each process enforces the limit from its own in-memory state and decides without a network round trip; peers keep those states approximately in sync by gossiping their counts. There is no shared datastore and no central coordinator, so no single failure disables limiting.
The trade-off is accuracy: under partition or gossip lag a limit may be briefly exceeded. peerlimit suits abuse protection, noisy-tenant throttling and soft API limits — not billing-grade quotas.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Node is this process's identity in the cluster. It must be unique and
// stable for the process's lifetime: it names this node's own G-Counter cell.
Node nodeID
// BindPort is the port memberlist listens on for gossip.
BindPort int
// Discoverer supplies the peer addresses to join, at startup and on every
// DiscoverInterval tick.
Discoverer PeerDiscoverer
// Rate is the sustained refill rate, in tokens per second.
Rate float64
// Burst is the bucket capacity: the most tokens available at once.
Burst float64
// KeyTTL is how long a key may sit idle before it is evicted from local
// state. Zero disables eviction. Must be set together with SweepInterval.
KeyTTL time.Duration
// SweepInterval is how often idle keys are checked against KeyTTL. Zero
// disables eviction. Must be set together with KeyTTL.
SweepInterval time.Duration
// SyncInterval is how often this node gossips its state to peers and merges
// theirs. Must be positive.
SyncInterval time.Duration
// DiscoverInterval is how often Discoverer is polled for peers. Must be positive.
DiscoverInterval time.Duration
// LogOutput receives peerlimit's own diagnostics (prefixed [peerlimit]) and
// memberlist's internal logs. Nil discards both.
LogOutput io.Writer
}
Config holds the parameters for a Limiter and is passed to New.
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter is a distributed rate limiter. Build one with New and release it with Close. It is safe for concurrent use.
Example ¶
This example runs a single-node limiter — 100 tokens per second with a burst of 50, scoped per client by the key. In a real deployment several processes find each other through a Discoverer and keep their counts in sync over gossip; the calling code stays exactly the same.
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/ykoloch/peerlimit"
)
func main() {
ctx := context.Background()
lim, err := peerlimit.New(ctx, peerlimit.Config{
Node: "node-1", // unique per process
BindPort: 7946, // gossip port
Discoverer: peerlimit.NewStaticDiscoverer(), // no peers: lone node
Rate: 100, // tokens per second
Burst: 50, // bucket capacity
SyncInterval: time.Second,
DiscoverInterval: 5 * time.Second,
})
if err != nil {
log.Fatal(err)
}
defer lim.Close()
// The key decides what the limit is scoped to: "user:123" per client,
// "global" for one shared limit.
if lim.Allow(ctx, "user:123") {
fmt.Println("request allowed")
}
}
Output:
func New ¶
New creates a Limiter, joins the gossip cluster via conf.Discoverer, and starts the background discover loop (and, when KeyTTL is set, the eviction sweep loop). The loops run until Close is called or ctx is cancelled.
type PeerDiscoverer ¶
PeerDiscoverer returns the current peer addresses ("host:port") to join. It is called once at startup and then polled periodically, so the set may change as peers come and go.
func NewDNSDiscoverer ¶
func NewDNSDiscoverer(host, port string) PeerDiscoverer
NewDNSDiscoverer returns a PeerDiscoverer that resolves host to its addresses and appends port to each — e.g. a Kubernetes headless service. A name that does not resolve yields no peers rather than an error.
func NewStaticDiscoverer ¶
func NewStaticDiscoverer(addrs ...string) PeerDiscoverer
NewStaticDiscoverer returns a PeerDiscoverer that always yields addrs. Use it for a fixed peer list known at startup.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
http
command
Command http demonstrates using peerlimit as net/http middleware.
|
Command http demonstrates using peerlimit as net/http middleware. |
|
Package httpmw adapts a peerlimit.Limiter to standard net/http middleware.
|
Package httpmw adapts a peerlimit.Limiter to standard net/http middleware. |