Explorar o código

Set a timeout when polling the main queue.

So that we do not need to sleep in between.
Jing Yang %!s(int64=5) %!d(string=hai) anos
pai
achega
eba98b755a
Modificáronse 1 ficheiros con 4 adicións e 6 borrados
  1. 4 6
      src/network.rs

+ 4 - 6
src/network.rs

@@ -5,7 +5,7 @@ use std::sync::{
 };
 use std::time::{Duration, Instant};
 
-use crossbeam_channel::{Receiver, Sender, TryRecvError};
+use crossbeam_channel::{Receiver, Sender, RecvTimeoutError};
 use parking_lot::Mutex;
 use rand::{thread_rng, Rng};
 
@@ -289,17 +289,15 @@ impl Network {
                     stop_timer = Instant::now();
                 }
 
-                match rx.try_recv() {
+                match rx.recv_timeout(Self::SHUTDOWN_DELAY / 2) {
                     Ok(rpc) => {
                         thread_pool
                             .spawn(Self::serve_rpc(network.clone(), rpc));
                     }
                     // All senders have disconnected. This should never happen,
                     // since the network instance itself holds a sender.
-                    Err(TryRecvError::Disconnected) => break,
-                    Err(TryRecvError::Empty) => {
-                        std::thread::sleep(Self::SHUTDOWN_DELAY)
-                    }
+                    Err(RecvTimeoutError::Disconnected) => break,
+                    Err(RecvTimeoutError::Timeout) => {}
                 }
             }