func (d *Dinghy) candidate() {
	d.logger.Println("entering candidate state")
	go func() {
		d.logger.Println("requesting vote")
		currentTerm, err := d.RequestVoteRequest()
		if err != nil {
			d.logger.Errorln("executing RequestVoteRequest", err)
			switch err {
			case ErrNewElectionTerm:
				d.State.StepDown(currentTerm)
			case ErrTooFewVotes:
				d.State.State(StateFollower)
			}
			return
		}
		// it wins the election
		d.State.LeaderID(d.State.ID())
		d.State.State(StateLeader)
	}()
	for {
		select {
		case <-d.stopChan:
			return
		case aer := <-d.State.AppendEntriesEvent():
			// https://raft.github.io/raft.pdf
			// While waiting for votes, a candidate may receive an
			// AppendEntries RPC from another server claiming to be
			// leader. If the leader’s term (included in its RPC) is at least
			// as large as the candidate’s current term, then the candidate
			// recognizes the leader as legitimate and returns to follower
			// state. If the term in the RPC is smaller than the candidate’s
			// current term, then the candidate rejects the RPC and continues
			// in candidate state.
			d.logger.Println("candidate got an AppendEntries from a leader", aer)
			if aer.Term >= d.State.Term() {
				d.State.StepDown(aer.Term)
				return
			}
		case newState := <-d.State.StateChanged():
			if newState == StateCandidate {
				continue
			}
			d.logger.Println("candidate state changed to", d.State.StateString(newState))
			return
		case e := <-d.State.ElectionTick():
			d.logger.Println("election timeout, restarting election", e)
			return
		}
	}
}
// candidate is for when in StateCandidate. The loop will
// attempt an election repeatedly until it receives events.
// https://raft.github.io/raft.pdf
// A candidate continues in
// this state until one of three things happens: (a) it wins the
// election, (b) another server establishes itself as leader, or
// (c) a period of time goes by with no winner.
candidate is for when in StateCandidate. The loop will
attempt an election repeatedly until it receives events.
// candidate is for when in StateCandidate . The loop will // attempt an election repeatedly until it receives events . // https : // raft . github . io / raft . pdf // A candidate continues in // this state until one of three things happens : ( a ) it wins the // election ( b ) another server establishes itself as leader or // ( c ) a period of time goes by with no winner .
func (d *Dinghy) candidate() {
	d.logger.Println("entering candidate state")
	go func() {
		d.logger.Println("requesting vote")
		currentTerm, err := d.RequestVoteRequest()
		if err != nil {
			d.logger.Errorln("executing RequestVoteRequest", err)
			switch err {
			case ErrNewElectionTerm:
				d.State.StepDown(currentTerm)
			case ErrTooFewVotes:
				d.State.State(StateFollower)
			}
			return
		}
		d.State.LeaderID(d.State.ID())
		d.State.State(StateLeader)
	}()
	for {
		select {
		case <-d.stopChan:
			return
		case aer := <-d.State.AppendEntriesEvent():
			d.logger.Println("candidate got an AppendEntries from a leader", aer)
			if aer.Term >= d.State.Term() {
				d.State.StepDown(aer.Term)
				return
			}
		case newState := <-d.State.StateChanged():
			if newState == StateCandidate {
				continue
			}
			d.logger.Println("candidate state changed to", d.State.StateString(newState))
			return
		case e := <-d.State.ElectionTick():
			d.logger.Println("election timeout, restarting election", e)
			return
		}
	}
}
func ( d * Dinghy ) candidate ( ) { d . logger . Println ( " " ) go func ( ) { d . logger . Println ( " " ) currentTerm , err := d . RequestVoteRequest ( ) if err != nil { d . logger . Errorln ( " " , err ) switch err { case ErrNewElectionTerm : d . State . StepDown ( currentTerm ) case ErrTooFewVotes : d . State . State ( StateFollower ) } return } d . State . LeaderID ( d . State . ID ( ) ) d . State . State ( StateLeader ) } ( ) for { select { case <- d . stopChan : return case aer := <- d . State . AppendEntriesEvent ( ) : d . logger . Println ( " " , aer ) if aer . Term >= d . State . Term ( ) { d . State . StepDown ( aer . Term ) return } case newState := <- d . State . StateChanged ( ) : if newState == StateCandidate { continue } d . logger . Println ( " " , d . State . StateString ( newState ) ) return case e := <- d . State . ElectionTick ( ) : d . logger . Println ( " " , e ) return } } }
candidate is for when in StateCandidate.
candidate is for when in StateCandidate .
// it wins the election // https://raft.github.io/raft.pdf // While waiting for votes, a candidate may receive an // AppendEntries RPC from another server claiming to be // leader. If the leader’s term (included in its RPC) is at least // as large as the candidate’s current term, then the candidate // recognizes the leader as legitimate and returns to follower // state. If the term in the RPC is smaller than the candidate’s // current term, then the candidate rejects the RPC and continues // in candidate state.