vipman/pkg/cluster/cluster.go
2020-11-14 00:54:18 +01:00

43 lines
871 B
Go

package cluster
import (
"context"
"errors"
"github.com/hashicorp/raft"
)
// Cluster - The Cluster object manages the state of the cluster for a particular node
type Cluster struct {
kv *KV
r *raft.Raft
stop chan struct{}
done chan struct{}
Callbacks Callbacks
}
type Callbacks struct {
Leader func(context.Context, *KV)
Follower func(context.Context, *KV)
}
// InitCluster - Will attempt to initialise all of the required settings for the cluster
func InitCluster(callbacks Callbacks) (*Cluster, error) {
if callbacks.Leader == nil {
return nil, errors.New("Leader is nil")
}
if callbacks.Follower == nil {
return nil, errors.New("Follower is nil")
}
c := &Cluster{
kv: &KV{
m: make(map[string][]byte),
},
stop: make(chan struct{}, 1),
done: make(chan struct{}, 1),
Callbacks: callbacks,
}
return c, nil
}