From 2e1446d300c118f621befd870d61c67e286edd63 Mon Sep 17 00:00:00 2001 From: psun256 Date: Wed, 10 Dec 2025 22:51:57 -0500 Subject: [PATCH] changed shortest prefix match to longest prefix match; update dockerfile --- Dockerfile | 3 +-- docker-compose.yml | 1 - src/config/loader.rs | 4 +++- src/config/mod.rs | 1 + 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4917c8f..4c3ef3c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,8 +33,7 @@ RUN --mount=type=cache,target=/usr/local/cargo/registry \ # change to scratch and get comment the apk command for prod, i guess FROM alpine:latest AS runtime -# RUN apk add --no-cache ca-certificates curl netcat-openbsd bind-tools strace +RUN apk add --no-cache ca-certificates curl netcat-openbsd bind-tools strace WORKDIR /enginewhy COPY --from=builder /enginewhy/target/x86_64-unknown-linux-musl/release/l4lb /usr/bin/l4lb -COPY config.yaml . ENTRYPOINT ["l4lb"] diff --git a/docker-compose.yml b/docker-compose.yml index 2a7ed42..aff7a46 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,4 @@ services: - # two-arm load balancer load-balancer: image: neoslhp/enginewhy-lb container_name: load-balancer diff --git a/src/config/loader.rs b/src/config/loader.rs index f6880ae..387a9ae 100644 --- a/src/config/loader.rs +++ b/src/config/loader.rs @@ -8,6 +8,7 @@ use crate::backend::*; use crate::balancer::Balancer; use crate::balancer::adaptive_weight::AdaptiveWeightBalancer; use crate::balancer::round_robin::RoundRobinBalancer; +use crate::balancer::ip_hashing::SourceIPHash; use crate::config::*; pub struct RoutingTable { @@ -100,6 +101,7 @@ pub fn build_lb( let balancer: Box = match &rule.strategy { LoadBalancerStrategy::RoundRobin => Box::new(RoundRobinBalancer::new(pool)), + LoadBalancerStrategy::SourceIPHash => Box::new(SourceIPHash::new(pool)), LoadBalancerStrategy::Adaptive { coefficients, alpha, @@ -119,7 +121,7 @@ pub fn build_lb( for table in listeners.values_mut() { table .entries - .sort_by(|(a, _), (b, _)| a.network_length().cmp(&b.network_length())); + .sort_by(|(a, _), (b, _)| b.network_length().cmp(&a.network_length())); } Ok((listeners, healths)) diff --git a/src/config/mod.rs b/src/config/mod.rs index 310358d..d2041d0 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -57,5 +57,6 @@ pub struct RuleConfig { #[serde(tag = "type")] pub enum LoadBalancerStrategy { RoundRobin, + SourceIPHash, Adaptive { coefficients: [f64; 4], alpha: f64 }, }