Jelajahi Sumber

Refactor: Separate spawn_request_votes().

Jing Yang 3 tahun lalu
induk
melakukan
3c0ef3495f
1 mengubah file dengan 17 tambahan dan 10 penghapusan
  1. 17 10
      src/election.rs

+ 17 - 10
src/election.rs

@@ -252,16 +252,7 @@ impl<Command: ReplicableCommand> Raft<Command> {
             )
         };
 
-        let mut votes = vec![];
-        for peer in self.peers.clone().into_iter() {
-            if peer != self.me {
-                let one_vote = self
-                    .thread_pool
-                    .spawn(Self::request_vote(peer, args.clone()));
-                votes.push(one_vote);
-            }
-        }
-
+        let votes = self.spawn_request_votes(args);
         let (tx, rx) = futures_channel::oneshot::channel();
         self.thread_pool.spawn(Self::count_vote_util_cancelled(
             me,
@@ -294,6 +285,22 @@ impl<Command: ReplicableCommand> Raft<Command> {
         None
     }
 
+    fn spawn_request_votes(
+        &self,
+        args: RequestVoteArgs,
+    ) -> Vec<tokio::task::JoinHandle<Option<bool>>> {
+        let mut votes = vec![];
+        for peer in self.peers.clone().into_iter() {
+            if peer != self.me {
+                let one_vote = self
+                    .thread_pool
+                    .spawn(Self::request_vote(peer, args.clone()));
+                votes.push(one_vote);
+            }
+        }
+        return votes;
+    }
+
     async fn quorum_before_cancelled(
         votes: Vec<tokio::task::JoinHandle<Option<bool>>>,
         cancel_token: futures_channel::oneshot::Receiver<()>,