コード例 #1
0
ファイル: state.go プロジェクト: pooya/craft
func captureVotes() {
	nVotes := 0
	voters := make(map[string]bool)
	for {
		sender := <-VoteChan
		if sender == "" {
			log.Print("Someone asked us not to be the leader. Stepping down.")
			transitionToFollower()
			return
		} else {
			if _, ok := voters[sender]; ok {
				log.Print("vote from ", sender, " is already processed")
				continue
			}
			if node.FindNode(sender) == nil {
				log.Print("Received vote from unknown sender: ", sender)
				continue
			}
			nVotes++
			if nVotes > config.NProcesses/2 {
				transitionToLeader()
				return
			}
		}
	}
}
コード例 #2
0
ファイル: state.go プロジェクト: pooya/craft
func selectLeader() {
	heartbeat := true
	for {
		select {
		case sender := <-HeartbeatChan:
			if config.UniqueId == sender {
				log.Print("Got heartbeat from myself")
			} else {
				log.Print("I am: " + config.UniqueId + ", got heartbeat from " + sender)
				VoteChan <- ""
				node := node.FindNode(sender)
				if node == nil {
					log.Fatal("sender is not part of config: ", sender)
				}
				leaderInput <- node
			}
			heartbeat = true
		case <-time.After(time.Duration(getCandidacyTimeout()) * time.Millisecond):
			if !heartbeat {
				if GetMyState() == LEADER {
					log.Print("No heartbeat from myself")
				} else {
					log.Print("Timer Expired, claim the throne")
					transitionToCandidate()
				}
			}
			heartbeat = false
		}
	}
}