clean up repo

This commit is contained in:
psun256
2025-12-18 18:52:28 -05:00
parent 54978770be
commit 7eae7db1f6
13 changed files with 329 additions and 46 deletions

View File

@@ -1,10 +1,10 @@
use rperf3::{Config, Server};
use serde_json::Value;
use std::collections::HashMap;
use std::net::{IpAddr, SocketAddr};
use std::sync::{Arc, RwLock};
use serde_json::Value;
use rperf3::{Config, Server};
use tokio::net::{TcpListener, TcpSocket, TcpStream};
use tokio::io::AsyncBufReadExt;
use tokio::net::{TcpSocket, TcpStream};
// Physical server health statistics, used for certain load balancing algorithms
#[derive(Debug, Default)]
@@ -47,14 +47,14 @@ pub async fn start_healthcheck_listener(
let listener = listener.ok_or_else(|| {
eprintln!("health listener could not bind to port");
std::io::Error::new(std::io::ErrorKind::Other, "health listener failed")
std::io::Error::other("health listener failed")
})?;
println!("healthcheck server listening on {}", addr);
loop {
let (stream, remote_addr) = match listener.accept().await {
let (stream, _remote_addr) = match listener.accept().await {
Ok(v) => v,
Err(e) => {
Err(_e) => {
continue;
}
};
@@ -131,4 +131,4 @@ fn process_metrics(
}
Ok(())
}
}

View File

@@ -1,11 +1,10 @@
use crate::backend::{Backend, BackendPool};
use crate::backend::health::ServerMetrics;
use crate::backend::{Backend, BackendPool};
use crate::balancer::{Balancer, ConnectionInfo};
use rand::prelude::*;
use rand::rngs::SmallRng;
use std::fmt::Debug;
use std::fs::Metadata;
use std::sync::{Arc, RwLock};
use std::sync::Arc;
#[derive(Debug)]
struct AdaptiveNode {
@@ -49,7 +48,7 @@ impl AdaptiveWeightBalancer {
}
impl Balancer for AdaptiveWeightBalancer {
fn choose_backend(&mut self, ctx: ConnectionInfo) -> Option<Arc<Backend>> {
fn choose_backend(&mut self, _ctx: ConnectionInfo) -> Option<Arc<Backend>> {
if self.pool.is_empty() {
return None;
}
@@ -72,7 +71,7 @@ impl Balancer for AdaptiveWeightBalancer {
let safe_w_sum = w_sum.max(1e-12);
let threshold = self.alpha * (r_sum / safe_w_sum);
for idx in 0..self.pool.len() {
let node = &self.pool[idx];
@@ -148,6 +147,7 @@ mod tests {
use super::*;
use crate::backend::Backend;
use std::net::SocketAddr;
use std::sync::RwLock;
fn backend_factory(id: &str, ip: &str, port: u16) -> Arc<Backend> {
Arc::new(Backend::new(

View File

@@ -19,7 +19,7 @@ impl RoundRobinBalancer {
}
impl Balancer for RoundRobinBalancer {
fn choose_backend(&mut self, ctx: ConnectionInfo) -> Option<Arc<Backend>> {
fn choose_backend(&mut self, _ctx: ConnectionInfo) -> Option<Arc<Backend>> {
let backends = self.pool.backends.clone();
if backends.is_empty() {
return None;

View File

@@ -7,8 +7,8 @@ use crate::backend::health::*;
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::balancer::round_robin::RoundRobinBalancer;
use crate::config::*;
pub struct RoutingTable {
@@ -37,7 +37,9 @@ pub fn build_lb(
let mut backends: HashMap<String, Arc<Backend>> = HashMap::new();
for backend_cfg in &config.backends {
let addr: SocketAddr = backend_cfg.ip.parse()
let addr: SocketAddr = backend_cfg
.ip
.parse()
.map_err(|_| format!("bad ip: {}", backend_cfg.ip))?;
let ip = addr.ip();
@@ -87,7 +89,7 @@ pub fn build_lb(
let mut port_groups: HashMap<u16, Vec<IpCidr>> = HashMap::new();
for client_def in &rule.clients {
let (cidr, port) = parse_client(&client_def)?;
let (cidr, port) = parse_client(client_def)?;
port_groups.entry(port).or_default().push(cidr);
}

View File

@@ -3,25 +3,22 @@ mod balancer;
mod config;
mod proxy;
use crate::backend::health::{start_healthcheck_listener, start_iperf_server, ServerMetrics};
use crate::backend::health::{ServerMetrics, start_healthcheck_listener, start_iperf_server};
use crate::balancer::ConnectionInfo;
use crate::config::loader::{build_lb, RoutingTable};
use crate::config::loader::{RoutingTable, build_lb};
use crate::proxy::tcp::proxy_tcp_connection;
use anywho::Error;
use clap::Parser;
use notify::{Event, RecursiveMode, Watcher};
use std::collections::HashMap;
use std::fs::File;
use std::hash::Hash;
use std::net::{IpAddr, SocketAddr};
use std::net::IpAddr;
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, RwLock};
use std::time::Duration;
use tokio::io::AsyncBufReadExt;
use tokio::net::TcpListener;
use tokio::sync::mpsc;
use clap::Parser;
use notify::{Event, RecursiveMode, Watcher};
use std::cmp;
static NEXT_CONN_ID: AtomicU64 = AtomicU64::new(1);
@@ -72,10 +69,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (tx, mut rx) = mpsc::channel(1);
let mut watcher = notify::recommended_watcher(move |res: Result<Event, notify::Error>| {
if let Ok(event) = res {
if event.kind.is_modify() {
let _ = tx.blocking_send(());
}
if let Ok(event) = res
&& event.kind.is_modify()
{
let _ = tx.blocking_send(());
}
})
.unwrap();