-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice-maker.go
198 lines (181 loc) · 4.35 KB
/
service-maker.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
package service
import (
"context"
"sync"
"time"
)
const (
RegisterState = "register"
GetState = "get"
FailedState = "failed"
WaitState = "wait"
ReadyState = "ready"
)
// Free to add something useful here like logging or metrics.
var (
EnterActionHook func(action, name string) = func(action, name string) { return }
ExitActionHook func(action, name string) = func(action, name string) { return }
)
var WaitForService time.Duration = 10 * time.Microsecond
var serviceProviders providers
type providers struct {
sync.RWMutex
m map[string]*service
}
func init() {
serviceProviders.m = make(map[string]*service)
}
// Provide signals what something became as a service.
func Provide(name string) *service {
EnterActionHook(RegisterState, name)
serviceProviders.Lock()
c, ok := serviceProviders.m[name]
if !ok {
c = &service{
Name: name,
NeedRestart: make(chan struct{}, 1),
Dependents: make(map[string]bool)}
serviceProviders.m[name] = c
}
serviceProviders.Unlock()
ExitActionHook(RegisterState, name)
return c
}
// Get returns the instance of the service. This method allows pass
// context for cancellation in a time.
func GetCancelable(ctx context.Context, name string) interface{} {
var (
p *service
ok bool
)
EnterActionHook(GetState, name)
for {
select {
case <-ctx.Done():
return nil
default:
serviceProviders.RLock()
p, ok = serviceProviders.m[name]
serviceProviders.RUnlock()
if ok {
p.RLock()
if p.IsReady {
s := p.Instance
p.RUnlock()
ExitActionHook(GetState, name)
return s
}
p.RUnlock()
}
time.Sleep(WaitForService) // XXX alternative with channels
}
}
}
// Get returns the instance of the service.
func Get(name string) interface{} {
return GetCancelable(context.Background(), name)
}
// List returns a list of currently ready to use services.
func List() map[string][]string {
var (
list = make(map[string][]string)
)
serviceProviders.RLock()
for name, service := range serviceProviders.m {
if service.IsReady {
deps := []string{}
for name := range service.Dependents {
deps = append(deps, name)
}
list[name] = deps
}
}
serviceProviders.RUnlock()
return list
}
// Fail signals the service that it is failed with anything
// critical. The service should listen on service.Failed() for these
// signals and handle them with the service restart. After restart
// service should Put() its instance to service again.
func Fail(name string) {
serviceProviders.RLock()
c, ok := serviceProviders.m[name]
serviceProviders.RUnlock()
if ok {
c.Lock()
if !c.IsReady {
c.Unlock()
return
}
EnterActionHook(FailedState, name)
c.IsReady = false
c.Unlock()
c.RLock()
for key := range c.Dependents {
if name == key {
continue // cyclic dependency detected
}
go func(name string) {
Fail(name)
}(key)
}
c.NeedRestart <- struct{}{}
c.RUnlock()
ExitActionHook(FailedState, name)
}
}
type service struct {
Name string
NeedRestart chan struct{}
sync.RWMutex
IsReady bool
Instance interface{}
Dependents map[string]bool // XXX сюда лучше каналы на рестарт сервисов
}
// WaitFor waits for a service and returns it instance.
func (c *service) WaitFor(name string) interface{} {
var (
p *service
ok, set bool
)
EnterActionHook(WaitState, name)
for {
serviceProviders.RLock()
p, ok = serviceProviders.m[name]
serviceProviders.RUnlock()
if ok {
if !set {
p.Lock()
p.Dependents[c.Name] = true
set = true
p.Unlock()
}
p.RLock()
if p.IsReady {
s := p.Instance
p.RUnlock()
ExitActionHook(WaitState, name)
return s
}
p.RUnlock()
}
time.Sleep(WaitForService) // XXX alternative with channels
}
}
// Ready puts the service instance into service structure and says the
// service is ready to serve.
func (c *service) Ready(service interface{}) {
EnterActionHook(ReadyState, c.Name)
c.Lock()
c.Instance = service
c.IsReady = true
c.Unlock()
ExitActionHook(ReadyState, c.Name)
}
// Failed notify the service that it is failed and should be
// restarted. The service should listen on service.Failed() for these
// signals and handle them with the service restart. After restart
// service should Put() its instance to service again.
func (c *service) Failed() chan struct{} { // XXX rename
return c.NeedRestart
}