Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
fix: Improve error management and reporting when using unix domain so…
Browse files Browse the repository at this point in the history
…ckets (#228)
  • Loading branch information
fabricedesre authored Sep 7, 2022
1 parent 82a1b82 commit 8d13e4f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
10 changes: 6 additions & 4 deletions iroh-gateway/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ impl Core {
bad_bits: Arc<Option<RwLock<BadBits>>>,
) -> anyhow::Result<Self> {
tokio::spawn(async move {
// TODO: handle error
rpc::new(rpc_addr, Gateway::default()).await
if let Err(err) = rpc::new(rpc_addr, Gateway::default()).await {
tracing::error!("Failed to run gateway rpc handler: {}", err);
}
});
let rpc_client = RpcClient::new(config.rpc_client().clone()).await?;
let mut templates = HashMap::new();
Expand All @@ -58,8 +59,9 @@ impl Core {
state: Arc<State>,
) -> anyhow::Result<Self> {
tokio::spawn(async move {
// TODO: handle error
rpc::new(rpc_addr, Gateway::default()).await
if let Err(err) = rpc::new(rpc_addr, Gateway::default()).await {
tracing::error!("Failed to run gateway rpc handler: {}", err);
}
});
Ok(Self { state })
}
Expand Down
12 changes: 6 additions & 6 deletions iroh-one/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ impl Config {
}
}

/// When running in ipfsd mode, the resolver will use memory channels to
/// When running in single binary mode, the resolver will use memory channels to
/// communicate with the p2p and store modules.
/// The gateway itself is exposing a UDS rpc endpoint to be also usable
/// as a single entry point for other system services if feature enabled.
pub fn default_ipfsd() -> RpcClientConfig {
pub fn default_rpc_config() -> RpcClientConfig {
#[cfg(feature = "uds-gateway")]
let path: PathBuf = TempDir::new("iroh").unwrap().path().join("ipfsd.http");

Expand Down Expand Up @@ -92,14 +92,14 @@ impl Default for Config {
fn default() -> Self {
#[cfg(feature = "uds-gateway")]
let gateway_uds_path: PathBuf = TempDir::new("iroh").unwrap().path().join("ipfsd.http");
let ipfsd = Self::default_ipfsd();
let rpc_client = Self::default_rpc_config();
let metrics_config = MetricsConfig::default();
Self {
rpc_client: ipfsd.clone(),
rpc_client: rpc_client.clone(),
metrics: metrics_config.clone(),
gateway: iroh_gateway::config::Config::default(),
store: default_store_config(ipfsd.clone(), metrics_config.clone()),
p2p: default_p2p_config(ipfsd, metrics_config),
store: default_store_config(rpc_client.clone(), metrics_config.clone()),
p2p: default_p2p_config(rpc_client, metrics_config),
#[cfg(feature = "uds-gateway")]
gateway_uds_path: Some(gateway_uds_path),
}
Expand Down
14 changes: 12 additions & 2 deletions iroh-one/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,20 @@ async fn main() -> Result<()> {
let mut path = TempDir::new("iroh")?.path().join("ipfsd.http");
if let Some(uds_path) = config.gateway_uds_path {
path = uds_path;
} else {
// Create the parent path when using the default value since it's likely
// it won't exist yet.
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(&parent);
}
}
let uds_server = uds::uds_server(shared_state, path);

tokio::spawn(async move {
uds_server.await.unwrap();
if let Some(uds_server) = uds::uds_server(shared_state, path) {
if let Err(err) = uds_server.await {
tracing::error!("Failure in http uds handler: {}", err);
}
}
})
};

Expand Down
31 changes: 23 additions & 8 deletions iroh-one/src/uds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,29 @@ impl connect_info::Connected<&UnixStream> for UdsConnectInfo {
pub fn uds_server(
state: Arc<State>,
path: PathBuf,
) -> Server<
ServerAccept,
axum::extract::connect_info::IntoMakeServiceWithConnectInfo<Router, UdsConnectInfo>,
) -> Option<
Server<
ServerAccept,
axum::extract::connect_info::IntoMakeServiceWithConnectInfo<Router, UdsConnectInfo>,
>,
> {
let _ = std::fs::remove_file(&path);
let uds = UnixListener::bind(&path).unwrap();
println!("Binding to UDS at {}", path.display());
let app = get_app_routes(&state);
Server::builder(ServerAccept { uds })
.serve(app.into_make_service_with_connect_info::<UdsConnectInfo>())
match UnixListener::bind(&path) {
Ok(uds) => {
tracing::debug!("Binding to UDS at {}", path.display());
let app = get_app_routes(&state);
Some(
Server::builder(ServerAccept { uds })
.serve(app.into_make_service_with_connect_info::<UdsConnectInfo>()),
)
}
Err(err) => {
tracing::error!(
"Failed to bind http uds socket at {}: {}",
path.display(),
err
);
None
}
}
}
9 changes: 9 additions & 0 deletions iroh-rpc-types/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ macro_rules! proxy {
anyhow::bail!("cannot bind socket: already exists: {}", path.display());
}
}

// If the parent directory doesn't exist, we'll fail to bind.
// Create a more precise error to recognize that case.
if let Some(parent) = path.parent() {
if !parent.exists() {
anyhow::bail!("socket parent directory doesn't exist: {}", parent.display());
}
}

// Delete file on close
struct UdsGuard(std::path::PathBuf);
impl Drop for UdsGuard {
Expand Down

0 comments on commit 8d13e4f

Please sign in to comment.