rustfmt, fixed ip hash issue
This commit is contained in:
30
src/main.rs
30
src/main.rs
@@ -1,16 +1,13 @@
|
||||
mod backend;
|
||||
mod balancer;
|
||||
mod config;
|
||||
mod backend;
|
||||
mod proxy;
|
||||
|
||||
use crate::balancer::{Balancer, ConnectionInfo};
|
||||
use crate::proxy::tcp::proxy_tcp_connection;
|
||||
use std::fs::File;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use tokio::net::TcpListener;
|
||||
use crate::backend::{Backend, BackendPool, ServerHealth};
|
||||
use crate::balancer::{Balancer, CURRENT_CONNECTION_INFO, ConnectionInfo};
|
||||
use crate::balancer::round_robin::RoundRobinBalancer;
|
||||
use crate::balancer::ip_hashing::SourceIPHash;
|
||||
use crate::proxy::tcp::proxy_tcp_connection;
|
||||
|
||||
static NEXT_CONN_ID: AtomicU64 = AtomicU64::new(1);
|
||||
|
||||
@@ -19,9 +16,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let f = File::open("config.yaml").expect("couldn't open config.yaml");
|
||||
let app_config: config::AppConfig = serde_saphyr::from_reader(f)?;
|
||||
|
||||
println!("Loaded {} backends, {} rules.",
|
||||
app_config.backends.len(),
|
||||
app_config.rules.len()
|
||||
println!(
|
||||
"Loaded {} backends, {} rules.",
|
||||
app_config.backends.len(),
|
||||
app_config.rules.len()
|
||||
);
|
||||
|
||||
let listeners = config::loader::build_lb(app_config);
|
||||
@@ -51,18 +49,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
let remote_ip = remote_addr.ip();
|
||||
let conn_id = NEXT_CONN_ID.fetch_add(1, Ordering::Relaxed);
|
||||
let client_ip = socket.local_addr()?;
|
||||
|
||||
CURRENT_CONNECTION_INFO.with(|info| {
|
||||
*info.borrow_mut() = Some(ConnectionInfo { client_ip : client_ip });
|
||||
});
|
||||
|
||||
let mut chosen_backend = None;
|
||||
|
||||
for (cidr, balancer_idx) in &mut routing_table.entries {
|
||||
if cidr.contains(&remote_ip) {
|
||||
let balancer = &mut routing_table.balancers[*balancer_idx];
|
||||
chosen_backend = balancer.choose_backend();
|
||||
chosen_backend = balancer.choose_backend(ConnectionInfo {
|
||||
client_ip: remote_ip,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -76,11 +71,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
} else {
|
||||
println!("error: no matching rule for {} on port {}", remote_ip, port);
|
||||
}
|
||||
|
||||
// clear the slot after use to avoid stale data
|
||||
CURRENT_CONNECTION_INFO.with(|info| {
|
||||
*info.borrow_mut() = None;
|
||||
});
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user