-
Notifications
You must be signed in to change notification settings - Fork 6
/
zk.go
133 lines (115 loc) · 3.77 KB
/
zk.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
// Copyright 2019 Samaritan Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zk
import (
"context"
"encoding/json"
"errors"
"path"
"github.com/samaritan-proxy/sash/internal/zk"
"github.com/samaritan-proxy/sash/model"
)
type ConnConfig = zk.ConnConfig
// InstanceUnmarshaler unmarshals raw byte array which is stored
// in the underlying registry into a service instance. JSONInstanceUnmarshaler
// is the default implementation, could customize according to demands.
type InstanceUnmarshaler interface {
Unmarshal(data []byte) (*model.ServiceInstance, error)
}
// JSONInstanceUnmarshaler implements the InstanceUnmarshaler interface.
type JSONInstanceUnmarshaler struct{}
// Unmarshal unmarshals raw data into a service instance.
func (u *JSONInstanceUnmarshaler) Unmarshal(data []byte) (*model.ServiceInstance, error) {
inst := &model.ServiceInstance{}
err := json.Unmarshal(data, inst)
return inst, err
}
type discoveryClientOption func(c *DiscoveryClient)
// WithInstanceUnmarshaler returns a option specifying a customized InstanceUnmarshaler.
func WithInstanceUnmarshaler(u InstanceUnmarshaler) discoveryClientOption {
return func(c *DiscoveryClient) {
c.instUnmarshaler = u
}
}
// DiscoveryClient is a client for service discovery based on zookeeper.
type DiscoveryClient struct {
connCfg *zk.ConnConfig
conn zk.Conn
basePath string
instUnmarshaler InstanceUnmarshaler
}
// NewDiscoveryClient creates a discovery client with given parameters.
func NewDiscoveryClient(connCfg *ConnConfig, options ...discoveryClientOption) (*DiscoveryClient, error) {
conn, err := zk.CreateConn(connCfg, false)
if err != nil {
return nil, err
}
c, err := NewDiscoveryClientWithConn(conn, connCfg.BasePath, options...)
if err != nil {
conn.Close()
return nil, err
}
c.connCfg = connCfg
return c, nil
}
// NewDiscoveryClientWithConn creates a discovery client with given zk conn and other parameters.
func NewDiscoveryClientWithConn(conn zk.Conn, basePath string, options ...discoveryClientOption) (*DiscoveryClient, error) {
if basePath == "" {
return nil, errors.New("empty base path")
}
c := &DiscoveryClient{
conn: conn,
basePath: basePath,
instUnmarshaler: new(JSONInstanceUnmarshaler),
}
for _, option := range options {
option(c)
}
return c, nil
}
// Run starts the client until the context is canceled or deadline exceed.
func (c *DiscoveryClient) Run(ctx context.Context) {
<-ctx.Done()
if c.connCfg != nil {
c.conn.Close()
}
}
// List lists all registered service names.
func (c *DiscoveryClient) List() ([]string, error) {
names, _, err := c.conn.Children(c.basePath)
return names, err
}
// Get retrieves a service by name.
func (c *DiscoveryClient) Get(name string) (*model.Service, error) {
svcPath := path.Join(c.basePath, name)
instNames, _, err := c.conn.Children(svcPath)
if err != nil {
return nil, err
}
insts := make([]*model.ServiceInstance, 0, len(instNames))
for _, instName := range instNames {
instPath := path.Join(svcPath, instName)
data, _, err := c.conn.Get(instPath)
if err != nil {
return nil, err
}
inst, err := c.instUnmarshaler.Unmarshal(data)
if err != nil {
return nil, err
}
insts = append(insts, inst)
}
svc := model.NewService(name, insts...)
return svc, nil
}