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
|
package service
import (
"fmt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"sync"
)
// Config provides ability to slice configuration sections and unmarshal configuration data into
// given structure.
type Config interface {
// Get nested config section (sub-map), returns nil if section not found.
Get(service string) Config
// Unmarshal unmarshal config data into given struct.
Unmarshal(out interface{}) error
}
// Container controls all internal RR services and provides plugin based system.
type Container interface {
// Register add new service to the registry under given name.
Register(name string, service Service)
// Reconfigure configures all underlying services with given configuration.
Configure(cfg Config) error
// Check is Service has been registered and configured.
Has(service string) bool
// Get returns Service instance by it's Name or nil if Service not found. Method must return only configured instance.
Get(service string) Service
// Serve all configured services. Non blocking.
Serve() error
// Close all active services.
Stop()
}
// Service provides high level functionality for road runner Service.
type Service interface {
// Configure must return configure service and return true if service is enabled. Must return error in case of
// misconfiguration. Services must not be used without proper configuration pushed first.
Configure(cfg Config, reg Container) (enabled bool, err error)
// Serve serves Service.
Serve() error
// Close setStopped Service Service.
Stop()
}
type registry struct {
log logrus.FieldLogger
mu sync.Mutex
candidates []*entry
configured []*entry
}
// entry creates association between service instance and given name.
type entry struct {
// Associated service name
Name string
// Associated service instance
Service Service
// serving indicates that service is currently serving, todo: needs mutex
mu sync.Mutex
serving bool
}
// serving returns true if service is serving.
func (e *entry) isServing() bool {
e.mu.Lock()
defer e.mu.Unlock()
return e.serving
}
// setStarted indicates that service is serving.
func (e *entry) setStarted() {
e.mu.Lock()
defer e.mu.Unlock()
e.serving = true
}
// setStopped indicates that service is being stopped.
func (e *entry) setStopped() {
e.mu.Lock()
defer e.mu.Unlock()
e.serving = false
}
// NewRegistry creates new registry.
func NewRegistry(log logrus.FieldLogger) Container {
return ®istry{
log: log,
candidates: make([]*entry, 0),
}
}
// Register add new service to the registry under given name.
func (r *registry) Register(name string, service Service) {
r.mu.Lock()
defer r.mu.Unlock()
r.candidates = append(r.candidates, &entry{
Name: name,
Service: service,
serving: false,
})
r.log.Debugf("%s.service: registered", name)
}
// Reconfigure configures all underlying services with given configuration.
func (r *registry) Configure(cfg Config) error {
if r.configured != nil {
return fmt.Errorf("service bus has been already configured")
}
r.configured = make([]*entry, 0)
for _, e := range r.candidates {
segment := cfg.Get(e.Name)
if segment == nil {
r.log.Debugf("%s.service: no config has been provided", e.Name)
continue
}
_, err := e.Service.Configure(segment, r)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("%s.service", e.Name))
}
//if s != nil {
// r.configured = append(r.configured, &entry{
// Name: e.Name,
// Service: s,
// serving: false,
// })
//}
}
return nil
}
// Check is Service has been registered.
func (r *registry) Has(service string) bool {
r.mu.Lock()
defer r.mu.Unlock()
for _, e := range r.configured {
if e.Name == service {
return true
}
}
return false
}
// Get returns Service instance by it's Name or nil if Service not found.
func (r *registry) Get(service string) Service {
r.mu.Lock()
defer r.mu.Unlock()
for _, e := range r.configured {
if e.Name == service {
return e.Service
}
}
return nil
}
// Serve all configured services. Non blocking.
func (r *registry) Serve() error {
if len(r.configured) == 0 {
return errors.New("no services attached")
}
done := make(chan interface{}, len(r.configured))
defer close(done)
for _, s := range r.configured {
go func(s *entry) {
defer s.setStopped()
s.setStarted()
if err := s.Service.Serve(); err != nil {
done <- err
}
}(s)
}
for i := 0; i < len(r.configured); i++ {
result := <-done
// found an error in one of the services, stopping the rest of running services.
if err, ok := result.(error); ok {
r.Stop()
return err
}
}
return nil
}
// Stop sends stop command to all running services.
func (r *registry) Stop() {
for _, s := range r.configured {
if s.isServing() {
s.Service.Stop()
}
}
}
|