Skip to content

Commit

Permalink
use try_join
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Woźniak committed Jun 17, 2024
1 parent c1290f7 commit 4c2d1d9
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions crates/starknet-devnet/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::future::IntoFuture;
use std::result::Result::Ok;
use std::time::Duration;

use anyhow::Ok;
use anyhow::Error;
use clap::Parser;
use cli::Args;
use server::api::json_rpc::RPC_SPEC_VERSION;
Expand All @@ -22,7 +23,7 @@ use starknet_types::rpc::state::Balance;
use starknet_types::traits::ToHexString;
use tokio::net::TcpListener;
use tokio::signal::unix::{signal, SignalKind};
use tokio::task;
use tokio::task::{self, JoinHandle};
use tokio::time::interval;
use tracing::{info, warn};
use tracing_subscriber::EnvFilter;
Expand Down Expand Up @@ -168,6 +169,16 @@ pub async fn set_and_log_fork_config(
Ok(())
}

async fn flatten<T, E>(handle: JoinHandle<Result<T, E>>) -> Result<T, Error>
where
E: Into<Error> + Send + 'static,
{
match handle.await {
Ok(result) => result.map_err(Into::into),
Err(join_error) => Err(Error::new(join_error)),
}
}

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
configure_tracing();
Expand Down Expand Up @@ -219,11 +230,7 @@ async fn main() -> Result<(), anyhow::Error> {
shutdown_signal(api.clone()).await;

// join both tasks
let (block_interval_result, server_result) = tokio::join!(block_interval_handle, server_handle);

// handle the results of the tasks
block_interval_result??;
server_result??;
let _ = tokio::try_join!(flatten(block_interval_handle), flatten(server_handle))?;

Ok(())
}
Expand Down

0 comments on commit 4c2d1d9

Please sign in to comment.