-
Notifications
You must be signed in to change notification settings - Fork 55
/
curator.go
291 lines (250 loc) · 9.02 KB
/
curator.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
/*
Copyright (c) 2014 Ashley Jeffs
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, sub to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package curator
import (
"errors"
"fmt"
"sync"
"time"
"github.com/Jeffail/leaps/lib/acl"
"github.com/Jeffail/leaps/lib/audit"
"github.com/Jeffail/leaps/lib/binder"
"github.com/Jeffail/leaps/lib/store"
"github.com/Jeffail/leaps/lib/util/service/log"
"github.com/Jeffail/leaps/lib/util/service/metrics"
)
//------------------------------------------------------------------------------
// Config - Holds configuration options for a curator.
type Config struct {
BinderConfig binder.Config `json:"binder" yaml:"binder"`
}
// NewConfig - Returns a fully defined curator configuration with the default
// values for each field.
func NewConfig() Config {
return Config{
BinderConfig: binder.NewConfig(),
}
}
//------------------------------------------------------------------------------
// Errors for the Curator type.
var (
ErrBinderNotFound = errors.New("binder was not found")
)
// Impl - The underlying implementation of the curator type. Creates and manages
// the entire lifecycle of binders internally.
type Impl struct {
config Config
store store.Type
auth acl.Authenticator
auditors AuditorContainer
log log.Modular
stats metrics.Type
// Binders
openBinders map[string]binder.Type
binderMutex sync.RWMutex
// Control channels
errorChan chan binder.Error
closeChan chan struct{}
closedChan chan struct{}
}
// New - Creates and returns a new curator, and launches its internal loop.
func New(
config Config,
log log.Modular,
stats metrics.Type,
auth acl.Authenticator,
store store.Type,
auditors AuditorContainer,
) (*Impl, error) {
curator := Impl{
config: config,
store: store,
log: log.NewModule(":curator"),
stats: stats,
auth: auth,
auditors: auditors,
openBinders: make(map[string]binder.Type),
errorChan: make(chan binder.Error, 10),
closeChan: make(chan struct{}),
closedChan: make(chan struct{}),
}
go curator.loop()
return &curator, nil
}
// Close - Shut the curator and all subsequent binders down. This call blocks
// until the shut down is finished, and you must ensure that this curator cannot
// be accessed after closing.
func (c *Impl) Close() {
c.log.Debugln("Close called")
c.closeChan <- struct{}{}
<-c.closedChan
}
/*
loop - The main loop of the curator. Two channels are listened to:
- Error channel, used by active binders to request a shut down, either due to
inactivity or an error having occurred. The curator then calls close on it and
removes it from the list of binders.
- Close channel, used by the owner of the curator to instigate a clean shut
down. The curator then forwards to call to all binders and closes itself.
*/
func (c *Impl) loop() {
c.log.Debugln("Loop called")
for {
select {
case err := <-c.errorChan:
if err.Err != nil {
c.stats.Incr("curator.binder_chan.error", 1)
c.log.Errorf("Binder (%v) %v\n", err.ID, err.Err)
} else {
c.log.Infof("Binder (%v) has requested shutdown\n", err.ID)
}
c.binderMutex.Lock()
if b, ok := c.openBinders[err.ID]; ok {
b.Close()
delete(c.openBinders, err.ID)
c.log.Infof("Binder (%v) was closed\n", err.ID)
c.stats.Incr("curator.binder_shutdown.success", 1)
c.stats.Decr("curator.open_binders", 1)
} else {
c.log.Errorf("Binder (%v) was not located in map\n", err.ID)
c.stats.Incr("curator.binder_shutdown.error", 1)
}
c.binderMutex.Unlock()
case <-c.closeChan:
c.log.Infoln("Received call to close, forwarding message to binders")
c.binderMutex.Lock()
for _, b := range c.openBinders {
b.Close()
c.stats.Decr("curator.open_binders", 1)
}
c.binderMutex.Unlock()
close(c.closedChan)
return
}
}
}
func (c *Impl) newBinder(id string) (binder.Type, error) {
var auditor audit.Auditor
var err error
if c.auditors != nil {
if auditor, err = c.auditors.Get(id); err != nil {
return nil, fmt.Errorf("failed to create auditor: %v", err)
}
}
return binder.New(
id, c.store, c.config.BinderConfig, c.errorChan, c.log, c.stats, auditor,
)
}
//------------------------------------------------------------------------------
// EditDocument - Locates or creates a Binder for an existing document and
// returns that Binder for subscribing to. Returns an error if there was a
// problem locating the document.
func (c *Impl) EditDocument(
userMetadata interface{}, token, documentID string, timeout time.Duration,
) (binder.Portal, error) {
c.log.Debugf("finding document %v, with userMetadata %v token %v\n", documentID, userMetadata, token)
if c.auth.Authenticate(userMetadata, token, documentID) < acl.EditAccess {
c.stats.Incr("curator.edit.rejected_client", 1)
return nil, fmt.Errorf(
"failed to authorise join of document id: %v with token: %v", documentID, token,
)
}
c.stats.Incr("curator.edit.accepted_client", 1)
c.binderMutex.Lock()
// Check for existing binder
if openBinder, ok := c.openBinders[documentID]; ok {
c.binderMutex.Unlock()
return openBinder.Subscribe(userMetadata, timeout)
}
openBinder, err := c.newBinder(documentID)
if err != nil {
c.binderMutex.Unlock()
c.stats.Incr("curator.bind_new.failed", 1)
c.log.Errorf("Failed to bind to document %v: %v\n", documentID, err)
return nil, err
}
c.openBinders[documentID] = openBinder
c.binderMutex.Unlock()
c.stats.Incr("curator.open_binders", 1)
return openBinder.Subscribe(userMetadata, timeout)
}
// ReadDocument - Locates or creates a Binder for an existing document and
// returns that Binder for subscribing to with read only privileges. Returns an
// error if there was a problem locating the document.
func (c *Impl) ReadDocument(
userMetadata interface{}, token, documentID string, timeout time.Duration,
) (binder.Portal, error) {
c.log.Debugf("finding document %v, with userMetadata %v token %v\n", documentID, userMetadata, token)
if c.auth.Authenticate(userMetadata, token, documentID) < acl.ReadAccess {
c.stats.Incr("curator.read.rejected_client", 1)
return nil, fmt.Errorf(
"failed to authorise read only join of document id: %v with token: %v",
documentID, token,
)
}
c.stats.Incr("curator.read.accepted_client", 1)
c.binderMutex.Lock()
// Check for existing binder
if openBinder, ok := c.openBinders[documentID]; ok {
c.binderMutex.Unlock()
return openBinder.SubscribeReadOnly(userMetadata, timeout)
}
openBinder, err := c.newBinder(documentID)
if err != nil {
c.binderMutex.Unlock()
c.stats.Incr("curator.bind_existing.failed", 1)
c.log.Errorf("Failed to bind to document %v: %v\n", documentID, err)
return nil, err
}
c.openBinders[documentID] = openBinder
c.binderMutex.Unlock()
c.stats.Incr("curator.open_binders", 1)
return openBinder.SubscribeReadOnly(userMetadata, timeout)
}
// CreateDocument - Creates a fresh Binder for a new document, which is
// subsequently stored, returns an error if either the document ID is already
// currently in use, or if there is a problem storing the new document. May
// require authentication, if so a userMetadata is supplied.
func (c *Impl) CreateDocument(
userMetadata interface{}, token string, doc store.Document, timeout time.Duration,
) (binder.Portal, error) {
c.log.Debugf("Creating new document with userMetadata %v token %v\n", userMetadata, token)
if c.auth.Authenticate(userMetadata, token, "") < acl.CreateAccess {
c.stats.Incr("curator.create.rejected_client", 1)
return nil, fmt.Errorf("failed to gain permission to create with token: %v", token)
}
c.stats.Incr("curator.create.accepted_client", 1)
if err := c.store.Create(doc); err != nil {
c.stats.Incr("curator.create_new.failed", 1)
c.log.Errorf("Failed to create new document: %v\n", err)
return nil, err
}
openBinder, err := c.newBinder(doc.ID)
if err != nil {
c.stats.Incr("curator.bind_new.failed", 1)
c.log.Errorf("Failed to bind to new document: %v\n", err)
return nil, err
}
c.binderMutex.Lock()
c.openBinders[doc.ID] = openBinder
c.binderMutex.Unlock()
c.stats.Incr("curator.open_binders", 1)
return openBinder.Subscribe(userMetadata, timeout)
}
//------------------------------------------------------------------------------