1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#![warn(
future_incompatible,
nonstandard_style,
rust_2018_idioms,
rust_2021_compatibility
)]
#![allow(clippy::async_yields_async)]
mod bounded_executor;
pub mod metrics;
mod p2p;
mod retry;
mod traits;
pub use crate::{
bounded_executor::BoundedExecutor,
p2p::P2pNetwork,
retry::RetryConfig,
traits::{Lucky, LuckyNetwork, PrimaryToWorkerRpc, ReliableNetwork, UnreliableNetwork},
};
#[derive(Debug)]
#[must_use]
pub struct CancelOnDropHandler<T>(tokio::task::JoinHandle<T>);
impl<T> Drop for CancelOnDropHandler<T> {
fn drop(&mut self) {
self.0.abort();
}
}
impl<T> std::future::Future for CancelOnDropHandler<T> {
type Output = T;
fn poll(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
use futures::future::FutureExt;
self.0.poll_unpin(cx).map(Result::unwrap)
}
}
pub const MAX_TASK_CONCURRENCY: usize = 500_000;
pub fn multiaddr_to_address(
multiaddr: &multiaddr::Multiaddr,
) -> anyhow::Result<anemo::types::Address> {
use multiaddr::Protocol;
let mut iter = multiaddr.iter();
match (iter.next(), iter.next()) {
(Some(Protocol::Ip4(ipaddr)), Some(Protocol::Tcp(port) | Protocol::Udp(port))) => {
Ok((ipaddr, port).into())
}
(Some(Protocol::Ip6(ipaddr)), Some(Protocol::Tcp(port) | Protocol::Udp(port))) => {
Ok((ipaddr, port).into())
}
(Some(Protocol::Dns(hostname)), Some(Protocol::Tcp(port) | Protocol::Udp(port))) => {
Ok((hostname.as_ref(), port).into())
}
_ => Err(anyhow::anyhow!("invalid address")),
}
}