update dependencies

This commit is contained in:
ridethepig 2023-03-08 12:03:06 +08:00
parent d24a574405
commit a82982bf09
6 changed files with 39 additions and 47 deletions

View File

@ -7,19 +7,19 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
clap = "3.0.0-beta.1" clap = { version = "4.1.8", features = ["derive"] }
httparse = "1.3" httparse = "1.8"
http = "0.2" http = "0.2.9"
log = "0.4" log = "0.4.17"
env_logger = "0.7" env_logger = "0.10.0"
pretty_env_logger = "0.4" pretty_env_logger = "0.4"
threadpool = "1.8" threadpool = "1.8.1"
tokio = { version = "0.2", features = ["full"] } tokio = { version = "1.26", features = ["full"] }
rand = "0.7" rand = "0.8.5"
parking_lot = "0.10" parking_lot = "0.12.1"
[dev-dependencies] [dev-dependencies]
nix = "0.17" nix = "0.26.2"
hyper = "0.13" hyper = "0.14.24"
reqwest = "0.10" reqwest = "0.11.14"
async-trait = "0.1" async-trait = "0.1.66"

View File

@ -1,41 +1,33 @@
mod request; mod request;
mod response; mod response;
use clap::Clap; use clap::Parser;
use rand::{Rng, SeedableRng}; use rand::{Rng, SeedableRng};
use std::net::{TcpListener, TcpStream}; use std::net::{TcpListener, TcpStream};
/// Contains information parsed from the command-line invocation of balancebeam. The Clap macros /// Contains information parsed from the command-line invocation of balancebeam. The Clap macros
/// provide a fancy way to automatically construct a command-line argument parser. /// provide a fancy way to automatically construct a command-line argument parser.
#[derive(Clap, Debug)] #[derive(Parser, Debug)]
#[clap(about = "Fun with load balancing")] #[command(about = "Fun with load balancing")]
struct CmdOptions { struct CmdOptions {
#[clap( /// "IP/port to bind to"
short, #[arg(short, long, default_value = "0.0.0.0:1100")]
long,
about = "IP/port to bind to",
default_value = "0.0.0.0:1100"
)]
bind: String, bind: String,
#[clap(short, long, about = "Upstream host to forward requests to")]
/// "Upstream host to forward requests to"
#[arg(short, long)]
upstream: Vec<String>, upstream: Vec<String>,
#[clap(
long, /// "Perform active health checks on this interval (in seconds)"
about = "Perform active health checks on this interval (in seconds)", #[arg(long, default_value = "10")]
default_value = "10"
)]
active_health_check_interval: usize, active_health_check_interval: usize,
#[clap(
long, /// "Path to send request to for active health checks"
about = "Path to send request to for active health checks", #[arg(long, default_value = "/")]
default_value = "/"
)]
active_health_check_path: String, active_health_check_path: String,
#[clap(
long, /// "Maximum number of requests to accept per IP per minute (0 = unlimited)"
about = "Maximum number of requests to accept per IP per minute (0 = unlimited)", #[arg(long, default_value = "0")]
default_value = "0"
)]
max_requests_per_minute: usize, max_requests_per_minute: usize,
} }
@ -100,7 +92,7 @@ fn main() {
fn connect_to_upstream(state: &ProxyState) -> Result<TcpStream, std::io::Error> { fn connect_to_upstream(state: &ProxyState) -> Result<TcpStream, std::io::Error> {
let mut rng = rand::rngs::StdRng::from_entropy(); let mut rng = rand::rngs::StdRng::from_entropy();
let upstream_idx = rng.gen_range(0, state.upstream_addresses.len()); let upstream_idx = rng.gen_range(0..state.upstream_addresses.len());
let upstream_ip = &state.upstream_addresses[upstream_idx]; let upstream_ip = &state.upstream_addresses[upstream_idx];
TcpStream::connect(upstream_ip).or_else(|err| { TcpStream::connect(upstream_ip).or_else(|err| {
log::error!("Failed to connect to upstream {}: {}", upstream_ip, err); log::error!("Failed to connect to upstream {}: {}", upstream_ip, err);

View File

@ -3,7 +3,7 @@ mod common;
use common::{init_logging, BalanceBeam, EchoServer, ErrorServer, Server}; use common::{init_logging, BalanceBeam, EchoServer, ErrorServer, Server};
use std::time::Duration; use std::time::Duration;
use tokio::time::delay_for; use tokio::time::sleep;
async fn setup_with_params( async fn setup_with_params(
n_upstreams: usize, n_upstreams: usize,
@ -162,7 +162,7 @@ async fn test_active_health_checks_check_http_status() {
upstreams.push(Box::new(ErrorServer::new_at_address(failed_ip).await)); upstreams.push(Box::new(ErrorServer::new_at_address(failed_ip).await));
log::info!("Waiting for health checks to realize server is dead..."); log::info!("Waiting for health checks to realize server is dead...");
delay_for(Duration::from_secs(3)).await; sleep(Duration::from_secs(3)).await;
// Make sure we get back successful requests // Make sure we get back successful requests
for i in 0..8 { for i in 0..8 {
@ -209,7 +209,7 @@ async fn test_active_health_checks_restore_failed_upstream() {
upstreams.push(Box::new(EchoServer::new_at_address(failed_ip).await)); upstreams.push(Box::new(EchoServer::new_at_address(failed_ip).await));
log::info!("Waiting a few seconds for the active health check to run..."); log::info!("Waiting a few seconds for the active health check to run...");
delay_for(Duration::from_secs(3)).await; sleep(Duration::from_secs(3)).await;
log::info!("Sending some more requests"); log::info!("Sending some more requests");
for i in 0..5 { for i in 0..5 {

View File

@ -2,7 +2,7 @@ use rand::Rng;
use std::time::Duration; use std::time::Duration;
use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::{Child, Command}; use tokio::process::{Child, Command};
use tokio::time::delay_for; use tokio::time::sleep;
pub struct BalanceBeam { pub struct BalanceBeam {
#[allow(dead_code)] #[allow(dead_code)]
@ -25,7 +25,7 @@ impl BalanceBeam {
max_requests_per_minute: Option<usize>, max_requests_per_minute: Option<usize>,
) -> BalanceBeam { ) -> BalanceBeam {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let address = format!("127.0.0.1:{}", rng.gen_range(1024, 65535)); let address = format!("127.0.0.1:{}", rng.gen_range(1024..65535));
let mut cmd = Command::new(BalanceBeam::target_bin_path()); let mut cmd = Command::new(BalanceBeam::target_bin_path());
cmd.arg("--bind").arg(&address); cmd.arg("--bind").arg(&address);
for upstream in upstreams { for upstream in upstreams {
@ -80,7 +80,7 @@ impl BalanceBeam {
}); });
// Hack: wait for executable to start running // Hack: wait for executable to start running
delay_for(Duration::from_secs(1)).await; sleep(Duration::from_secs(1)).await;
BalanceBeam { child, address } BalanceBeam { child, address }
} }

View File

@ -42,7 +42,7 @@ pub struct EchoServer {
impl EchoServer { impl EchoServer {
pub async fn new() -> EchoServer { pub async fn new() -> EchoServer {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
EchoServer::new_at_address(format!("127.0.0.1:{}", rng.gen_range(1024, 65535))).await EchoServer::new_at_address(format!("127.0.0.1:{}", rng.gen_range(1024..65535))).await
} }
pub async fn new_at_address(bind_addr_string: String) -> EchoServer { pub async fn new_at_address(bind_addr_string: String) -> EchoServer {

View File

@ -30,7 +30,7 @@ impl ErrorServer {
#[allow(dead_code)] #[allow(dead_code)]
pub async fn new() -> ErrorServer { pub async fn new() -> ErrorServer {
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
ErrorServer::new_at_address(format!("127.0.0.1:{}", rng.gen_range(1024, 65535))).await ErrorServer::new_at_address(format!("127.0.0.1:{}", rng.gen_range(1024..65535))).await
} }
#[allow(dead_code)] #[allow(dead_code)]