Skip to content

Commit

Permalink
added error management in the connection to the socket
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj authored and FlorentinDUBOIS committed Jul 13, 2022
1 parent 01ec16c commit 53f6911
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 16 additions & 13 deletions bin/src/ctl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod command;
use crate::cli;
use crate::util;
use crate::{get_config_file_path, load_configuration};
use anyhow::Context;
use sozu_command::channel::Channel;
use sozu_command::command::{CommandRequest, CommandResponse};
use sozu_command::config::Config;
Expand Down Expand Up @@ -34,7 +35,8 @@ pub fn ctl(matches: cli::Sozu) -> Result<(), anyhow::Error> {
std::process::exit(0);
}

let channel = create_channel(&config).expect("could not connect to the command unix socket");
let channel =
create_channel(&config).with_context(|| "could not connect to the command unix socket")?;
let timeout: u64 = matches.timeout.unwrap_or(config.ctl_command_timeout);

match matches.cmd {
Expand Down Expand Up @@ -178,9 +180,12 @@ pub fn ctl(matches: cli::Sozu) -> Result<(), anyhow::Error> {
remove_tcp_frontend(channel, timeout, &id, address)
}
},
FrontendCmd::List { http, https, tcp, domain } => {
list_frontends(channel, timeout, http, https, tcp, domain)
}
FrontendCmd::List {
http,
https,
tcp,
domain,
} => list_frontends(channel, timeout, http, https, tcp, domain),
},
SubCmd::Listener { cmd } => match cmd {
ListenerCmd::Http { cmd } => match cmd {
Expand Down Expand Up @@ -331,16 +336,14 @@ pub fn ctl(matches: cli::Sozu) -> Result<(), anyhow::Error> {
}
}

pub fn create_channel(
config: &Config,
) -> Result<Channel<CommandRequest, CommandResponse>, io::Error> {
Channel::from_path(
&config.command_socket_path(),
pub fn create_channel(config: &Config) -> anyhow::Result<Channel<CommandRequest, CommandResponse>> {
let mut channel = Channel::from_path(
&config.command_socket_path()?,
config.command_buffer_size,
config.max_command_buffer_size,
)
.and_then(|mut channel| {
channel.set_nonblocking(false);
Ok(channel)
})
.with_context(|| "Could not create Channel from the given path")?;

channel.set_nonblocking(false);
Ok(channel)
}
2 changes: 1 addition & 1 deletion bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn start(matches: &cli::Sozu) -> Result<(), anyhow::Error> {
set_workers_affinity(&workers);
}

let command_socket_path = config.command_socket_path();
let command_socket_path = config.command_socket_path()?;

command::start(config, command_socket_path, workers).with_context(|| "could not start Sozu")?;

Expand Down
1 change: 1 addition & 0 deletions command/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ include = [
]

[dependencies]
anyhow = "^1.0.42"
hex = "^0.4"
log = "^0.4"
pem = "^0.8"
Expand Down
22 changes: 11 additions & 11 deletions command/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! parsing data from the configuration file
use anyhow::{bail, Context};
use std::collections::{HashMap, HashSet};
use std::env;
use std::fs::File;
Expand Down Expand Up @@ -1251,27 +1252,26 @@ impl Config {
v
}

pub fn command_socket_path(&self) -> String {
pub fn command_socket_path(&self) -> anyhow::Result<String> {
let config_path_buf = PathBuf::from(self.config_path.clone());
let mut config_folder = config_path_buf
.parent()
.expect("could not get parent folder of configuration file")
.to_path_buf();
let mut config_folder = match config_path_buf.parent() {
Some(path) => path.to_path_buf(),
None => bail!("could not get parent folder of configuration file"),
};

let socket_path = PathBuf::from(self.command_socket.clone());
let mut parent = match socket_path.parent() {
None => config_folder,
Some(path) => {
config_folder.push(path);
match config_folder.canonicalize() {
Ok(path) => path,
Err(e) => panic!("could not get command socket folder path: {}", e),
}
config_folder.canonicalize().with_context(|| {
format!("could not get command socket folder path: {:?}", path)
})?
}
};

let path = match socket_path.file_name() {
None => panic!("could not get command socket file name"),
None => bail!("could not get command socket file name"),
Some(f) => {
parent.push(f);
parent
Expand All @@ -1280,7 +1280,7 @@ impl Config {

path.to_str()
.map(|s| s.to_string())
.expect("could not parse command socket path")
.with_context(|| "could not parse command socket path")
}

pub fn saved_state_path(&self) -> Option<String> {
Expand Down

0 comments on commit 53f6911

Please sign in to comment.