-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
60 lines (50 loc) · 1.58 KB
/
registry.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
package grpclb
import (
etcd "github.com/coreos/etcd/clientv3"
etcdnaming "github.com/coreos/etcd/clientv3/naming"
"golang.org/x/net/context"
"google.golang.org/grpc/naming"
)
const (
// registryKeepAliveTTL the Time-To-Live of this service in the registry.
// It also used when the server exited unexpectedly.
registryKeepAliveTTL int64 = 3
)
// Registry for the server side to register and deregister from resolver.
type Registry interface {
// Register register the server information into Registry.
Register(*Server) error
// Deregister deregister the server form Registry.
Deregister(*Server) error
// Close the registry
Close() error
}
type etcdRegistry struct {
r *etcdnaming.GRPCResolver
leaseID etcd.LeaseID
ctx context.Context
cancel context.CancelFunc
}
// NewEtcdRegistry create a registry by given etcd client.
func NewEtcdRegistry(c *etcd.Client) (Registry, error) {
er := &etcdRegistry{r: &etcdnaming.GRPCResolver{Client: c}}
er.ctx, er.cancel = context.WithCancel(context.Background())
l := etcd.NewLease(c)
resp, err := l.Grant(er.ctx, registryKeepAliveTTL)
if err != nil {
return nil, err
}
er.leaseID = resp.ID
l.KeepAlive(er.ctx, resp.ID)
return er, nil
}
func (er *etcdRegistry) Register(s *Server) error {
return er.r.Update(er.ctx, s.Name, naming.Update{Op: naming.Add, Addr: s.Addr, Metadata: s.Metadata}, etcd.WithLease(er.leaseID))
}
func (er *etcdRegistry) Deregister(s *Server) error {
return er.r.Update(er.ctx, s.Name, naming.Update{Op: naming.Delete, Addr: s.Addr})
}
func (er *etcdRegistry) Close() error {
er.cancel()
return nil
}