1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! A libray for weighted balancing algorithm.
//! It provides three weighted balancing (elect) algorithm.
//! One is random algorithm.
//! Another is weighted balancing algorithm used by LVS.
//! The third is smooth weighted balancing algorithm used by Nginx.
//!
//! The LVS weighted round-robin scheduling is introduced at http://kb.linuxvirtualserver.org/wiki/Weighted_Round-Robin_Scheduling.
//! The Nginx smooth weighted round-robin balancing algorithm is introduced at https://github.com/phusion/nginx/commit/27e94984486058d73157038f7950a0a36ecc6e35.
//! The random algorithm is not smooth although it follows weight configuration.
//! Using it is simple:
//! ```rust
//!     use weighted_rs::{SmoothWeight, Weight};
//!     use std::collections::HashMap;
//!
//!     let mut sw: SmoothWeight<&str> = SmoothWeight::new();
//!     sw.add("server1", 5);
//!     sw.add("server2", 2);
//!     sw.add("server3", 3);
//!
//!     for _ in 0..100 {
//!         let s = sw.next().unwrap();
//!         println!("{}", s);
//!     }
//! ```

use std::collections::HashMap;

pub mod random_weight;
pub mod roundrobin_weight;
pub mod smooth_weight;

pub use random_weight::*;
pub use roundrobin_weight::*;
pub use smooth_weight::*;

// A common trait for weight algorithm.
pub trait Weight {
    type Item;

    // next gets next selected item.
    fn next(&mut self) -> Option<Self::Item>;
    // add adds a weighted item for selection.
    fn add(&mut self, item: Self::Item, weight: isize);

    // all returns all items.
    fn all(&self) -> HashMap<Self::Item, isize>;

    // remove_all removes all weighted items.
    fn remove_all(&mut self);

    // reset resets the balancing algorithm.
    fn reset(&mut self);
}