Campaign starts a leader election. It will block until this node becomes a leader or context cancelled.
(ctx context.Context)
| 40 | |
| 41 | // Campaign starts a leader election. It will block until this node becomes a leader or context cancelled. |
| 42 | func (e *Election) Campaign(ctx context.Context) error { |
| 43 | if err := e.driver.Campaign(ctx); err != nil { |
| 44 | return fmt.Errorf("not elected: %w", err) |
| 45 | } |
| 46 | e.status.isLeader.Store(true) |
| 47 | // trigger events |
| 48 | e.dispatcher.Dispatch(ctx, OnStatusChanged, OnStatusChangedPayload{Status: e.status}) |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | // Resign gives up the leadership. |
| 53 | func (e *Election) Resign(ctx context.Context) error { |