element/agent.go
Michael Crosby 382f52335c Remove GRPC from element
I came in like a wrecking ball....

I think the grpc server and rpc configuration for the server should be
handled outside of this package.  There are many ways to configure it
and we need more flexability on start and shutdown for grpc services.
Signal handling should be in the caller.
2018-09-14 08:25:56 -04:00

67 lines
1.3 KiB
Go

package element
import (
"errors"
"time"
"github.com/hashicorp/memberlist"
)
const (
defaultInterval = time.Second * 10
nodeReconcileTimeout = defaultInterval * 3
nodeUpdateTimeout = defaultInterval / 2
)
var (
ErrUnknownConnectionType = errors.New("unknown connection type")
)
// Agent represents the node agent
type Agent struct {
config *Config
members *memberlist.Memberlist
peerUpdateChan chan bool
nodeEventChan chan *NodeEvent
registeredServices map[string]struct{}
memberConfig *memberlist.Config
}
// NewAgent returns a new node agent
func NewAgent(cfg *Config) (*Agent, error) {
updateCh := make(chan bool)
nodeEventCh := make(chan *NodeEvent)
mc, err := cfg.memberListConfig(updateCh, nodeEventCh)
if err != nil {
return nil, err
}
ml, err := memberlist.Create(mc)
if err != nil {
return nil, err
}
return &Agent{
config: cfg,
members: ml,
peerUpdateChan: updateCh,
nodeEventChan: nodeEventCh,
memberConfig: mc,
}, nil
}
// SyncInterval returns the cluster sync interval
func (a *Agent) SyncInterval() time.Duration {
return a.memberConfig.PushPullInterval
}
// Subscribe subscribes to the node event channel
func (a *Agent) Subscribe(ch chan *NodeEvent) {
go func() {
for {
select {
case evt := <-a.nodeEventChan:
ch <- evt
}
}
}()
}