-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04176ae
commit 943c20d
Showing
5 changed files
with
138 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,6 @@ perf.data* | |
|
||
# VSCode | ||
/.vscode | ||
|
||
# cross | ||
/zcross |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
use libp2p::swarm::NetworkBehaviour; | ||
use libp2p::{identify, ping}; | ||
use libp2p::{gossipsub, identify, ping}; | ||
|
||
#[derive(NetworkBehaviour)] | ||
pub struct AnchorBehaviour { | ||
/// Provides IP addresses and peer information. | ||
pub identify: identify::Behaviour, | ||
/// Used for connection health checks. | ||
pub ping: ping::Behaviour, | ||
// /// The routing pub-sub mechanism for Anchor. | ||
// pub gossipsub: gossipsub::Behaviour, | ||
/// The routing pub-sub mechanism for Anchor. | ||
pub gossipsub: gossipsub::Behaviour, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use futures::channel::mpsc::Receiver; | ||
use futures::{future, Future, StreamExt}; | ||
use std::{pin::Pin, task::Context, task::Poll}; | ||
use task_executor::ShutdownReason; | ||
use tokio::signal::windows::{ctrl_c, CtrlC}; | ||
use tracing::error; | ||
|
||
pub(crate) async fn handle_shutdown_signals( | ||
mut signal_rx: Receiver<ShutdownReason>, | ||
) -> Result<ShutdownReason, String> { | ||
let inner_shutdown = async move { | ||
signal_rx | ||
.next() | ||
.await | ||
.ok_or("Internal shutdown channel exhausted") | ||
}; | ||
futures::pin_mut!(inner_shutdown); | ||
|
||
let register_handlers = async { | ||
let mut handles = vec![]; | ||
|
||
// Setup for handling Ctrl+C | ||
match ctrl_c() { | ||
Ok(ctrl_c) => { | ||
let ctrl_c = SignalFuture::new(ctrl_c, "Received Ctrl+C"); | ||
handles.push(ctrl_c); | ||
} | ||
Err(e) => error!(error = ?e, "Could not register Ctrl+C handler"), | ||
} | ||
|
||
future::select(inner_shutdown, future::select_all(handles.into_iter())).await | ||
}; | ||
|
||
match register_handlers.await { | ||
future::Either::Left((Ok(reason), _)) => Ok(reason), | ||
future::Either::Left((Err(e), _)) => Err(e.into()), | ||
future::Either::Right(((res, _, _), _)) => { | ||
res.ok_or_else(|| "Handler channel closed".to_string()) | ||
} | ||
} | ||
} | ||
|
||
struct SignalFuture { | ||
signal: CtrlC, | ||
message: &'static str, | ||
} | ||
|
||
impl SignalFuture { | ||
pub fn new(signal: CtrlC, message: &'static str) -> SignalFuture { | ||
SignalFuture { signal, message } | ||
} | ||
} | ||
|
||
impl Future for SignalFuture { | ||
type Output = Option<ShutdownReason>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
match self.signal.poll_recv(cx) { | ||
Poll::Pending => Poll::Pending, | ||
Poll::Ready(Some(_)) => Poll::Ready(Some(ShutdownReason::Success(self.message))), | ||
Poll::Ready(None) => Poll::Ready(None), | ||
} | ||
} | ||
} |