-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbus.go
More file actions
192 lines (152 loc) · 3.76 KB
/
bus.go
File metadata and controls
192 lines (152 loc) · 3.76 KB
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
package bus
import (
"fmt"
"sync"
"github.com/CyCoreSystems/ari/v5"
"github.com/CyCoreSystems/ari/v5/stdbus"
"github.com/inconshreveable/log15"
"github.com/pkg/errors"
"github.com/nats-io/nats.go"
)
// busWrapper binds a NATS subject to an ari.Bus, passing any received NATS messages to that bus
type busWrapper struct {
subject string
log log15.Logger
sub *nats.Subscription
bus ari.Bus
}
func newBusWrapper(subject string, nc *nats.EncodedConn, log log15.Logger) (*busWrapper, error) {
var err error
w := &busWrapper{
subject: subject,
log: log,
bus: stdbus.New(),
}
w.sub, err = nc.Subscribe(subject, func(m *nats.Msg) {
w.receive(m)
})
if err != nil {
return nil, errors.Wrapf(err, "failed to subscribe to NATS subject %s", subject)
}
return w, nil
}
func (w *busWrapper) receive(o *nats.Msg) {
e, err := ari.DecodeEvent(o.Data)
if err != nil {
w.log.Error("failed to convert received message to ari.Event", "error", err)
return
}
w.bus.Send(e)
}
func (w *busWrapper) Close() {
if err := w.sub.Unsubscribe(); err != nil {
w.log.Error("failed to unsubscribe when closing NATS subscription:", err)
}
w.bus.Close()
}
// Bus provides an ari.Bus interface to NATS
type Bus struct {
prefix string
log log15.Logger
nc *nats.EncodedConn
subjectBuses map[string]*busWrapper
mu sync.RWMutex
}
// New returns a new Bus
func New(prefix string, nc *nats.EncodedConn, log log15.Logger) *Bus {
return &Bus{
prefix: prefix,
log: log,
nc: nc,
subjectBuses: make(map[string]*busWrapper),
}
}
type subBus struct {
bus ari.Bus
subs []ari.Subscription
mu sync.Mutex
}
func (b *subBus) Close() {
for _, s := range b.subs {
s.Cancel()
}
b.subs = nil
// NOTE: we are NOT closing the parent bus here and now, since it could be used by any number of other clients
// TODO: Ultimately, we will need to derive a way to check to see if the parent bus is then unused, in which case, the NATS subscription(s) should then be closed.
}
// Used as callback from stdbus
func (b *subBus) Cancel(s interface{}) {
b.mu.Lock()
for i, si := range b.subs {
if s == si {
b.subs[i] = b.subs[len(b.subs)-1] // replace the current with the end
b.subs[len(b.subs)-1] = nil // remove the end
b.subs = b.subs[:len(b.subs)-1] // lop off the end
break
}
}
b.mu.Unlock()
}
func (b *subBus) Send(e ari.Event) {
b.bus.Send(e)
}
func (b *subBus) Subscribe(key *ari.Key, eTypes ...string) ari.Subscription {
sub := b.bus.Subscribe(key, eTypes...)
sub.AddCancelCallback(b.Cancel)
b.mu.Lock()
b.subs = append(b.subs, sub)
b.mu.Unlock()
return sub
}
// SubBus creates and returns a new ariBus which is subtended from this one
func (b *Bus) SubBus() ari.Bus {
return &subBus{
bus: b,
}
}
func (b *Bus) subjectFromKey(key *ari.Key) string {
if key == nil {
return fmt.Sprintf("%sevent.>", b.prefix)
}
if key.Dialog != "" {
return fmt.Sprintf("%sdialogevent.%s", b.prefix, key.Dialog)
}
subj := fmt.Sprintf("%sevent.", b.prefix)
if key.App == "" {
return subj + ">"
}
subj += key.App + "."
if key.Node == "" {
return subj + ">"
}
return subj + key.Node
}
// Close implements ari.Bus
func (b *Bus) Close() {
b.mu.Lock()
for _, w := range b.subjectBuses {
w.Close()
}
b.mu.Unlock()
}
// 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
subject := b.subjectFromKey(key)
b.mu.Lock()
w, ok := b.subjectBuses[subject]
if !ok {
w, err = newBusWrapper(subject, b.nc, b.log)
if err != nil {
b.log.Error("failed to create bus wrapper", "key", key, "error", err)
return nil
}
b.subjectBuses[subject] = w
}
b.mu.Unlock()
return w.bus.Subscribe(key, n...)
}