forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
258 lines (210 loc) · 6.07 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
/*
* Copyright (c) 2018. 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"
"github.com/gyuho/goraph"
"github.com/micro/go-micro/client"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/micro"
"github.com/spf13/pflag"
)
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
GetService(string) ([]Service, error)
GetServicesByName(string) []Service
GetPeers() map[string]*Peer
ListServices(withExcluded ...bool) ([]Service, error)
ListRunningServices() ([]Service, error)
ListServicesWithMicroMeta(string, ...string) ([]Service, error)
SetServiceStopped(string) error
Filter(func(Service) bool) error
Watch() (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
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...)
}
// 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() (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),
}
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)
}
}
// 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
}
// GetService returns the service by name
func (c *pydioregistry) GetService(string) ([]Service, error) {
return nil, nil
}
// GetClient returns the default client for the service name
func GetClient(name string) (string, client.Client) {
return common.SERVICE_GRPC_NAMESPACE_ + name, defaults.NewClient()
}
// 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() (Watcher, error) {
return newWatcher(), nil
}
// 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 {
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 {
s.AfterInit()
}
return nil
}
func GetPeers() map[string]*Peer {
return Default.GetPeers()
}
func (c *pydioregistry) GetPeers() map[string]*Peer {
return c.peers
}
func (c *pydioregistry) GetPeer(address string) *Peer {
if p, ok := c.peers[address]; ok {
return p
}
new := NewPeer(address)
c.peers[address] = new
return new
}