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//! ### `BoxLoadBalancer<T>`
26//!
27//! An async trait variant that can be used with `async_trait` for boxed trait objects
28//! or dynamic dispatch. Provides similar functionality to `LoadBalancer` but supports
29//! fully `async` method signatures.
30//!
31//! ```rust
32//! use async_trait::async_trait;
33//!
34//! #[async_trait]
35//! pub trait BoxLoadBalancer<T>: Send + Sync + Clone + 'static {
36//!     /// Asynchronously allocate a resource.
37//!     async fn alloc(&self) -> T;
38//!
39//!     /// Attempt to allocate a resource synchronously without awaiting.
40//!     fn try_alloc(&self) -> Option<T>;
41//! }
42//! ```
43pub mod general;
44pub mod interval;
45pub mod limit;
46pub mod proxy;
47pub mod random;
48pub mod simple;
49pub mod threshold;
50pub use anyhow;
51pub use get_if_addrs;
52pub use reqwest;
53
54use async_trait::async_trait;
55use std::future::Future;
56
57pub trait LoadBalancer<T>: Send + Sync + Clone + 'static {
58    fn alloc(&self) -> impl Future<Output = T> + Send;
59    fn try_alloc(&self) -> Option<T>;
60}
61
62#[async_trait]
63pub trait BoxLoadBalancer<T>: Send + Sync + Clone + 'static {
64    async fn alloc(&self) -> T;
65    fn try_alloc(&self) -> Option<T>;
66}