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 reqwest;
52
53use async_trait::async_trait;
54use std::future::Future;
55
56pub trait LoadBalancer<T>: Send + Sync + Clone + 'static {
57 fn alloc(&self) -> impl Future<Output = T> + Send;
58 fn try_alloc(&self) -> Option<T>;
59}
60
61#[async_trait]
62pub trait BoxLoadBalancer<T>: Send + Sync + Clone + 'static {
63 async fn alloc(&self) -> T;
64 fn try_alloc(&self) -> Option<T>;
65}