forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
188 lines (154 loc) · 4.2 KB
/
wrapper.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
package registry
import (
"errors"
"fmt"
"time"
"github.com/micro/go-micro/registry"
"github.com/pydio/cells/common/config"
pydioregistry "github.com/pydio/cells/common/registry"
)
type registryWithExpiry struct {
registry.Registry
duration time.Duration
}
func NewRegistryWithExpiry(r registry.Registry, duration time.Duration) registry.Registry {
return ®istryWithExpiry{Registry: r, duration: duration}
}
func (r *registryWithExpiry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
// Adding time as a string metadata for the nodes
for _, node := range s.Nodes {
// Adding twice the ttl to make sure we don't have any false positive
data, _ := time.Now().Add(r.duration).MarshalText()
node.Metadata["expiry"] = string(data)
}
return r.Registry.Register(s, opts...)
}
func (r *registryWithExpiry) ListServices() ([]*registry.Service, error) {
services, err := r.Registry.ListServices()
if err != nil {
return nil, err
}
for _, service := range services {
var nodes []*registry.Node
for _, node := range service.Nodes {
exp, ok := node.Metadata["expiry"]
if !ok {
continue
}
var expiry time.Time
if err := expiry.UnmarshalText([]byte(exp)); err != nil {
continue
}
if expiry.Before(time.Now()) {
ss := cp([]*registry.Service{service})
ss[0].Nodes = []*registry.Node{node}
if err := r.Registry.Deregister(ss[0]); err != nil {
return nil, err
}
continue
}
nodes = append(nodes, node)
}
service.Nodes = nodes
}
return services, nil
}
func (r *registryWithExpiry) prune() {
ticker := time.NewTicker(1 * time.Minute)
for {
select {
case <-r.Registry.Options().Context.Done():
return
case <-ticker.C:
r.ListServices()
}
}
}
type registryWithUnique struct {
registry.Registry
}
func NewRegistryWithUnique(r registry.Registry) registry.Registry {
return ®istryWithUnique{Registry: r}
}
func (r *registryWithUnique) Register(s *registry.Service, opts ...registry.RegisterOption) error {
if config.Get("services", s.Name, "unique").Bool() {
ss, err := r.GetService(s.Name)
if err != nil {
return err
}
if len(ss) > 0 {
return errors.New("ErrServiceStartNeedsRetry - service already running")
}
}
return r.Registry.Register(s, opts...)
}
type registryWithPeers struct {
registry.Registry
}
func NewRegistryWithPeers(r registry.Registry) registry.Registry {
return ®istryWithPeers{
Registry: r,
}
}
func (r *registryWithPeers) Register(s *registry.Service, opts ...registry.RegisterOption) error {
for _, node := range s.Nodes {
pydioregistry.GetPeer(node).Add(s, fmt.Sprintf("%d", node.Port))
}
return r.Registry.Register(s, opts...)
}
func (r *registryWithPeers) Deregister(s *registry.Service) error {
for _, node := range s.Nodes {
pydioregistry.GetPeer(node).Delete(s, fmt.Sprintf("%d", node.Port))
}
return r.Registry.Deregister(s)
}
type registryWithProcesses struct {
registry.Registry
}
func NewRegistryWithProcesses(r registry.Registry) registry.Registry {
return ®istryWithProcesses{
Registry: r,
}
}
func (r *registryWithProcesses) Register(s *registry.Service, opts ...registry.RegisterOption) error {
for _, node := range s.Nodes {
pydioregistry.GetProcess(node).Add(s.Name)
}
return r.Registry.Register(s, opts...)
}
func (r *registryWithProcesses) Deregister(s *registry.Service) error {
for _, node := range s.Nodes {
pydioregistry.GetProcess(node).Delete(s.Name)
}
return r.Registry.Deregister(s)
}
// cp copies a service. Because we're caching handing back pointers would
// create a race condition, so we do this instead
// its fast enough
func cp(current []*registry.Service) []*registry.Service {
var services []*registry.Service
for _, service := range current {
// copy service
s := new(registry.Service)
*s = *service
// copy nodes
var nodes []*registry.Node
for _, node := range service.Nodes {
n := new(registry.Node)
*n = *node
nodes = append(nodes, n)
}
s.Nodes = nodes
// copy endpoints
var eps []*registry.Endpoint
for _, ep := range service.Endpoints {
e := new(registry.Endpoint)
*e = *ep
eps = append(eps, e)
}
s.Endpoints = eps
// append service
services = append(services, s)
}
return services
}