Skip to content

Commit

Permalink
flatten ProxyRequestOrder variants into RequestContent
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Mar 8, 2023
1 parent efb9c5d commit 7759984
Show file tree
Hide file tree
Showing 37 changed files with 864 additions and 930 deletions.
34 changes: 16 additions & 18 deletions bin/src/acme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sozu_command_lib::{
config::Config,
worker::{
AddCertificate, Backend, CertificateAndKey, CertificateFingerprint, HttpFrontend, PathRule,
ProxyRequestOrder, RemoveBackend, ReplaceCertificate, Route, RulePosition, TlsVersion,
RemoveBackend, ReplaceCertificate, Route, RulePosition, TlsVersion,
},
};
use std::{fs::File, io::Write, iter, net::SocketAddr, thread, time};
Expand Down Expand Up @@ -284,7 +284,7 @@ fn set_up_proxying(
path_begin: &str,
server_address: SocketAddr,
) -> anyhow::Result<()> {
let add_http_front = ProxyRequestOrder::AddHttpFrontend(HttpFrontend {
let add_http_front = RequestContent::AddHttpFrontend(HttpFrontend {
route: Route::ClusterId(cluster_id.to_owned()),
hostname: String::from(hostname),
address: *frontend,
Expand All @@ -296,7 +296,7 @@ fn set_up_proxying(

order_command(channel, add_http_front).with_context(|| "Order AddHttpFront failed")?;

let add_backend = ProxyRequestOrder::AddBackend(Backend {
let add_backend = RequestContent::AddBackend(Backend {
cluster_id: String::from(cluster_id),
backend_id: format!("{cluster_id}-0"),
address: server_address,
Expand All @@ -317,7 +317,7 @@ fn remove_proxying(
path_begin: &str,
server_address: SocketAddr,
) -> anyhow::Result<()> {
let remove_http_front_order = ProxyRequestOrder::RemoveHttpFrontend(HttpFrontend {
let remove_http_front_order = RequestContent::RemoveHttpFrontend(HttpFrontend {
route: Route::ClusterId(cluster_id.to_owned()),
address: *frontend,
hostname: String::from(hostname),
Expand All @@ -329,7 +329,7 @@ fn remove_proxying(
order_command(channel, remove_http_front_order)
.with_context(|| "RemoveHttpFront order failed")?;

let remove_backend_order = ProxyRequestOrder::RemoveBackend(RemoveBackend {
let remove_backend_order = RequestContent::RemoveBackend(RemoveBackend {
cluster_id: String::from(cluster_id),
backend_id: format!("{cluster_id}-0"),
address: server_address,
Expand Down Expand Up @@ -359,7 +359,7 @@ fn add_certificate(
.with_context(|| "could not load certificate chain".to_string())?;

let certificate_order = match old_fingerprint {
None => ProxyRequestOrder::AddCertificate(AddCertificate {
None => RequestContent::AddCertificate(AddCertificate {
address: *frontend,
certificate: CertificateAndKey {
certificate,
Expand All @@ -371,7 +371,7 @@ fn add_certificate(
expired_at: None,
}),

Some(f) => ProxyRequestOrder::ReplaceCertificate(ReplaceCertificate {
Some(f) => RequestContent::ReplaceCertificate(ReplaceCertificate {
address: *frontend,
new_certificate: CertificateAndKey {
certificate,
Expand All @@ -392,14 +392,11 @@ fn add_certificate(

fn order_command(
channel: &mut Channel<ClientRequest, CommandResponse>,
order: ProxyRequestOrder,
order: RequestContent,
) -> anyhow::Result<()> {
let id = generate_id();
channel
.write_message(&ClientRequest::new(
id.clone(),
RequestContent::Proxy(Box::new(order.clone())),
))
.write_message(&ClientRequest::new(id.clone(), order.clone()))
.with_context(|| "Could not write message on the channel")?;

loop {
Expand All @@ -419,23 +416,24 @@ fn order_command(
bail!("could not execute order: {}", response.message);
}
CommandStatus::Ok => {
// TODO: remove the pattern matching and only display the response message
match order {
ProxyRequestOrder::AddBackend(_) => {
RequestContent::AddBackend(_) => {
info!("backend added : {}", response.message)
}
ProxyRequestOrder::RemoveBackend(_) => {
RequestContent::RemoveBackend(_) => {
info!("backend removed : {} ", response.message)
}
ProxyRequestOrder::AddCertificate(_) => {
RequestContent::AddCertificate(_) => {
info!("certificate added: {}", response.message)
}
ProxyRequestOrder::RemoveCertificate(_) => {
RequestContent::RemoveCertificate(_) => {
info!("certificate removed: {}", response.message)
}
ProxyRequestOrder::AddHttpFrontend(_) => {
RequestContent::AddHttpFrontend(_) => {
info!("front added: {}", response.message)
}
ProxyRequestOrder::RemoveHttpFrontend(_) => {
RequestContent::RemoveHttpFrontend(_) => {
info!("front removed: {}", response.message)
}
_ => {
Expand Down
51 changes: 25 additions & 26 deletions bin/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use sozu_command_lib::{
scm_socket::{Listeners, ScmSocket},
state::ConfigState,
worker::{
MetricsConfiguration, ProxyRequest, ProxyRequestOrder, ProxyResponse, ProxyResponseContent,
MetricsConfiguration, ProxyRequest, ProxyResponse, ProxyResponseContent,
ProxyResponseStatus,
},
};
Expand Down Expand Up @@ -531,33 +531,32 @@ impl CommandServer {

//FIXME: too many loops, this could be cleaner
for message in self.config.generate_config_messages() {
if let RequestContent::Proxy(order) = message.content {
if let Err(e) = self.state.dispatch(&order) {
error!("Could not execute order on state: {:#}", e);
}
let order = message.content;
if let Err(e) = self.state.dispatch(&order) {
error!("Could not execute order on state: {:#}", e);
}

if let &ProxyRequestOrder::AddCertificate(_) = &*order {
debug!("config generated AddCertificate( ... )");
} else {
debug!("config generated {:?}", order);
}
if let &RequestContent::AddCertificate(_) = &order {
debug!("config generated AddCertificate( ... )");
} else {
debug!("config generated {:?}", order);
}

let mut count = 0usize;
for ref mut worker in self.workers.iter_mut().filter(|worker| {
worker.run_state != RunState::Stopping && worker.run_state != RunState::Stopped
}) {
worker.send(message.id.clone(), *order.clone()).await;
count += 1;
}
let mut count = 0usize;
for ref mut worker in self.workers.iter_mut().filter(|worker| {
worker.run_state != RunState::Stopping && worker.run_state != RunState::Stopped
}) {
worker.send(message.id.clone(), order.clone()).await;
count += 1;
}

if count == 0 {
// FIXME: should send back error here
error!("no worker found");
} else {
self.in_flight
.insert(message.id.clone(), (tx.clone(), count));
total_message_count += count;
}
if count == 0 {
// FIXME: should send back error here
error!("no worker found");
} else {
self.in_flight
.insert(message.id.clone(), (tx.clone(), count));
total_message_count += count;
}
}

Expand Down Expand Up @@ -688,7 +687,7 @@ impl CommandServer {
new_worker
.send(
format!("RESTART-{new_worker_id}-STATUS"),
ProxyRequestOrder::Status,
RequestContent::Status,
)
.await;

Expand Down

0 comments on commit 7759984

Please sign in to comment.