forked from ExploratoryEngineering/clusterfunk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_node.go
69 lines (58 loc) · 1.78 KB
/
service_node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package funk
import (
"time"
"github.com/lab5e/clusterfunk/pkg/lg"
"github.com/lab5e/clusterfunk/pkg/toolbox"
"github.com/lab5e/gotoolbox/netutils"
)
// ServiceNode is a node for services that will be discoverable but not a
// member of the cluster. Nonvoting nodes requires a fair bit of capacity in
// the cluster but service nodes are more loosely related and may provide
// services to the cluster itself.
type ServiceNode interface {
RegisterServiceEndpoint(endpointName string, listenAddress string) error
Stop()
}
// NewServiceNode creates a new ServiceNode instance
func NewServiceNode(serviceName string, params ServiceParameters) (ServiceNode, error) {
params.Final()
ret := &serviceNode{
Serf: NewSerfNode(),
}
if params.ZeroConf {
// TODO: Merge this with cluster init code. Some similarities
registry := toolbox.NewZeroconfRegistry(params.Name)
var err error
addrs, err := registry.Resolve(ZeroconfSerfKind, 1*time.Second)
if err != nil {
return nil, err
}
if len(params.Serf.JoinAddress) == 0 {
if len(addrs) > 0 {
params.Serf.JoinAddress = addrs
}
if len(addrs) == 0 {
lg.Warning("No serf nodes found via zeroconf, wont join")
}
}
if err := registry.Register(ZeroconfSerfKind, params.NodeID, netutils.PortOfHostPort(params.Serf.Endpoint)); err != nil {
return nil, err
}
}
if err := ret.Serf.Start(params.NodeID, serviceName, params.Serf); err != nil {
return nil, err
}
return ret, nil
}
type serviceNode struct {
Serf *SerfNode
}
func (s *serviceNode) RegisterServiceEndpoint(endpointName string, listenAddress string) error {
s.Serf.SetTag(endpointName, listenAddress)
return s.Serf.PublishTags()
}
func (s *serviceNode) Stop() {
if err := s.Serf.Stop(); err != nil {
lg.Error("Error stopping Serf node: %v", err)
}
}