45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package cluster
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
)
|
|
|
|
// Cluster - The Cluster object manages the state of the cluster for a particular node
|
|
type Cluster struct {
|
|
stateMachine FSM
|
|
stop chan struct{}
|
|
done chan struct{}
|
|
Callbacks LeaderCallbacks
|
|
}
|
|
|
|
type LeaderCallbacks struct {
|
|
// OnStartedLeading is called when a LeaderElector client starts leading
|
|
OnStartedLeading func(context.Context)
|
|
// OnStoppedLeading is called when a LeaderElector client stops leading
|
|
OnStoppedLeading func()
|
|
// OnNewLeader is called when the client observes a leader that is
|
|
// not the previously observed leader. This includes the first observed
|
|
// leader when the client starts.
|
|
OnNewLeader func(string)
|
|
}
|
|
|
|
// InitCluster - Will attempt to initialise all of the required settings for the cluster
|
|
func InitCluster(callbacks LeaderCallbacks) (*Cluster, error) {
|
|
if callbacks.OnStartedLeading == nil {
|
|
return nil, errors.New("OnStartedLeading is nil")
|
|
}
|
|
if callbacks.OnStoppedLeading == nil {
|
|
return nil, errors.New("OnStoppedLeading is nil")
|
|
}
|
|
if callbacks.OnNewLeader == nil {
|
|
return nil, errors.New("OnNewLeader is nil")
|
|
}
|
|
c := &Cluster{
|
|
stop: make(chan struct{}, 1),
|
|
done: make(chan struct{}, 1),
|
|
Callbacks: callbacks,
|
|
}
|
|
|
|
return c, nil
|
|
}
|