load_balancer/lib.rs
1//! # Load Balancer Library
2//!
3//! This library provides a set of generic load balancer implementations for distributing
4//! workloads across multiple targets, such as clients, network endpoints, or resources.
5//!
6//! ## Traits
7//!
8//! ### `LoadBalancer<T>`
9//!
10//! A generic trait for asynchronous or synchronous load balancing. Implementors provide
11//! methods to allocate a resource from the pool.
12//!
13//! ```rust
14//! use std::future::Future;
15//!
16//! pub trait LoadBalancer<T>: Send + Sync + Clone + 'static {
17//! /// Asynchronously allocate a resource.
18//! fn alloc(&self) -> impl Future<Output = T> + Send;
19//!
20//! /// Attempt to allocate a resource synchronously without awaiting.
21//! fn try_alloc(&self) -> Option<T>;
22//! }
23//! ```
24
25/// Cooldown-based load balancer with per-entry reuse intervals.
26pub mod cooldown;
27/// Fault-tolerant load balancer with error tracking and auto-disabling.
28pub mod fault_tolerant;
29/// Proxy pool with health checking and latency-based sorting.
30pub mod proxy_pool;
31/// Random selection load balancer.
32pub mod random;
33/// Token-bucket rate limiter.
34pub mod rate_limit;
35/// Round-robin sequential load balancer.
36pub mod round_robin;
37/// Sliding-window load balancer with per-entry rate limits.
38pub mod window;
39
40pub use anyhow;
41pub use dashmap;
42pub use reqwest;
43
44/// A generic load balancer trait for allocating resources from a pool.
45///
46/// Implementors manage a collection of items and distribute them according to
47/// their specific strategy (round-robin, random, cooldown, etc.).
48pub trait LoadBalancer<T>: Send + Sync + Clone + 'static {
49 /// Asynchronously allocate a resource from the pool.
50 fn alloc(&self) -> impl Future<Output = T> + Send;
51 /// Attempt to allocate a resource synchronously without awaiting.
52 fn try_alloc(&self) -> Option<T>;
53}