-
Notifications
You must be signed in to change notification settings - Fork 180
/
registry.go
307 lines (254 loc) · 7.62 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// Package registry provides the main glue between services
//
// It wraps micro registry (running services declared to the discovery server) into a more generic registry where all
// actual plugins are self-declared.
package registry
import (
"sync"
"time"
"github.com/gyuho/goraph"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/registry"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/pydio/cells/common"
defaults "github.com/pydio/cells/common/micro"
)
var (
// Default registry
Default = NewRegistry()
)
// Registry provides an interface for service discovery
// and an abstraction over varying implementations
// {consul, etcd, zookeeper, ...}
type Registry interface {
Init(...Option)
Register(Service, ...RegisterOption) error
Deregister(Service) error
GetServiceByName(string) Service
GetServicesByName(string) []Service
GetPeers() map[string]*Peer
GetPeer(*registry.Node) *Peer
GetCurrentProcess() *Process
GetCurrentChildrenProcesses() []*Process
GetProcesses() map[string]*Process
GetProcess(*registry.Node) *Process
ListServices(withExcluded ...bool) ([]Service, error)
ListServicesWithFilter(func(Service) bool) ([]Service, error)
GetRunningService(string) ([]Service, error)
ListRunningServices() ([]Service, error)
ListServicesWithMicroMeta(string, ...string) ([]Service, error)
Filter(func(Service) bool) error
Watch() (registry.Watcher, error)
String() string
Options() Options
BeforeInit() error
AfterInit() error
}
type pydioregistry struct {
registerlock *sync.RWMutex
register map[string]Service
graph goraph.Graph
// List of peer addresses that have a service associated with the micro registry
peerlock *sync.RWMutex
peers map[string]*Peer
processeslock *sync.RWMutex
processes map[string]*Process
runninglock *sync.RWMutex
opts Options
flags pflag.FlagSet
}
// Init the default registry
func Init(opts ...Option) {
Default.Init(opts...)
}
// ListServices returns the list of services that are started in the system
func ListServices(withExcluded ...bool) ([]Service, error) {
return Default.ListServices(withExcluded...)
}
// ListServicesWithFilter returns the list of services that are started in the system
func ListServicesWithFilter(fn func(Service) bool) ([]Service, error) {
return Default.ListServicesWithFilter(fn)
}
// GetRunningService returns the list of services that are started in the system
func GetRunningService(name string) ([]Service, error) {
return Default.GetRunningService(name)
}
// ListRunningServices returns the list of services that are started in the system
func ListRunningServices() ([]Service, error) {
return Default.ListRunningServices()
}
// Watch triggers a watch of the default registry
func Watch() (registry.Watcher, error) {
return Default.Watch()
}
// NewRegistry provides a new registry object
func NewRegistry(opts ...Option) Registry {
r := &pydioregistry{
graph: goraph.NewGraph(),
registerlock: new(sync.RWMutex),
register: make(map[string]Service),
opts: newOptions(opts...),
peerlock: new(sync.RWMutex),
peers: make(map[string]*Peer),
processeslock: new(sync.RWMutex),
processes: make(map[string]*Process),
runninglock: new(sync.RWMutex),
}
return r
}
// Init the registry with the options in arguments
func (c *pydioregistry) Init(opts ...Option) {
// process options
for _, o := range opts {
o(&c.opts)
}
// c.maintainRunningServicesList()
}
// Deregister sets a service as excluded in the registry
func (c *pydioregistry) Deregister(s Service) error {
// delete our hash of the service
c.registerlock.Lock()
c.register[s.Name()].SetExcluded(true)
c.registerlock.Unlock()
return nil
}
// Register a new service. Manages dependencies
func (c *pydioregistry) Register(s Service, opts ...RegisterOption) error {
var options RegisterOptions
for _, o := range opts {
o(&options)
}
c.registerlock.Lock()
defer c.registerlock.Unlock()
id := s.Name()
c.register[id] = s
mainNode := goraph.NewNode(id)
if node, err := c.graph.GetNode(mainNode); err == nil && node != nil {
mainNode = node
} else {
c.graph.AddNode(mainNode)
}
// Handling dependencies
for _, dependency := range options.Dependencies {
dependencyNode := goraph.NewNode(dependency)
if node, err := c.graph.GetNode(dependencyNode); err == nil && node != nil {
dependencyNode = node
} else {
c.graph.AddNode(dependencyNode)
}
c.graph.AddEdge(dependencyNode.ID(), mainNode.ID(), 1)
}
for _, flag := range options.Flags {
c.flags.AddFlag(flag)
}
return nil
}
// GetClient returns the default client for the service name
func GetClient(name string) (string, client.Client) {
return common.ServiceGrpcNamespace_ + name, defaults.NewClient()
}
// ShortRequestTimeout returns a 1s RequestTimeout option
func ShortRequestTimeout() client.CallOption {
return client.WithRequestTimeout(1 * time.Second)
}
// Filter the service out of the registry
func (c *pydioregistry) Filter(f func(s Service) bool) error {
services, err := c.ListServices()
if err != nil {
return err
}
for _, s := range services {
if f(s) {
c.Deregister(s)
}
}
return nil
}
// Watch the registry for new entries
func (c *pydioregistry) Watch() (registry.Watcher, error) {
return defaults.Registry().Watch()
}
// String representation of the registry
func (c *pydioregistry) String() string {
return "pydio"
}
// Options returns the list of options set to the registry
func (c *pydioregistry) Options() Options {
return c.opts
}
// BeforeInit calls the before init function for each service in the registry
func (c *pydioregistry) BeforeInit() error {
services, err := c.ListServices()
if err != nil {
return err
}
for _, s := range services {
if (s.RequiresFork() && viper.GetBool("fork")) || !s.RequiresFork() {
s.BeforeInit()
}
}
return nil
}
// AfterInit calls the after init function for each service in the registry
func (c *pydioregistry) AfterInit() error {
services, err := c.ListServices()
if err != nil {
return err
}
for _, s := range services {
if (s.RequiresFork() && viper.GetBool("is_fork")) || !s.RequiresFork() {
s.AfterInit()
}
}
return nil
}
func GetPeers() map[string]*Peer {
return Default.GetPeers()
}
func (c *pydioregistry) GetPeers() map[string]*Peer {
ret := make(map[string]*Peer, len(c.peers))
// copy map
c.peerlock.RLock()
for k, v := range c.peers {
ret[k] = v
}
c.peerlock.RUnlock()
return ret
}
func GetPeer(node *registry.Node) *Peer {
return Default.GetPeer(node)
}
// GetPeer retrieves or creates a Peer from the Node info
func (c *pydioregistry) GetPeer(node *registry.Node) *Peer {
c.peerlock.RLock()
p, ok := c.peers[node.Address]
c.peerlock.RUnlock()
if ok {
return p
}
p = NewPeer(node.Address, node.Metadata)
c.peerlock.Lock()
c.peers[node.Address] = p
c.peerlock.Unlock()
return p
}