-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
78 lines (68 loc) · 1.66 KB
/
service.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
70
71
72
73
74
75
76
77
78
package discovery
import (
clientv3 "go.etcd.io/etcd/client/v3"
"context"
)
type Service struct {
nodeKey string
nodeAddress string
stopCh chan struct{}
etcdClient *clientv3.Client
config *ServiceConfig
}
type ServiceConfig struct {
HeartbeatSeconds int32 // default 5 seconds
}
// Create a Service
func NewService(etcdClient* clientv3.Client, nodeKey string, nodeAddress string, config *ServiceConfig)(*Service){
return &Service{
nodeKey:nodeKey,
nodeAddress:nodeAddress,
stopCh:make(chan struct{}, 2),
etcdClient:etcdClient,
config:config,
}
}
// Start Service: register to ETCD, and start heatbeat
func (service *Service)Start()(<- chan struct{}, error){
heatbeatSeconds := int64(5)
if service.config != nil && service.config.HeartbeatSeconds > 0 {
heatbeatSeconds = int64(service.config.HeartbeatSeconds)
}
stoppedCh := make(chan struct{}, 1)
// register
grantResp, err := service.etcdClient.Grant(context.TODO(), heatbeatSeconds)
if err != nil {
return nil, err
}
grantId := grantResp.ID
_, err = service.etcdClient.Put(context.TODO(), service.nodeKey, service.nodeAddress, clientv3.WithLease(grantId))
if err != nil {
return nil, err
}
keepAliveResp, err := service.etcdClient.KeepAlive(context.TODO(), grantId)
if err != nil {
return nil, err
}
// heartbeat
go func(){
defer func() {
stoppedCh <- struct{}{}
}()
LOOP:
for {
select {
case <- service.stopCh:
service.etcdClient.Revoke(context.TODO(), grantId)
break LOOP
case <-keepAliveResp: // todo : if fail?
break
}
}
}()
return stoppedCh, nil
}
// Stop a Service: delete from ETCD
func (service *Service)Stop(){
service.stopCh <- struct{}{}
}