restructuring stuff

This commit is contained in:
psun256
2025-12-03 21:35:08 -05:00
parent 3e61c621e7
commit 07cb45fa73
7 changed files with 130 additions and 1 deletions

71
src/backend/mod.rs Normal file
View File

@@ -0,0 +1,71 @@
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::RwLock;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
pub struct BackendPool {
pub backends: Arc<RwLock<HashMap<String, Arc<Backend>>>>,
}
#[derive(Debug)]
pub struct Backend {
pub id: String,
pub address: SocketAddr,
pub is_healthy: AtomicBool, // no clue how this should work, for now
pub current_load: AtomicUsize, // no clue how this should work, for now
}
impl BackendPool {
pub fn new(initial_backends: Vec<Arc<Backend>>) -> Self {
let mut map = HashMap::new();
for backend in initial_backends {
map.insert(backend.id.clone(), backend);
}
Self {
backends: Arc::new(RwLock::new(map)),
}
}
pub fn add_backend(&self, backend: Arc<Backend>) {
let mut backends_guard = self.backends
.write()
.expect("BackendPool lock poisoned");
// let backends_guard = self.backends.read().unwrap_or_else(|poisoned| poisoned.into_inner());
backends_guard.insert(backend.id.clone(), backend);
}
pub fn get_backend(&self, id: &str) -> Option<Arc<Backend>> {
let backends_guard = self.backends
.read()
.expect("BackendPool lock poisoned");
// let backends_guard = self.backends.read().unwrap_or_else(|poisoned| poisoned.into_inner());
backends_guard.get(id).cloned()
}
pub fn bruh_amogus_sus(&self) {
for k in self.backends.read().unwrap().keys() {
self.backends.write().unwrap().get(k).unwrap().increment_current_load();
}
}
}
impl Backend {
pub fn new(id: String, address: SocketAddr) -> Self {
Self {
id: id,
address: address,
is_healthy: AtomicBool::new(false),
current_load: AtomicUsize::new(0),
}
}
pub fn increment_current_load(&self) {
self.current_load.fetch_add(1, Ordering::SeqCst);
}
pub fn decrement_current_load(&self) {
self.current_load.fetch_sub(1, Ordering::SeqCst);
}
}

0
src/balancer/mod.rs Normal file
View File

0
src/config.rs Normal file
View File

View File

@@ -1,5 +1,11 @@
mod balancer;
mod config;
mod backend;
mod proxy;
use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

27
src/proxy/mod.rs Normal file
View File

@@ -0,0 +1,27 @@
mod tcp_proxy;
use std::net::SocketAddr;
use std::time::Instant;
// owned and accessed by only one thread.
pub struct ConnectionContext {
pub connection_id: u64,
pub client_addr: SocketAddr,
pub start_time: Instant,
pub backend_addr: Option<SocketAddr>,
pub bytes_transferred: usize,
// pub protocol: String,
// pub sticky_id: Option<String>,
}
impl ConnectionContext {
pub fn new(connection_id: u64, client_addr: SocketAddr) -> Self {
Self {
connection_id: connection_id,
client_addr: client_addr,
start_time: Instant::now(),
backend_addr: Default::default(),
bytes_transferred: 0,
}
}
}

2
src/proxy/tcp_proxy.rs Normal file
View File

@@ -0,0 +1,2 @@
use super::*;