This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
/
event_stream.go
404 lines (376 loc) · 10.1 KB
/
event_stream.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package api
import (
"encoding/json"
"fmt"
"os"
"os/signal"
"reflect"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/digitalrebar/logger"
"github.com/VictorLowther/jsonpatch2/utils"
"github.com/digitalrebar/provision/v4/models"
"github.com/gorilla/websocket"
)
// TestFunc is a function type used to test if an item matches some
// sort of condition.
type TestFunc func(interface{}) (bool, error)
// AndItems retuens a TestFunc that returns true if all the passed-in
// TestFuncs also return true.
func AndItems(fs ...TestFunc) TestFunc {
return func(ref interface{}) (bool, error) {
for _, f := range fs {
if b, e := f(ref); e != nil {
return false, e
} else if !b {
return false, nil
}
}
return true, nil
}
}
// OrItems returns a TestFunc that returns true if any of the
// passed-in TestFuncs return true.
func OrItems(fs ...TestFunc) TestFunc {
return func(ref interface{}) (bool, error) {
for _, f := range fs {
if b, e := f(ref); e != nil {
return false, e
} else if b {
return true, e
}
}
return false, nil
}
}
// NotItems returns a TestFunc that returns the opposite of the
// passed-in TestFunc.
func NotItem(f TestFunc) TestFunc {
return func(ref interface{}) (bool, error) {
b, e := f(ref)
return !b, e
}
}
// EqualItem creates a test function to see if a value in
// the passed interface is equal
func EqualItem(field string, value interface{}) TestFunc {
// Convert the value to a json remarshaled value
var nv interface{}
if err := utils.Remarshal(value, &nv); err != nil {
return func(ref interface{}) (bool, error) {
return false, err
}
}
return func(ref interface{}) (bool, error) {
fields := map[string]interface{}{}
if err := utils.Remarshal(ref, &fields); err != nil {
return false, err
}
if d, ok := fields[field]; ok {
return reflect.DeepEqual(d, nv), nil
}
return false, nil
}
}
func (c *Client) ws() (*websocket.Conn, error) {
return c.Websocket("ws")
}
// RecievedEvent contains an event received from the digitalrebar
// provision server along with any errors that occurred while
// receiving the event.
type RecievedEvent struct {
E models.Event
Err error
}
// ValueInList parses a list string for matches with escapes.
//
// \1 is the dot character.
// \2 is the comma character.
// \3 is the asterisk character.
func ValueInList(val, list string) bool {
for _, p := range strings.Split(list, ",") {
p = strings.Replace(p, "\\1", ".", -1)
p = strings.Replace(p, "\\2", ",", -1)
p = strings.Replace(p, "\\3", "*", -1)
if strings.TrimSpace(p) == val {
return true
}
}
return false
}
func (r *RecievedEvent) matches(registration string) bool {
tak := strings.SplitN(registration, ".", 3)
if len(tak) != 3 {
return false
}
return (tak[0] == "*" || ValueInList(r.E.Type, tak[0])) &&
(tak[1] == "*" || ValueInList(r.E.Action, tak[1])) &&
(tak[2] == "*" || ValueInList(r.E.Key, tak[2]))
}
// EventStream receives events from the digitalrebar provider. You
// can read received events by reading from its Events channel.
type EventStream struct {
logger.Logger
src string
client *Client
handleId int64
conn *websocket.Conn
subscriptions map[string][]int64
receivers map[int64]chan RecievedEvent
mux *sync.Mutex
kill chan struct{}
rchan chan RecievedEvent
}
func (es *EventStream) processEvents(running chan struct{}) {
close(running)
for {
_, msg, err := es.conn.NextReader()
if err != nil {
es.conn.Close()
es.mux.Lock()
for h, receiver := range es.receivers {
receiver <- RecievedEvent{Err: err}
close(receiver)
es.receivers[h] = nil
}
es.mux.Unlock()
return
}
evt := RecievedEvent{}
evt.Err = json.NewDecoder(msg).Decode(&evt.E)
toSend := map[int64]chan RecievedEvent{}
es.mux.Lock()
for reg, handles := range es.subscriptions {
if !evt.matches(reg) {
continue
}
for _, i := range handles {
if toSend[i] == nil {
toSend[i] = es.receivers[i]
}
}
}
for i := range toSend {
select {
case toSend[i] <- evt:
default:
es.Errorf("Failed to send an event")
}
}
es.mux.Unlock()
}
}
// Events creates a new EventStream from the client.
func (c *Client) Events() (*EventStream, error) {
conn, err := c.ws()
if err != nil {
return nil, err
}
res := &EventStream{
Logger: c.Logger.SetPrincipal("events"),
client: c,
conn: conn,
subscriptions: map[string][]int64{},
receivers: map[int64]chan RecievedEvent{},
mux: &sync.Mutex{},
kill: make(chan struct{}, 1),
}
newID := atomic.AddInt64(&res.handleId, 1)
res.rchan = make(chan RecievedEvent, 100)
res.subscriptions["websocket.*.*"] = []int64{newID}
res.receivers[newID] = res.rchan
running := make(chan struct{})
go res.processEvents(running)
<-running
return res, nil
}
// Close closes down the EventStream. You should drain the Events
// until you read a RecievedEvent that has an empty E and a non-nil
// Err
func (es *EventStream) Close() error {
return es.conn.WriteMessage(websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
}
func (es *EventStream) subscribe(handle int64, events ...string) (int, error) {
if es.receivers[handle] == nil {
return 0, fmt.Errorf("No such handle %d", handle)
}
count := 0
for _, evt := range events {
handles := es.subscriptions[evt]
if handles == nil {
handles = []int64{}
}
idx := sort.Search(len(handles), func(i int) bool { return handles[i] >= handle })
if idx == len(handles) {
handles = append(handles, handle)
} else if handles[idx] == handle {
continue
} else {
handles = append(handles, 0)
copy(handles[idx+1:], handles[idx:])
handles[idx] = handle
}
if es.subscriptions[evt] == nil {
if err := es.conn.WriteMessage(websocket.TextMessage, []byte("register "+evt)); err != nil {
return count, err
}
count += 1
}
es.subscriptions[evt] = handles
}
return count, nil
}
// Subscribe directs an EventStream to listen for matching events.
func (es *EventStream) Subscribe(handle int64, events ...string) error {
es.mux.Lock()
count, err := es.subscribe(handle, events...)
es.mux.Unlock()
if err == nil {
// Really wait should be for my specific one, but ...
// Multi-threaded apps will have issues.
for i := 0; i < count; i++ {
<-es.rchan
}
}
return err
}
// Register directs the EventStream to subscribe to Events from the digital rebar provisioner.
//
// Event subscriptions consist of a string with the following format:
//
// type.action.key
//
// type is the object type that you want to listen for events about.
// * means to listen for events about all object types.
//
// action is the action that caused the event to be created. * means
// to listen for all actions.
//
// key is the unique identifier of the object to listen for. * means
// to listen for events from all objects
func (es *EventStream) Register(events ...string) (int64, <-chan RecievedEvent, error) {
newID := atomic.AddInt64(&es.handleId, 1)
es.mux.Lock()
ch := make(chan RecievedEvent, 100)
es.receivers[newID] = ch
count, err := es.subscribe(newID, events...)
es.mux.Unlock()
if err == nil {
// Really wait should be for my specific one, but ...
// Multi-threaded apps will have issues.
for i := 0; i < count; i++ {
<-es.rchan
}
}
return newID, ch, err
}
func (es *EventStream) deregister(handle int64) (int, error) {
ch, ok := es.receivers[handle]
if !ok {
return 0, fmt.Errorf("No such handle %d", handle)
}
count := 0
for evt, handles := range es.subscriptions {
idx := sort.Search(len(handles), func(i int) bool { return handles[i] >= handle })
if idx == len(handles) || handles[idx] != handle {
continue
} else if idx != len(handles)-1 {
copy(handles[idx:], handles[idx+1:])
}
handles = handles[:len(handles)-1]
es.subscriptions[evt] = handles
if len(handles) == 0 {
count += 1
es.conn.WriteMessage(websocket.TextMessage, []byte("deregister "+evt))
delete(es.subscriptions, evt)
}
}
delete(es.receivers, handle)
if ch != nil {
close(ch)
}
return count, nil
}
// Deregister directs the EventStream to unsubscribe from Events from
// the digitalrebar provisioner. It takes the same parameters as
// Register.
func (es *EventStream) Deregister(handle int64) error {
es.mux.Lock()
count, err := es.deregister(handle)
es.mux.Unlock()
// Really wait should be for my specific one, but ...
// Multi-threaded apps will have issues.
for i := 0; i < count; i++ {
<-es.rchan
}
return err
}
func (es *EventStream) Kill() {
es.kill <- struct{}{}
}
// WaitFor waits for an item to match test. It subscribes to an
// EventStream that watches all update and save envents for the object
// in question, and returns a string indicating whether the match
// succeeded, failed, or timed out.
//
// The API for this function is subject to refactoring and change, and
// should not be considered to be stable yet.
func (es *EventStream) WaitFor(
item models.Model,
test TestFunc,
timeout time.Duration) (string, error) {
// Make some basic vars
prefix := item.Prefix()
id := item.Key()
interrupt := make(chan os.Signal, 1)
evts := []string{prefix + ".update." + id, prefix + ".save." + id}
// Handle interrupt signal while selecting
signal.Notify(interrupt, os.Interrupt)
defer signal.Reset(os.Interrupt)
// Register for events
handle, ch, err := es.Register(evts...)
if err != nil {
return "", err
}
defer es.Deregister(handle)
// Setup the timer
timer := time.NewTimer(timeout)
defer func() {
if timer != nil && !timer.Stop() {
<-timer.C
}
}()
for {
if err := es.client.FillModel(item, id); err != nil {
return fmt.Sprintf("fill: %v", err), err
}
found, err := test(item)
if found && err == nil {
return "complete", nil
}
if err != nil {
return fmt.Sprintf("test: %v", err), err
}
select {
case <-es.kill:
return "interrupt", nil
case evt := <-ch:
if evt.Err != nil {
return fmt.Sprintf("read: %v", err), err
}
if err != nil {
return fmt.Sprintf("read: %v", err), err
}
case <-interrupt:
return "interrupt", nil
case <-timer.C:
timer.Stop()
timer = nil
return "timeout", nil
}
}
}