Skip to content

Commit

Permalink
Network: use wasm_timer for request timeouts, closes #2311
Browse files Browse the repository at this point in the history
  • Loading branch information
styppo committed Mar 18, 2024
1 parent 26a5042 commit 2524dee
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions network-libp2p/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
use async_trait::async_trait;
use base64::Engine;
use bytes::Bytes;
use futures::{future::BoxFuture, ready, stream::BoxStream, Stream, StreamExt};
use futures::{future::BoxFuture, ready, stream::BoxStream, Stream, StreamExt, TryFutureExt};
#[cfg(not(feature = "tokio-time"))]
use instant::Instant;
#[cfg(all(target_family = "wasm", not(feature = "tokio-websocket")))]
Expand Down Expand Up @@ -57,12 +57,9 @@ use nimiq_utils::tagged_signing::{TaggedKeyPair, TaggedSignable, TaggedSigned};
use nimiq_validator_network::validator_record::ValidatorRecord;
use parking_lot::{Mutex, RwLock};
use thiserror::Error;
use tokio::sync::{broadcast, mpsc, oneshot};
#[cfg(feature = "tokio-time")]
use tokio::time::{Instant, Interval};
use tokio::{
sync::{broadcast, mpsc, oneshot},
time,
};
use tokio_stream::wrappers::{BroadcastStream, ReceiverStream};
#[cfg(not(feature = "tokio-time"))]
use wasm_timer::Interval;
Expand Down Expand Up @@ -1721,17 +1718,14 @@ impl Network {

if let Ok(request_id) = output_rx.await {
let timeout = Req::TIME_WINDOW.mul_f32(1.5f32);
let result = match time::timeout(timeout, response_rx).await {
Ok(result) => result,
let future = response_rx.map_err(std::io::Error::other);
match wasm_timer::TryFutureExt::timeout(future, timeout).await {
Err(_) => {
warn!(%request_id, request_type = std::any::type_name::<Req>(), %peer_id, "Request timed out with no response from libp2p");
return Err(RequestError::OutboundRequest(OutboundRequestError::Timeout));
Err(RequestError::OutboundRequest(
OutboundRequestError::SenderFutureDropped,
))
}
};
match result {
Err(_) => Err(RequestError::OutboundRequest(
OutboundRequestError::SenderFutureDropped,
)),
Ok(result) => {
let data = result?;
if let Ok((message, left_over)) =
Expand Down

0 comments on commit 2524dee

Please sign in to comment.