2024-10-28 09:49:48 -07:00
// src/main.rs
2024-11-04 10:56:52 -08:00
use clap ::Parser ;
2024-11-10 21:57:32 -08:00
use clap ::ValueEnum ;
2024-10-28 09:49:48 -07:00
// declare child modules
mod router ;
2024-11-04 10:56:52 -08:00
mod server ;
mod tree ;
2024-10-28 09:49:48 -07:00
2024-11-10 21:57:32 -08:00
use crate ::router ::PolicyConfig ;
#[ derive(Debug, Clone, ValueEnum) ]
pub enum PolicyType {
Random ,
RoundRobin ,
ApproxTree ,
}
2024-10-28 09:49:48 -07:00
#[ derive(Parser, Debug) ]
#[ command(author, version, about, long_about = None) ]
struct Args {
2024-11-10 21:57:32 -08:00
#[ arg(
long ,
default_value = " 127.0.0.1 " ,
help = " Host address to bind the server to "
) ]
2024-10-28 09:49:48 -07:00
host : String ,
2024-11-10 21:57:32 -08:00
#[ arg(long, default_value_t = 3001, help = " Port number to listen on " ) ]
2024-10-28 09:49:48 -07:00
port : u16 ,
2024-11-10 21:57:32 -08:00
#[ arg(
long ,
value_delimiter = ',' ,
help = " Comma-separated list of worker URLs to distribute requests to "
) ]
2024-10-28 09:49:48 -07:00
worker_urls : Vec < String > ,
2024-11-10 21:57:32 -08:00
#[ arg(
long ,
default_value_t = PolicyType ::RoundRobin ,
value_enum ,
help = " Load balancing policy to use: random, round_robin, or approx_tree "
) ]
policy : PolicyType ,
#[ arg(
long ,
requires = " policy " ,
required_if_eq ( " policy " , " approx_tree " ) ,
help = " Path to the tokenizer file, required when using approx_tree policy "
) ]
tokenizer_path : Option < String > ,
#[ arg(
long ,
default_value = " 0.50 " ,
requires = " policy " ,
required_if_eq ( " policy " , " approx_tree " ) ,
help = " Cache threshold (0.0-1.0) for approx_tree routing. Routes to cached worker if match rate exceeds threshold, otherwise routes to shortest queue worker "
) ]
cache_threshold : Option < f32 > ,
}
impl Args {
fn get_policy_config ( & self ) -> PolicyConfig {
match self . policy {
PolicyType ::Random = > PolicyConfig ::RandomConfig ,
PolicyType ::RoundRobin = > PolicyConfig ::RoundRobinConfig ,
PolicyType ::ApproxTree = > PolicyConfig ::ApproxTreeConfig {
tokenizer_path : self
. tokenizer_path
. clone ( )
. expect ( " tokenizer_path is required for approx_tree policy " ) ,
cache_threshold : self
. cache_threshold
. expect ( " cache_threshold is required for approx_tree policy " ) ,
} ,
}
}
2024-10-28 09:49:48 -07:00
}
#[ actix_web::main ]
async fn main ( ) -> std ::io ::Result < ( ) > {
let args = Args ::parse ( ) ;
2024-11-10 21:57:32 -08:00
let policy_config = args . get_policy_config ( ) ;
server ::startup ( args . host , args . port , args . worker_urls , policy_config ) . await
2024-11-04 10:56:52 -08:00
}