-
Notifications
You must be signed in to change notification settings - Fork 35
/
bus.go
181 lines (148 loc) · 3.29 KB
/
bus.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
package bus
import (
"fmt"
"sync"
"github.com/CyCoreSystems/ari-proxy/v5/messagebus"
"github.com/CyCoreSystems/ari/v5"
"github.com/inconshreveable/log15"
)
// EventChanBufferLength is the number of unhandled events which can be queued
// to the event channel buffer before further events are lost.
var EventChanBufferLength = 10
// Bus provides an ari.Bus interface to MessageBus
type Bus struct {
prefix string
log log15.Logger
mbus messagebus.Client
}
// New returns a new Bus
func New(prefix string, m messagebus.Client, log log15.Logger) *Bus {
return &Bus{
prefix: prefix,
log: log,
mbus: m,
}
}
func (b *Bus) subjectFromKey(key *ari.Key) string {
if key == nil {
return fmt.Sprintf(
"%sevent.%s",
b.prefix,
b.mbus.GetWildcardString(messagebus.WildcardZeroOrMoreWords),
)
}
if key.Dialog != "" {
return fmt.Sprintf("%sdialogevent.%s", b.prefix, key.Dialog)
}
subj := fmt.Sprintf("%sevent.", b.prefix)
if key.App == "" {
return subj + b.mbus.GetWildcardString(messagebus.WildcardZeroOrMoreWords)
}
subj += key.App + "."
if key.Node == "" {
return subj + b.mbus.GetWildcardString(messagebus.WildcardZeroOrMoreWords)
}
return subj + key.Node
}
// Subscription represents an ari.Subscription over MessageBus
type Subscription struct {
key *ari.Key
log log15.Logger
subscription messagebus.Subscription
eventChan chan ari.Event
events []string
closed bool
mu sync.RWMutex
}
// Close implements ari.Bus
func (b *Bus) Close() {
// No-op
}
// Send implements ari.Bus
func (b *Bus) Send(e ari.Event) {
// No-op
}
// Subscribe implements ari.Bus
func (b *Bus) Subscribe(key *ari.Key, n ...string) ari.Subscription {
var err error
s := &Subscription{
key: key,
log: b.log,
eventChan: make(chan ari.Event, EventChanBufferLength),
events: n,
}
var app string
if key != nil {
app = key.App
}
s.subscription, err = b.mbus.SubscribeEvent(
b.subjectFromKey(key),
app,
s.receive,
)
if err != nil {
b.log.Error("failed to subscribe to MessageBus", "error", err)
return nil
}
return s
}
// Events returns the channel on which events from this subscription will be sent
func (s *Subscription) Events() <-chan ari.Event {
return s.eventChan
}
// Cancel destroys the subscription
func (s *Subscription) Cancel() {
if s == nil {
return
}
if s.subscription != nil {
err := s.subscription.Unsubscribe()
if err != nil {
s.log.Error("failed unsubscribe from MessageBus", "error", err)
}
}
s.mu.Lock()
if !s.closed {
s.closed = true
close(s.eventChan)
}
s.mu.Unlock()
}
func (s *Subscription) receive(data []byte) {
e, err := ari.DecodeEvent(data)
if err != nil {
s.log.Error("failed to convert received message to ari.Event", "error", err)
return
}
if s.matchEvent(e) {
s.mu.RLock()
if !s.closed {
s.eventChan <- e
}
s.mu.RUnlock()
}
}
func (s *Subscription) matchEvent(o ari.Event) bool {
// First, filter by type
var match bool
for _, kind := range s.events {
if kind == o.GetType() || kind == ari.Events.All {
match = true
break
}
}
if !match {
return false
}
// If we don't have a resource ID, we match everything
// Next, match the entity
if s.key == nil || s.key.ID == "" {
return true
}
for _, k := range o.Keys() {
if s.key.Match(k) {
return true
}
}
return false
}