-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
185 lines (155 loc) · 4.31 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
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
181
182
183
184
185
package registry
import (
"log"
"strconv"
"github.com/werbenhu/chash"
)
const (
registryName = "registry-group" // Default group name for registry service
DefaultReplicas = "10000" // Default number of replicas to virtualize a service
)
// Registry is the registry server object
type Registry struct {
opt *Option
serf Discovery
api Api
}
// New creates a new registry object that can start a registry server when calling Serve().
func New(opts []IOption) *Registry {
option := DefaultOption()
for _, o := range opts {
o(option)
}
s := &Registry{opt: option}
// If Advertise is not set, set it to Addr
if len(s.opt.Advertise) == 0 {
s.opt.Advertise = s.opt.Addr
}
s.api = NewRpcServer()
s.serf = NewSerf(NewMember(
s.opt.Id,
s.opt.Bind,
s.opt.BindAdvertise,
s.opt.Registries,
registryName,
s.opt.Advertise,
))
s.serf.SetHandler(s)
return s
}
// Serve runs the registry server
func (s *Registry) Serve() {
if err := s.serf.Start(); err != nil {
panic(err)
}
if err := s.api.Start(s.opt.Addr); err != nil {
panic(err)
}
}
// Close closes the registry server
func (s *Registry) Close() {
if s.serf != nil {
s.serf.Stop()
}
if s.api != nil {
s.api.Stop()
}
chash.RemoveAllGroup() // Remove all groups from chash
log.Printf("[DEBUG] registry server is closed.\n")
}
// OnMemberJoin is triggered when a new service is registered
func (s *Registry) OnMemberJoin(m *Member) error {
log.Printf("[INFO] a new member joined, id:%s, bind:%s, group:%s, service:%s\n",
m.Id, m.Bind, m.Service.Group, m.Service.Addr)
return s.insert(m)
}
// OnMemberLeave is triggered when a service leaves
func (s *Registry) OnMemberLeave(m *Member) error {
log.Printf("[INFO] a new member left, id:%s, bind:%s, group:%s, service:%s\n",
m.Id, m.Bind, m.Service.Group, m.Service.Addr)
return s.delete(m)
}
// OnMemberUpdate is triggered when a service is updated
func (s *Registry) OnMemberUpdate(m *Member) error {
log.Printf("[INFO] a new member updated, id:%s, bind:%s, group:%s, service:%s\n",
m.Id, m.Bind, m.Service.Group, m.Service.Addr)
return s.insert(m)
}
// delete removes a service from chash
func (s *Registry) delete(m *Member) error {
if len(m.Service.Group) == 0 {
return ErrGroupNameEmpty
}
replicas, err := strconv.Atoi(m.Replicas)
if err != nil {
return ErrReplicasParam
}
group, _ := chash.CreateGroup(m.Service.Group, replicas)
if err := group.Delete(m.Service.Id); err != nil {
return err
}
return nil
}
// insert adds a service to chash
func (s *Registry) insert(m *Member) error {
if len(m.Service.Group) == 0 {
return ErrGroupNameEmpty
}
replicas, err := strconv.Atoi(m.Replicas)
if err != nil {
return ErrReplicasParam
}
payload, err := m.Marshal()
if err != nil {
return err
}
group, _ := chash.CreateGroup(m.Service.Group, replicas)
if err := group.Upsert(m.Service.Id, payload); err != nil {
return err
}
return nil
}
// Match uses a consistent hashing algorithm to assign a service to a key.
func (s *Registry) Match(groupName string, key string) (*Service, error) {
// Get the group associated with the group name.
group, err := chash.GetGroup(groupName)
if err != nil {
return nil, err
}
// Find the element in the group that matches the key.
_, payload, err := group.Match(key)
if err != nil {
return nil, err
}
// Unmarshal the payload to create a Member object.
m := &Member{}
if err := m.Unmarshal(payload); err != nil {
return nil, err
}
// Return the Service associated with the Member.
return &m.Service, nil
}
// Members returns a list of services for a given group name.
func (s *Registry) Members(groupName string) []*Service {
// Create an empty list of services.
services := make([]*Service, 0)
// Get the group associated with the group name.
group, err := chash.GetGroup(groupName)
if err != nil {
return services
}
// Get the elements in the group and create a Service object for each.
elements := group.GetElements()
for _, element := range elements {
// Unmarshal the payload to create a Member object.
m := &Member{}
if err := m.Unmarshal(element.Payload); err != nil {
log.Printf("[ERROR] element to member err:%s\n", err.Error())
continue
}
// Add the Service associated with the Member to the list.
services = append(services, &m.Service)
}
// Return the list of services.
return services
}