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 proxy;
45pub mod interval;
46pub mod ip;
47pub mod limit;
48pub mod random;
49pub mod simple;
50pub mod threshold;
51pub use anyhow;
52pub use get_if_addrs;
53pub use reqwest;
54
55use async_trait::async_trait;
56use std::future::Future;
57
58pub trait LoadBalancer<T>: Send + Sync + Clone + 'static {
59    fn alloc(&self) -> impl Future<Output = T> + Send;
60    fn try_alloc(&self) -> Option<T>;
61}
62
63#[async_trait]
64pub trait BoxLoadBalancer<T>: Send + Sync + Clone + 'static {
65    async fn alloc(&self) -> T;
66    fn try_alloc(&self) -> Option<T>;
67}