1use std::{
22 collections::VecDeque,
23 convert::Infallible,
24 error::Error,
25 fmt, io,
26 task::{Context, Poll},
27 time::Duration,
28};
29
30use futures::{
31 future::{BoxFuture, Either},
32 prelude::*,
33};
34use futures_timer::Delay;
35use libp2p_core::upgrade::ReadyUpgrade;
36use libp2p_swarm::{
37 handler::{ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound},
38 ConnectionHandler, ConnectionHandlerEvent, Stream, StreamProtocol, StreamUpgradeError,
39 SubstreamProtocol,
40};
41
42use crate::{protocol, PROTOCOL_NAME};
43
44#[derive(Debug, Clone)]
46pub struct Config {
47 timeout: Duration,
49 interval: Duration,
51}
52
53impl Config {
54 pub fn new() -> Self {
64 Self {
65 timeout: Duration::from_secs(20),
66 interval: Duration::from_secs(15),
67 }
68 }
69
70 pub fn with_timeout(mut self, d: Duration) -> Self {
72 self.timeout = d;
73 self
74 }
75
76 pub fn with_interval(mut self, d: Duration) -> Self {
78 self.interval = d;
79 self
80 }
81}
82
83impl Default for Config {
84 fn default() -> Self {
85 Self::new()
86 }
87}
88
89#[derive(Debug)]
91pub enum Failure {
92 Timeout,
95 Unsupported,
97 Other {
99 error: Box<dyn std::error::Error + Send + Sync + 'static>,
100 },
101}
102
103impl Failure {
104 fn other(e: impl std::error::Error + Send + Sync + 'static) -> Self {
105 Self::Other { error: Box::new(e) }
106 }
107}
108
109impl fmt::Display for Failure {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 match self {
112 Failure::Timeout => f.write_str("Ping timeout"),
113 Failure::Other { error } => write!(f, "Ping error: {error}"),
114 Failure::Unsupported => write!(f, "Ping protocol not supported"),
115 }
116 }
117}
118
119impl Error for Failure {
120 fn source(&self) -> Option<&(dyn Error + 'static)> {
121 match self {
122 Failure::Timeout => None,
123 Failure::Other { error } => Some(&**error),
124 Failure::Unsupported => None,
125 }
126 }
127}
128
129pub struct Handler {
132 config: Config,
134 interval: Delay,
136 pending_errors: VecDeque<Failure>,
138 failures: u32,
142 outbound: Option<OutboundState>,
144 inbound: Option<PongFuture>,
148 state: State,
150}
151
152#[derive(Debug, Clone, Copy, PartialEq, Eq)]
153enum State {
154 Inactive {
156 reported: bool,
160 },
161 Active,
163}
164
165impl Handler {
166 pub fn new(config: Config) -> Self {
168 Handler {
169 config,
170 interval: Delay::new(Duration::new(0, 0)),
171 pending_errors: VecDeque::with_capacity(2),
172 failures: 0,
173 outbound: None,
174 inbound: None,
175 state: State::Active,
176 }
177 }
178
179 fn on_dial_upgrade_error(
180 &mut self,
181 DialUpgradeError { error, .. }: DialUpgradeError<
182 (),
183 <Self as ConnectionHandler>::OutboundProtocol,
184 >,
185 ) {
186 self.outbound = None; self.interval.reset(Duration::new(0, 0));
199
200 let error = match error {
201 StreamUpgradeError::NegotiationFailed => {
202 debug_assert_eq!(self.state, State::Active);
203
204 self.state = State::Inactive { reported: false };
205 return;
206 }
207 StreamUpgradeError::Timeout => Failure::Other {
209 error: Box::new(std::io::Error::new(
210 std::io::ErrorKind::TimedOut,
211 "ping protocol negotiation timed out",
212 )),
213 },
214 StreamUpgradeError::Apply(e) => libp2p_core::util::unreachable(e),
215 StreamUpgradeError::Io(e) => Failure::Other { error: Box::new(e) },
216 };
217
218 self.pending_errors.push_front(error);
219 }
220}
221
222impl ConnectionHandler for Handler {
223 type FromBehaviour = Infallible;
224 type ToBehaviour = Result<Duration, Failure>;
225 type InboundProtocol = ReadyUpgrade<StreamProtocol>;
226 type OutboundProtocol = ReadyUpgrade<StreamProtocol>;
227 type OutboundOpenInfo = ();
228 type InboundOpenInfo = ();
229
230 fn listen_protocol(&self) -> SubstreamProtocol<ReadyUpgrade<StreamProtocol>> {
231 SubstreamProtocol::new(ReadyUpgrade::new(PROTOCOL_NAME), ())
232 }
233
234 fn on_behaviour_event(&mut self, _: Infallible) {}
235
236 #[tracing::instrument(level = "trace", name = "ConnectionHandler::poll", skip(self, cx))]
237 fn poll(
238 &mut self,
239 cx: &mut Context<'_>,
240 ) -> Poll<ConnectionHandlerEvent<ReadyUpgrade<StreamProtocol>, (), Result<Duration, Failure>>>
241 {
242 match self.state {
243 State::Inactive { reported: true } => {
244 return Poll::Pending; }
246 State::Inactive { reported: false } => {
247 self.state = State::Inactive { reported: true };
248 return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Err(
249 Failure::Unsupported,
250 )));
251 }
252 State::Active => {}
253 }
254
255 if let Some(fut) = self.inbound.as_mut() {
257 match fut.poll_unpin(cx) {
258 Poll::Pending => {}
259 Poll::Ready(Err(e)) => {
260 tracing::debug!("Inbound ping error: {:?}", e);
261 self.inbound = None;
262 }
263 Poll::Ready(Ok(stream)) => {
264 tracing::trace!("answered inbound ping from peer");
265
266 self.inbound = Some(protocol::recv_ping(stream).boxed());
268 }
269 }
270 }
271
272 loop {
273 if let Some(error) = self.pending_errors.pop_back() {
275 tracing::debug!("Ping failure: {:?}", error);
276
277 self.failures += 1;
278
279 if self.failures > 1 {
285 return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Err(error)));
286 }
287 }
288
289 match self.outbound.take() {
291 Some(OutboundState::Ping(mut ping)) => match ping.poll_unpin(cx) {
292 Poll::Pending => {
293 self.outbound = Some(OutboundState::Ping(ping));
294 break;
295 }
296 Poll::Ready(Ok((stream, rtt))) => {
297 tracing::debug!(?rtt, "ping succeeded");
298 self.failures = 0;
299 self.interval.reset(self.config.interval);
300 self.outbound = Some(OutboundState::Idle(stream));
301 return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Ok(rtt)));
302 }
303 Poll::Ready(Err(e)) => {
304 self.interval.reset(self.config.interval);
305 self.pending_errors.push_front(e);
306 }
307 },
308 Some(OutboundState::Idle(stream)) => match self.interval.poll_unpin(cx) {
309 Poll::Pending => {
310 self.outbound = Some(OutboundState::Idle(stream));
311 break;
312 }
313 Poll::Ready(()) => {
314 self.outbound = Some(OutboundState::Ping(
315 send_ping(stream, self.config.timeout).boxed(),
316 ));
317 }
318 },
319 Some(OutboundState::OpenStream) => {
320 self.outbound = Some(OutboundState::OpenStream);
321 break;
322 }
323 None => match self.interval.poll_unpin(cx) {
324 Poll::Pending => break,
325 Poll::Ready(()) => {
326 self.outbound = Some(OutboundState::OpenStream);
327 let protocol = SubstreamProtocol::new(ReadyUpgrade::new(PROTOCOL_NAME), ());
328 return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest {
329 protocol,
330 });
331 }
332 },
333 }
334 }
335
336 Poll::Pending
337 }
338
339 fn on_connection_event(
340 &mut self,
341 event: ConnectionEvent<Self::InboundProtocol, Self::OutboundProtocol>,
342 ) {
343 match event {
344 ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
345 protocol: mut stream,
346 ..
347 }) => {
348 stream.ignore_for_keep_alive();
349 self.inbound = Some(protocol::recv_ping(stream).boxed());
350 }
351 ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
352 protocol: mut stream,
353 ..
354 }) => {
355 stream.ignore_for_keep_alive();
356 self.outbound = Some(OutboundState::Ping(
357 send_ping(stream, self.config.timeout).boxed(),
358 ));
359 }
360 ConnectionEvent::DialUpgradeError(dial_upgrade_error) => {
361 self.on_dial_upgrade_error(dial_upgrade_error)
362 }
363 _ => {}
364 }
365 }
366}
367
368type PingFuture = BoxFuture<'static, Result<(Stream, Duration), Failure>>;
369type PongFuture = BoxFuture<'static, Result<Stream, io::Error>>;
370
371enum OutboundState {
373 OpenStream,
375 Idle(Stream),
377 Ping(PingFuture),
379}
380
381async fn send_ping(stream: Stream, timeout: Duration) -> Result<(Stream, Duration), Failure> {
383 let ping = protocol::send_ping(stream);
384 futures::pin_mut!(ping);
385
386 match future::select(ping, Delay::new(timeout)).await {
387 Either::Left((Ok((stream, rtt)), _)) => Ok((stream, rtt)),
388 Either::Left((Err(e), _)) => Err(Failure::other(e)),
389 Either::Right(((), _)) => Err(Failure::Timeout),
390 }
391}