update default load balance option

This commit is contained in:
Jun Kurihara 2023-05-26 15:43:23 +09:00
commit f66be5fef1
No known key found for this signature in database
GPG key ID: 48ADFD173ED22B03
7 changed files with 60 additions and 20 deletions

View file

@ -0,0 +1,25 @@
/// Constants to specify a load balance option
pub(super) mod load_balance_options {
pub const FIX_TO_FIRST: &str = "none";
pub const ROUND_ROBIN: &str = "round_robin";
pub const RANDOM: &str = "random";
pub const STICKY_ROUND_ROBIN: &str = "sticky";
}
#[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とかには不要なので
/// Randomly chose one upstream server
Random,
/// Round robin with session persistance using cookie
StickyRoundRobin,
}
impl Default for LoadBalance {
fn default() -> Self {
Self::FixToFirst
}
}

View file

@ -1,3 +1,4 @@
mod load_balance;
mod upstream;
mod upstream_opts;

View file

@ -1,4 +1,7 @@
use super::{BytesName, PathNameBytesExp, UpstreamOption};
use super::{
load_balance::{load_balance_options as lb_opts, LoadBalance},
BytesName, PathNameBytesExp, UpstreamOption,
};
use crate::log::*;
use derive_builder::Builder;
use rand::Rng;
@ -51,23 +54,6 @@ impl ReverseProxy {
}
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
/// Load Balancing Option
pub enum LoadBalance {
/// Simple round robin without session persistance
RoundRobin,
/// Randomly chose one upstream server
Random,
/// Round robin with session persistance using cookie
StickyRoundRobin,
}
impl Default for LoadBalance {
fn default() -> Self {
Self::RoundRobin
}
}
#[derive(Debug, Clone)]
/// Upstream struct just containing uri without path
pub struct Upstream {
@ -87,7 +73,7 @@ pub struct UpstreamGroup {
/// Path in [[PathNameBytesExp]] that will be used to replace the "path" part of incoming url
pub replace_path: Option<PathNameBytesExp>,
#[builder(default)]
#[builder(setter(custom), default)]
/// Load balancing option
pub lb: LoadBalance,
#[builder(default)]
@ -97,6 +83,7 @@ pub struct UpstreamGroup {
/// Activated upstream options defined in [[UpstreamOption]]
pub opts: HashSet<UpstreamOption>,
}
impl UpstreamGroupBuilder {
pub fn path(&mut self, v: &Option<String>) -> &mut Self {
let path = match v {
@ -114,6 +101,24 @@ impl UpstreamGroupBuilder {
);
self
}
pub fn lb(&mut self, v: &Option<String>) -> &mut Self {
let lb = if let Some(x) = v {
match x.as_str() {
lb_opts::FIX_TO_FIRST => LoadBalance::FixToFirst,
lb_opts::ROUND_ROBIN => LoadBalance::RoundRobin,
lb_opts::RANDOM => LoadBalance::Random,
lb_opts::STICKY_ROUND_ROBIN => LoadBalance::StickyRoundRobin,
_ => {
error!("Specified load balancing option is invalid.");
LoadBalance::default()
}
}
} else {
LoadBalance::default()
};
self.lb = Some(lb);
self
}
pub fn opts(&mut self, v: &Option<Vec<String>>) -> &mut Self {
let opts = if let Some(opts) = v {
opts
@ -128,6 +133,7 @@ impl UpstreamGroupBuilder {
}
}
// TODO: カウンタの移動
#[derive(Debug, Clone, Default)]
pub struct UpstreamCount(Arc<AtomicUsize>);
@ -135,6 +141,7 @@ impl UpstreamGroup {
/// Get an enabled option of load balancing [[LoadBalance]]
pub fn get(&self) -> Option<&Upstream> {
match self.lb {
LoadBalance::FixToFirst => self.upstream.get(0),
LoadBalance::RoundRobin => {
let idx = self.increment_cnt();
self.upstream.get(idx)

View file

@ -205,6 +205,7 @@ fn get_reverse_proxy(rp_settings: &[ReverseProxyOption]) -> std::result::Result<
.upstream(rpo.upstream.iter().map(|x| x.to_upstream().unwrap()).collect())
.path(&rpo.path)
.replace_path(&rpo.replace_path)
.lb(&rpo.load_balance)
.opts(&rpo.upstream_options)
.build()
.unwrap();

View file

@ -57,6 +57,7 @@ pub struct ReverseProxyOption {
pub replace_path: Option<String>,
pub upstream: Vec<UpstreamParams>,
pub upstream_options: Option<Vec<String>>,
pub load_balance: Option<String>,
}
#[derive(Deserialize, Debug, Default)]