Просмотр исходного кода

fix: use retry_notify to log errors

Yujia Qiao 4 лет назад
Родитель
Сommit
ba3c134a87
2 измененных файлов с 30 добавлено и 20 удалено
  1. 13 7
      src/client.rs
  2. 17 13
      src/server.rs

+ 13 - 7
src/client.rs

@@ -166,13 +166,19 @@ async fn do_data_channel_handshake<T: Transport>(
     };
 
     // Connect to remote_addr
-    let mut conn: T::Stream = backoff::future::retry(backoff, || async {
-        Ok(args
-            .connector
-            .connect(&args.remote_addr)
-            .await
-            .with_context(|| "Failed to connect to remote_addr")?)
-    })
+    let mut conn: T::Stream = backoff::future::retry_notify(
+        backoff,
+        || async {
+            Ok(args
+                .connector
+                .connect(&args.remote_addr)
+                .await
+                .with_context(|| "Failed to connect to remote_addr")?)
+        },
+        |e, _| {
+            error!("{:?}", e);
+        },
+    )
     .await?;
 
     // Send nonce

+ 17 - 13
src/server.rs

@@ -138,6 +138,7 @@ impl<'a, T: 'static + Transport> Server<'a, T> {
         // Wait for connections and shutdown signals
         loop {
             tokio::select! {
+                // FIXME: This should be cancel safe.
                 // Wait for incoming control and data channels
                 ret = self.transport.accept(&l) => {
                     match ret {
@@ -481,8 +482,11 @@ fn tcp_listen_and_send(
     let (tx, rx) = mpsc::channel(CHAN_SIZE);
 
     tokio::spawn(async move {
-        let l = backoff::future::retry(listen_backoff(), || async {
+        // FIXME: Respect shutdown signal
+        let l = backoff::future::retry_notify(listen_backoff(), || async {
             Ok(TcpListener::bind(&addr).await?)
+        }, |e, _| {
+            error!("{:?}", e);
         })
         .await
         .with_context(|| "Failed to listen for the service");
@@ -581,18 +585,18 @@ async fn run_udp_connection_pool<T: Transport>(
 ) -> Result<()> {
     // TODO: Load balance
 
-    let l: UdpSocket = backoff::future::retry(listen_backoff(), || async {
-        Ok(match UdpSocket::bind(&bind_addr)
-            .await
-            .with_context(|| "Failed to listen for the service")
-        {
-            Err(e) => {
-                error!("{:?}", e);
-                Err(e)
-            }
-            v => v,
-        }?)
-    })
+    // FIXME: Respect shutdown signal
+    let l: UdpSocket = backoff::future::retry_notify(
+        listen_backoff(),
+        || async {
+            Ok(UdpSocket::bind(&bind_addr)
+                .await
+                .with_context(|| "Failed to listen for the service")?)
+        },
+        |e, _| {
+            error!("{:?}", e);
+        },
+    )
     .await
     .with_context(|| "Failed to listen for the service")?;