refactor: update logic of round-robin
This commit is contained in:
parent
8b53aae760
commit
5cba376394
3 changed files with 60 additions and 38 deletions
|
|
@ -1,3 +1,9 @@
|
|||
use derive_builder::Builder;
|
||||
use std::sync::{
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
Arc,
|
||||
};
|
||||
|
||||
/// Constants to specify a load balance option
|
||||
pub(super) mod load_balance_options {
|
||||
pub const FIX_TO_FIRST: &str = "none";
|
||||
|
|
@ -6,13 +12,48 @@ pub(super) mod load_balance_options {
|
|||
pub const STICKY_ROUND_ROBIN: &str = "sticky";
|
||||
}
|
||||
|
||||
//
|
||||
// /// Counter for load balancing
|
||||
// pub cnt: UpstreamCount,
|
||||
|
||||
// TODO: カウンタの移動
|
||||
#[derive(Debug, Clone, Builder)]
|
||||
pub struct LbRoundRobinCount {
|
||||
#[builder(default)]
|
||||
cnt: Arc<AtomicUsize>,
|
||||
#[builder(setter(custom), default)]
|
||||
max_val: usize,
|
||||
}
|
||||
impl LbRoundRobinCountBuilder {
|
||||
pub fn max_val(&mut self, v: &usize) -> &mut Self {
|
||||
self.max_val = Some(*v);
|
||||
self
|
||||
}
|
||||
}
|
||||
impl LbRoundRobinCount {
|
||||
/// Get a current count of upstream served
|
||||
fn current_cnt(&self) -> usize {
|
||||
self.cnt.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Increment the count of upstream served up to the max value
|
||||
pub fn increment_cnt(&self) -> usize {
|
||||
if self.current_cnt() < self.max_val - 1 {
|
||||
self.cnt.fetch_add(1, Ordering::Relaxed)
|
||||
} else {
|
||||
// Clear the counter
|
||||
self.cnt.fetch_and(0, Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
/// Load Balancing Option
|
||||
pub enum LoadBalance {
|
||||
/// Fix to the first upstream. Use if only one upstream destination is specified
|
||||
FixToFirst,
|
||||
/// Simple round robin without session persistance
|
||||
RoundRobin, // TODO: カウンタはここにいれる。randomとかには不要なので
|
||||
RoundRobin(LbRoundRobinCount), // TODO: カウンタはここにいれる。randomとかには不要なので
|
||||
/// Randomly chose one upstream server
|
||||
Random,
|
||||
/// Round robin with session persistance using cookie
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue