load-balancer
A set of asynchronous load balancers for Rust, built on Tokio.
| Module | Strategy |
|---|---|
round_robin |
Sequential round-robin allocation |
random |
Uniform random selection |
window |
Per-entry rate limit over a time window |
fault_tolerant |
Rate limit + error tracking with auto-disabling |
cooldown |
Per-entry cooldown interval between allocations |
proxy_pool |
HTTP proxy pool with health checking and latency sorting |
rate_limit |
Token-bucket rate limiter (single + per-key map) |
All balancers (except RateLimiter) implement the LoadBalancer<T> trait:
Examples
RoundRobin
use RoundRobin;
use LoadBalancer;
async
node-a
node-b
node-c
node-a
node-b
node-c
Random
use Random;
use LoadBalancer;
async
s3
s1
s4
s2
s3
Window
Limits the number of allocations per entry within a time window.
use Window;
use LoadBalancer;
use Duration;
async
node-1
node-1
node-1
node-1
node-1
node-2
node-2
node-2
node-1 ← window reset after 1s
node-1
Custom interval:
let lb = new_interval;
FaultTolerant
Extends Window with error tracking: entries that exceed their error
threshold are auto-disabled until the next reset cycle. Call success()
to gradually restore an entry's reputation.
use FaultTolerant;
use LoadBalancer;
async
api-1 → FAIL
api-1 → FAIL
api-2 → OK ← api-1 is now disabled
api-2 → OK
api-2 → OK
api-2 → OK
api-2 → OK
api-3 → OK
api-3 → OK
api-3 → OK
Cooldown
Each entry enters a cooldown period after allocation and cannot be
reused until it expires. Use tokio::time::sleep to wait it out,
or let alloc().await block until an entry becomes available.
use Cooldown;
use LoadBalancer;
use Duration;
async
slow
fast
fast
slow
fast
ProxyPool
HTTP proxy pool with health checking, latency-based sorting, and automatic dead-proxy removal.
use ProxyPool;
use LoadBalancer;
use Duration;
async
Adding proxies dynamically:
pool.extend.await; // append only, no validation
pool.extend_check.await?; // append and validate immediately
RateLimiter
A standalone global rate limiter. Does not implement LoadBalancer;
useful for protecting downstream APIs.
use RateLimiter;
use Duration;
async
request 0 → allowed
request 1 → allowed
request 2 → allowed
request 3 → denied, retry in 1s
request 4 → denied, retry in 1s
RateLimiterMap
Per-key rate limiting with independent limits per key. Keys that haven't been
inserted are rejected by default — call [insert_rate] or [insert_rate_interval] first.
use RateLimiterMap;
use Duration;
async
alice 0 → allowed
alice 1 → allowed
alice 2 → allowed
alice 3 → denied
Custom Timers
Every balancer (RoundRobin, Random, Cooldown, Window, FaultTolerant,
ProxyPool) exposes an update_timer method that spawns a background task to run
a custom handler at a fixed interval. This is useful for dynamic reconfiguration,
reloading entries from a file, or periodic health checks without blocking.
use RoundRobin;
use Duration;
async