forked from golang/groupcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetcd.go
180 lines (155 loc) · 3.71 KB
/
etcd.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package groupcache
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/adrian-lin-1-0-0/groupcache/discovery"
etcd "go.etcd.io/etcd/client/v3"
)
var (
ErrServiceNameEmpty = errors.New("service name is empty")
ErrServiceEndpointEmpty = errors.New("service endpoint is empty")
ErrGetServicesFailed = errors.New("get services failed")
)
type Client struct {
client *etcd.Client
self discovery.Service
ctx context.Context
ttl time.Duration
heartbeat time.Duration
leaseID etcd.LeaseID
once sync.Once
leaser etcd.Lease
}
const (
defaultTTL = 10 * time.Second
defaultHeartbeat = 3 * time.Second
)
var _ discovery.Client = (*Client)(nil)
func NewClientWithEtcdClient(ctx context.Context, cli *etcd.Client, service discovery.Service) (*Client, error) {
if service.Name == "" {
return nil, ErrServiceNameEmpty
}
if service.Endpoint == "" {
return nil, ErrServiceEndpointEmpty
}
return &Client{
client: cli,
self: service,
ctx: ctx,
ttl: defaultTTL,
heartbeat: defaultHeartbeat,
once: sync.Once{},
}, nil
}
func NewEtcdClient(ctx context.Context, endpoints []string, service discovery.Service) (*Client, error) {
if service.Name == "" {
return nil, ErrServiceNameEmpty
}
if service.Endpoint == "" {
return nil, ErrServiceEndpointEmpty
}
cli, err := etcd.New(etcd.Config{
Endpoints: endpoints,
Context: ctx,
DialTimeout: 5 * time.Second,
})
if err != nil {
return nil, err
}
return &Client{
client: cli,
self: service,
ctx: ctx,
ttl: defaultTTL,
heartbeat: defaultHeartbeat,
once: sync.Once{},
}, nil
}
func (c *Client) GetServices(basePath string) ([]discovery.Service, error) {
resp, err := c.client.Get(c.ctx, basePath, etcd.WithPrefix())
if err != nil {
return nil, ErrGetServicesFailed
}
var services []discovery.Service
for _, kv := range resp.Kvs {
if string(kv.Key) == c.self.Name {
continue
}
services = append(services, discovery.Service{
Name: string(kv.Key),
Endpoint: string(kv.Value),
})
}
return services, nil
}
func (c *Client) WatchPrefix(basePath string) (discovery.EventChan, error) {
watchChan := c.client.Watch(c.ctx, basePath, etcd.WithPrefix())
eventChan := make(chan discovery.Event)
go func() {
for resp := range watchChan {
for _, ev := range resp.Events {
var event discovery.Event
switch ev.Type {
case etcd.EventTypePut:
event.Type = discovery.EventPut
case etcd.EventTypeDelete:
event.Type = discovery.EventDelete
}
event.Key = string(ev.Kv.Key)
event.Value = string(ev.Kv.Value)
eventChan <- event
}
}
}()
return eventChan, nil
}
func (c *Client) InitLease(ttl int64) error {
lease := etcd.NewLease(c.client)
c.leaser = lease
leaseResp, err := lease.Grant(c.ctx, ttl)
if err != nil {
return fmt.Errorf("grant lease failed: %v", err)
}
c.leaseID = leaseResp.ID
return nil
}
func (c *Client) Register(service discovery.Service) error {
c.once.Do(func() {
err := c.InitLease(int64(c.ttl.Seconds()))
if err != nil {
return
}
go c.keepAlive()
})
if c.leaseID == 0 {
panic(errors.New("leaseID is nil"))
}
_, err := c.client.Put(c.ctx, service.Name, service.Endpoint, etcd.WithLease(c.leaseID))
if err != nil {
panic("register service failed :" + err.Error())
}
return err
}
func (c *Client) Unregister(service discovery.Service) error {
_, err := c.client.Delete(c.ctx, service.Name)
return err
}
func (c *Client) keepAlive() {
leaseKeepAliveQueue, err := c.leaser.KeepAlive(c.ctx, c.leaseID)
if err != nil {
panic(err)
}
for {
select {
case _, ok := <-leaseKeepAliveQueue:
if !ok {
return
}
case <-c.ctx.Done():
return
}
}
}