Browse Source

migrate net select::futures to use async_std timeout function instead

Dastan-glitch 4 years ago
parent
commit
66699fd1d5
3 changed files with 34 additions and 23 deletions
  1. 14 9
      src/net/connector.rs
  2. 10 6
      src/net/protocol/protocol_version.rs
  3. 10 8
      src/net/session/seed_session.rs

+ 14 - 9
src/net/connector.rs

@@ -1,11 +1,13 @@
-use futures::FutureExt;
+use async_std::future::timeout;
 use smol::Async;
-use std::net::{SocketAddr, TcpStream};
+use std::{
+    net::{SocketAddr, TcpStream},
+    time::Duration,
+};
 
 use crate::{
     error::{Error, Result},
     net::{Channel, ChannelPtr, SettingsPtr},
-    util::sleep,
 };
 
 /// Create outbound socket connections.
@@ -21,14 +23,17 @@ impl Connector {
 
     /// Establish an outbound connection.
     pub async fn connect(&self, hostaddr: SocketAddr) -> Result<ChannelPtr> {
-        futures::select! {
-            stream_result = Async::<TcpStream>::connect(hostaddr).fuse() => {
-                match stream_result {
+        let stream_result =
+            timeout(Duration::from_secs(self.settings.connect_timeout_seconds.into()), async {
+                match Async::<TcpStream>::connect(hostaddr).await {
                     Ok(stream) => Ok(Channel::new(stream, hostaddr).await),
-                    Err(_) => Err(Error::ConnectFailed)
+                    Err(_) => Err(Error::ConnectFailed),
                 }
-            }
-            _ = sleep(self.settings.connect_timeout_seconds).fuse() => Err(Error::ConnectTimeout)
+            })
+            .await;
+        match stream_result {
+            Ok(t) => t,
+            Err(_) => Err(Error::ConnectTimeout),
         }
     }
 }

+ 10 - 6
src/net/protocol/protocol_version.rs

@@ -1,12 +1,11 @@
-use futures::FutureExt;
+use async_std::future::timeout;
 use log::*;
 use smol::Executor;
-use std::sync::Arc;
+use std::{sync::Arc, time::Duration};
 
 use crate::{
     error::{Error, Result},
     net::{message, message_subscriber::MessageSubscription, ChannelPtr, SettingsPtr},
-    util::sleep,
 };
 
 /// Implements the protocol version handshake sent out by nodes at the beginning
@@ -48,9 +47,14 @@ impl ProtocolVersion {
         // Send version, wait for verack
         // Wait for version, send verack
         // Fin.
-        let result = futures::select! {
-            _ = self.clone().exchange_versions(executor).fuse() => Ok(()),
-            _ = sleep(self.settings.channel_handshake_seconds).fuse() => Err(Error::ChannelTimeout)
+        let result = match timeout(
+            Duration::from_secs(self.settings.channel_handshake_seconds.into()),
+            self.clone().exchange_versions(executor),
+        )
+        .await
+        {
+            Ok(t) => t,
+            Err(_) => Err(Error::ChannelTimeout),
         };
         debug!(target: "net", "ProtocolVersion::run() [END]");
         result

+ 10 - 8
src/net/session/seed_session.rs

@@ -1,9 +1,10 @@
 use async_executor::Executor;
-use futures::FutureExt;
+use async_std::future::timeout;
 use log::*;
 use std::{
     net::SocketAddr,
     sync::{Arc, Weak},
+    time::Duration,
 };
 
 use crate::{
@@ -13,7 +14,6 @@ use crate::{
         session::{Session, SessionBitflag, SESSION_SEED},
         ChannelPtr, Connector, HostsPtr, P2p, SettingsPtr,
     },
-    util::sleep,
 };
 
 /// Defines seed connections session.
@@ -49,8 +49,8 @@ impl SeedSession {
         // This line loops through all the tasks and waits for them to finish.
         // But if the seed_query_timeout_seconds times out before they are finished,
         // then it will simply quit and the tasks will get dropped.
-        futures::select! {
-            _ = async move {
+        let result =
+            timeout(Duration::from_secs(settings.seed_query_timeout_seconds.into()), async move {
                 for (i, task) in tasks.into_iter().enumerate() {
                     // Ignore errors
                     match task.await {
@@ -58,11 +58,13 @@ impl SeedSession {
                         Err(err) => warn!("Seed query #{} failed for reason: {}", i, err),
                     }
                 }
-            }.fuse() => {
-            }
-            _ = sleep(settings.seed_query_timeout_seconds).fuse() => {
+            })
+            .await;
+        match result {
+            Ok(_) => {}
+            Err(_) => {
                 error!("Querying seeds timed out");
-                return Err(Error::OperationFailed);
+                return Err(Error::OperationFailed)
             }
         }