Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix clap version #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion proj-2/balancebeam/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = "3.0.0-beta.1"
clap = { version = "4.5.7", features = ["derive"] }
httparse = "1.3"
http = "0.2"
log = "0.4"
Expand Down
51 changes: 28 additions & 23 deletions proj-2/balancebeam/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
mod request;
mod response;

use clap::Clap;
use clap::Parser;
use rand::{Rng, SeedableRng};
use std::net::{TcpListener, TcpStream};

/// 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.
#[derive(Clap, Debug)]
#[clap(about = "Fun with load balancing")]
#[derive(Parser, Debug)]
#[command(about = "Fun with load balancing")]
struct CmdOptions {
#[clap(
short,
long,
about = "IP/port to bind to",
default_value = "0.0.0.0:1100"
)]
// IP/port to bind to
// #[arg(short, long, default_value_t = ("0.0.0.0:1100".to_string()))]
#[arg(short, long, help = "IP/port to bind to", default_value_t = String::from("0.0.0.0:1100"))]
bind: String,
#[clap(short, long, about = "Upstream host to forward requests to")]
// Upstream host to forward requests to
#[arg(short, long, help = "Upstream host to forward requests to")]
upstream: Vec<String>,
#[clap(
// Perform active health checks on this interval (in seconds)
#[arg(
long,
about = "Perform active health checks on this interval (in seconds)",
default_value = "10"
help = "Perform active health checks on this interval (in seconds)",
default_value_t = 10
)]
active_health_check_interval: usize,
#[clap(
long,
about = "Path to send request to for active health checks",
default_value = "/"
)]
// Path to send request to for active health checks
#[arg(long, help = "Path to send request to for active health checks",default_value_t = String::from("/"))]
active_health_check_path: String,
#[clap(
// Maximum number of requests to accept per IP per minute (0 = unlimited)
#[arg(
long,
about = "Maximum number of requests to accept per IP per minute (0 = unlimited)",
default_value = "0"
help = "Maximum number of requests to accept per IP per minute (0 = unlimited)",
default_value_t = 0
)]
max_requests_per_minute: usize,
}
Expand Down Expand Up @@ -111,7 +108,11 @@ fn connect_to_upstream(state: &ProxyState) -> Result<TcpStream, std::io::Error>

fn send_response(client_conn: &mut TcpStream, response: &http::Response<Vec<u8>>) {
let client_ip = client_conn.peer_addr().unwrap().ip().to_string();
log::info!("{} <- {}", client_ip, response::format_response_line(&response));
log::info!(
"{} <- {}",
client_ip,
response::format_response_line(&response)
);
if let Err(error) = response::write_to_stream(&response, client_conn) {
log::warn!("Failed to send response to client: {}", error);
return;
Expand Down Expand Up @@ -177,7 +178,11 @@ fn handle_connection(mut client_conn: TcpStream, state: &ProxyState) {

// Forward the request to the server
if let Err(error) = request::write_to_stream(&request, &mut upstream_conn) {
log::error!("Failed to send request to upstream {}: {}", upstream_ip, error);
log::error!(
"Failed to send request to upstream {}: {}",
upstream_ip,
error
);
let response = response::make_http_error(http::StatusCode::BAD_GATEWAY);
send_response(&mut client_conn, &response);
return;
Expand Down