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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::transport::{ConnectionInfo, IListener, ITransport, ListenerEvent, TransportListener};
use crate::{transport::TransportError, Multiaddr, Transport};
use async_trait::async_trait;
use futures::future::{select, Either};
use futures_timer::Delay;
use log::trace;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct TransportTimeout<InnerTrans> {
inner: InnerTrans,
outgoing_timeout: Duration,
incoming_timeout: Duration,
}
impl<InnerTrans> TransportTimeout<InnerTrans> {
pub fn new(trans: InnerTrans, timeout: Duration) -> Self {
TransportTimeout {
inner: trans,
outgoing_timeout: timeout,
incoming_timeout: timeout,
}
}
pub fn with_outgoing_timeout(trans: InnerTrans, timeout: Duration) -> Self {
TransportTimeout {
inner: trans,
outgoing_timeout: timeout,
incoming_timeout: Duration::from_secs(100 * 365 * 24 * 3600),
}
}
pub fn with_ingoing_timeout(trans: InnerTrans, timeout: Duration) -> Self {
TransportTimeout {
inner: trans,
outgoing_timeout: Duration::from_secs(100 * 365 * 24 * 3600),
incoming_timeout: timeout,
}
}
}
#[async_trait]
impl<InnerTrans> Transport for TransportTimeout<InnerTrans>
where
InnerTrans: Transport + Clone + 'static,
InnerTrans::Output: ConnectionInfo + 'static,
{
type Output = InnerTrans::Output;
fn listen_on(&mut self, addr: Multiaddr) -> Result<IListener<Self::Output>, TransportError> {
let listener = self.inner.listen_on(addr)?;
let listener = TimeoutListener {
inner: listener,
timeout: self.incoming_timeout,
};
Ok(Box::new(listener))
}
async fn dial(&mut self, addr: Multiaddr) -> Result<Self::Output, TransportError> {
let output = select(self.inner.dial(addr), Delay::new(self.outgoing_timeout)).await;
match output {
Either::Left((stream, _)) => {
trace!("dialing connected first");
Ok(stream?)
}
Either::Right(_) => {
trace!("dialing timeout first");
Err(TransportError::Timeout)
}
}
}
fn box_clone(&self) -> ITransport<Self::Output> {
Box::new(self.clone())
}
fn protocols(&self) -> Vec<u32> {
self.inner.protocols()
}
}
pub struct TimeoutListener<TOutput> {
inner: IListener<TOutput>,
timeout: Duration,
}
#[async_trait]
impl<TOutput: Send> TransportListener for TimeoutListener<TOutput> {
type Output = TOutput;
async fn accept(&mut self) -> Result<ListenerEvent<Self::Output>, TransportError> {
let output = select(self.inner.accept(), Delay::new(self.timeout)).await;
match output {
Either::Left((r, _)) => {
trace!("accepted first");
r
}
Either::Right(_) => {
trace!("accept timeout first");
Err(TransportError::Timeout)
}
}
}
fn multi_addr(&self) -> Option<&Multiaddr> {
self.inner.multi_addr()
}
}
#[cfg(test)]
mod tests {
use crate::transport::memory::MemoryTransport;
use crate::{Multiaddr, Transport};
use libp2prs_runtime::task;
use std::time::Duration;
#[test]
fn dialer_and_listener_timeout() {
fn test1(addr: Multiaddr) {
task::block_on(async move {
let mut timeout_listener = MemoryTransport::default().timeout(Duration::from_secs(1)).listen_on(addr).unwrap();
assert!(timeout_listener.accept().await.is_err());
});
}
fn test2(addr: Multiaddr) {
task::block_on(async move {
let mut tcp = MemoryTransport::default().timeout(Duration::from_secs(1));
assert!(tcp.dial(addr.clone()).await.is_err());
});
}
test1("/memory/1111".parse().unwrap());
test2("/memory/1111".parse().unwrap());
}
}