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: remove unneeded termination handler #30

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
removed termination and fixed missing shutdown signal handling
  • Loading branch information
ksrichard committed Aug 16, 2024
commit 8bf60d2b5593b359636b21cfde3f52b8434e9ecf
34 changes: 1 addition & 33 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ hex = "0.4.3"
serde_json = "1.0.122"
hickory-resolver = { version = "*", features = ["dns-over-rustls"] }
convert_case = "0.6.0"
ctrlc = { version = "3.4.5", features = ["termination"] }

[package.metadata.cargo-machete]
ignored = ["log4rs"]
5 changes: 1 addition & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ mod sharechain;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut cli_shutdown = Shutdown::new();
let cli_shutdown_signal = cli_shutdown.to_signal();
ctrlc::set_handler(move || cli_shutdown.trigger()).expect("Error setting termination handler");
Cli::parse().handle_command(cli_shutdown_signal).await?;
Cli::parse().handle_command(Shutdown::new().to_signal()).await?;
Ok(())
}
7 changes: 5 additions & 2 deletions src/server/grpc/base_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use minotari_app_grpc::{
ValueAtHeightResponse,
},
};
use tari_shutdown::ShutdownSignal;
use tokio::sync::Mutex;
use tonic::{transport::Channel, Request, Response, Status, Streaming};

Expand Down Expand Up @@ -100,9 +101,11 @@ pub struct TariBaseNodeGrpc {
}

impl TariBaseNodeGrpc {
pub async fn new(base_node_address: String) -> Result<Self, Error> {
pub async fn new(base_node_address: String, shutdown_signal: ShutdownSignal) -> Result<Self, Error> {
Ok(Self {
client: Arc::new(Mutex::new(util::connect_base_node(base_node_address).await?)),
client: Arc::new(Mutex::new(
util::connect_base_node(base_node_address, shutdown_signal).await?,
)),
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/server/grpc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use thiserror::Error;
pub enum Error {
#[error("Tonic error: {0}")]
Tonic(#[from] TonicError),
#[error("Shutdown")]
Shutdown,
}

#[derive(Error, Debug)]
Expand Down
6 changes: 5 additions & 1 deletion src/server/grpc/p2pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use minotari_app_grpc::tari_rpc::{
SubmitBlockRequest, SubmitBlockResponse,
};
use tari_core::proof_of_work::sha3x_difficulty;
use tari_shutdown::ShutdownSignal;
use tari_utilities::hex::Hex;
use tokio::sync::Mutex;
use tonic::{Request, Response, Status};
Expand Down Expand Up @@ -53,9 +54,12 @@ where
p2p_client: p2p::ServiceClient,
share_chain: Arc<S>,
stats_store: Arc<StatsStore>,
shutdown_signal: ShutdownSignal,
) -> Result<Self, Error> {
Ok(Self {
client: Arc::new(Mutex::new(util::connect_base_node(base_node_address).await?)),
client: Arc::new(Mutex::new(
util::connect_base_node(base_node_address, shutdown_signal).await?,
)),
p2p_client,
share_chain,
stats_store,
Expand Down
16 changes: 15 additions & 1 deletion src/server/grpc/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ use std::time::Duration;
use log::error;
use minotari_app_grpc::tari_rpc::base_node_client::BaseNodeClient;
use minotari_node_grpc_client::BaseNodeGrpcClient;
use tari_shutdown::ShutdownSignal;
use tokio::select;
use tokio::time::sleep;
use tonic::transport::Channel;

use crate::server::grpc::error::{Error, TonicError};

/// Utility function to connect to a Base node and try infinitely when it fails until gets connected.
pub async fn connect_base_node(base_node_address: String) -> Result<BaseNodeClient<Channel>, Error> {
pub async fn connect_base_node(
base_node_address: String,
shutdown_signal: ShutdownSignal,
) -> Result<BaseNodeClient<Channel>, Error> {
let client_result = BaseNodeGrpcClient::connect(base_node_address.clone())
.await
.map_err(|e| Error::Tonic(TonicError::Transport(e)));
Expand All @@ -21,6 +26,7 @@ pub async fn connect_base_node(base_node_address: String) -> Result<BaseNodeClie
Err(error) => {
error!("[Retry] Failed to connect to Tari base node: {:?}", error.to_string());
let mut client = None;
tokio::pin!(shutdown_signal);
while client.is_none() {
sleep(Duration::from_secs(5)).await;
match BaseNodeGrpcClient::connect(base_node_address.clone())
Expand All @@ -30,6 +36,14 @@ pub async fn connect_base_node(base_node_address: String) -> Result<BaseNodeClie
Ok(curr_client) => client = Some(curr_client),
Err(error) => error!("[Retry] Failed to connect to Tari base node: {:?}", error.to_string()),
}
select! {
() = &mut shutdown_signal => {
return Err(Error::Shutdown);
}
else => {
continue;
}
}
}
client.unwrap()
},
Expand Down
8 changes: 5 additions & 3 deletions src/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ where
let mut base_node_grpc_server = None;
let mut p2pool_server = None;
if config.mining_enabled {
let base_node_grpc_service = TariBaseNodeGrpc::new(config.base_node_address.clone())
.await
.map_err(Error::Grpc)?;
let base_node_grpc_service =
TariBaseNodeGrpc::new(config.base_node_address.clone(), shutdown_signal.clone())
.await
.map_err(Error::Grpc)?;
base_node_grpc_server = Some(BaseNodeServer::new(base_node_grpc_service));

let p2pool_grpc_service = ShaP2PoolGrpc::new(
config.base_node_address.clone(),
p2p_service.client(),
share_chain.clone(),
stats_store.clone(),
shutdown_signal.clone(),
)
.await
.map_err(Error::Grpc)?;
Expand Down
Loading