func (d *Dinghy) leader() error {
	d.logger.Println("entering leader state")
	go d.AppendEntriesRequest()
	errChan := make(chan error)
	go func() {
		// Run the OnLeader event in a goroutine in case
		// it has a long delay. Any errors returned will exit the
		// leader state.
		d.mu.Lock()
		defer d.mu.Unlock()
		if err := d.OnLeader(); err != nil {
			d.logger.Errorln("executing OnLeader", err)
			errChan <- err
		}
	}()
	for {
		select {
		case err := <-errChan:
			d.State.State(StateFollower)
			go func() {
				// Removing the state change event here
				// before returning error.
				<-d.State.StateChanged()
			}()
			return err
		case <-d.stopChan:
			return nil
		case <-d.State.AppendEntriesEvent():
			// ignore any append entries to self.
			continue
		case newState := <-d.State.StateChanged():
			if newState == StateLeader {
				continue
			}
			d.logger.Println("leader state changed to", d.State.StateString(newState))
			return nil
		case h := <-d.State.HeartbeatTick():
			d.logger.Println("sending to peers AppendEntriesRequest", d.Nodes, h)
			currentTerm, err := d.AppendEntriesRequest()
			if err != nil {
				d.logger.Errorln("executing AppendEntriesRequest", err)
				switch err {
				case ErrNewElectionTerm:
					d.State.StepDown(currentTerm)
					return nil
				}
			}
		}
	}
}
// leader is for when in StateLeader. The loop will continually send
// a heartbeat of AppendEntries to all peers at a rate of HeartbeatTimeoutMS.
leader is for when in StateLeader. The loop will continually send
a heartbeat of AppendEntries to all peers at a rate of HeartbeatTimeoutMS.
// leader is for when in StateLeader . The loop will continually send // a heartbeat of AppendEntries to all peers at a rate of HeartbeatTimeoutMS .
func (d *Dinghy) leader() error {
	d.logger.Println("entering leader state")
	go d.AppendEntriesRequest()
	errChan := make(chan error)
	go func() {
		d.mu.Lock()
		defer d.mu.Unlock()
		if err := d.OnLeader(); err != nil {
			d.logger.Errorln("executing OnLeader", err)
			errChan <- err
		}
	}()
	for {
		select {
		case err := <-errChan:
			d.State.State(StateFollower)
			go func() {
				<-d.State.StateChanged()
			}()
			return err
		case <-d.stopChan:
			return nil
		case <-d.State.AppendEntriesEvent():
			continue
		case newState := <-d.State.StateChanged():
			if newState == StateLeader {
				continue
			}
			d.logger.Println("leader state changed to", d.State.StateString(newState))
			return nil
		case h := <-d.State.HeartbeatTick():
			d.logger.Println("sending to peers AppendEntriesRequest", d.Nodes, h)
			currentTerm, err := d.AppendEntriesRequest()
			if err != nil {
				d.logger.Errorln("executing AppendEntriesRequest", err)
				switch err {
				case ErrNewElectionTerm:
					d.State.StepDown(currentTerm)
					return nil
				}
			}
		}
	}
}
func ( d * Dinghy ) leader ( ) error { d . logger . Println ( " " ) go d . AppendEntriesRequest ( ) errChan := make ( chan error ) go func ( ) { d . mu . Lock ( ) defer d . mu . Unlock ( ) if err := d . OnLeader ( ) ; err != nil { d . logger . Errorln ( " " , err ) errChan <- err } } ( ) for { select { case err := <- errChan : d . State . State ( StateFollower ) go func ( ) { <- d . State . StateChanged ( ) } ( ) return err case <- d . stopChan : return nil case <- d . State . AppendEntriesEvent ( ) : continue case newState := <- d . State . StateChanged ( ) : if newState == StateLeader { continue } d . logger . Println ( " " , d . State . StateString ( newState ) ) return nil case h := <- d . State . HeartbeatTick ( ) : d . logger . Println ( " " , d . Nodes , h ) currentTerm , err := d . AppendEntriesRequest ( ) if err != nil { d . logger . Errorln ( " " , err ) switch err { case ErrNewElectionTerm : d . State . StepDown ( currentTerm ) return nil } } } } }
leader is for when in StateLeader.
leader is for when in StateLeader .
// Run the OnLeader event in a goroutine in case // it has a long delay. Any errors returned will exit the // leader state. // Removing the state change event here // before returning error. // ignore any append entries to self.