Преглед изворни кода

Move election.rs to the new standalone daemon_watch.

Jing Yang пре 3 година
родитељ
комит
9796a49bdc
2 измењених фајлова са 5 додато и 7 уклоњено
  1. 3 6
      src/election.rs
  2. 2 1
      src/raft.rs

+ 3 - 6
src/election.rs

@@ -5,7 +5,6 @@ use std::time::{Duration, Instant};
 use parking_lot::{Condvar, Mutex};
 use rand::{thread_rng, Rng};
 
-use crate::daemon_watch::Daemon;
 use crate::sync_log_entries::SyncLogEntriesComms;
 use crate::term_marker::TermMarker;
 use crate::utils::{retry_rpc, RPC_DEADLINE};
@@ -127,10 +126,10 @@ impl<Command: ReplicableCommand> Raft<Command> {
     /// election timer. There could be more than one vote-counting tasks running
     /// at the same time, but all earlier tasks except the newest one will
     /// eventually realize the term they were competing for has passed and quit.
-    pub(crate) fn run_election_timer(&self) {
+    pub(crate) fn run_election_timer(&self) -> impl FnOnce() {
         let this = self.clone();
 
-        let election_daemon = move || {
+        move || {
             log::info!("{:?} election timer daemon running ...", this.me);
 
             let election = this.election.clone();
@@ -213,9 +212,7 @@ impl<Command: ReplicableCommand> Raft<Command> {
             }
 
             log::info!("{:?} election timer daemon done.", this.me);
-        };
-        self.daemon_watch
-            .create_daemon(Daemon::ElectionTimer, election_daemon);
+        }
     }
 
     fn run_election(

+ 2 - 1
src/raft.rs

@@ -151,7 +151,8 @@ impl<Command: ReplicableCommand> Raft<Command> {
         // internal thread pool.
         this.schedule_heartbeats(HEARTBEAT_INTERVAL);
         // The last step is to start running election timer.
-        this.run_election_timer();
+        daemon_watch
+            .create_daemon(Daemon::ElectionTimer, this.run_election_timer());
         this
     }
 }