-
Notifications
You must be signed in to change notification settings - Fork 9
/
simplemultiplexer.go
106 lines (91 loc) · 3.02 KB
/
simplemultiplexer.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
// Copyright 2019 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package pubsub
import (
"sync"
)
// TODO: refactor Multiplexer type for structured hub to be based off this type.
// SimpleMultiplexer allows multiple subscriptions to be made sharing a single
// message queue from the hub. This means that all the messages for the
// various subscriptions are called back in the order that the messages were
// published. If more than one handler is added to the SimpleMultiplexer that
// matches any given topic, the handlers are called back one after the other
// in the order that they were added.
type SimpleMultiplexer interface {
Add(topic string, handler func(string, interface{}))
AddMatch(matcher func(string) bool, handler func(string, interface{}))
Unsubscribe()
}
type simpleElement struct {
matcher func(string) bool
handler func(string, interface{})
}
type simpleMultiplexer struct {
logger Logger
mu sync.Mutex
outputs []simpleElement
unsubscriber func()
}
// NewMultiplexer creates a new multiplexer for the hub and subscribes it.
// Unsubscribing the multiplexer stops calls for all handlers added.
func (h *SimpleHub) NewMultiplexer() SimpleMultiplexer {
mp := &simpleMultiplexer{logger: h.logger}
unsub := h.SubscribeMatch(mp.match, mp.callback)
mp.unsubscriber = unsub
return mp
}
// Add a handler for a specific topic.
func (m *simpleMultiplexer) Add(topic string, handler func(string, interface{})) {
m.AddMatch(equalTopic(topic), handler)
}
// AddMatch adds another handler for any topic that matches the matcher.
func (m *simpleMultiplexer) AddMatch(matcher func(string) bool, handler func(string, interface{})) {
// This mimics the behaviour of the simple hub itself.
if handler == nil || matcher == nil {
// It is safe but useless.
return
}
m.mu.Lock()
m.outputs = append(m.outputs, simpleElement{matcher: matcher, handler: handler})
m.mu.Unlock()
}
// Unsubscribe the multiplexer from the simple hub.
func (m *simpleMultiplexer) Unsubscribe() {
m.mu.Lock()
unsubscriber := m.unsubscriber
m.unsubscriber = nil
m.mu.Unlock()
if multiUnsubscribeTestHook != nil {
multiUnsubscribeTestHook()
}
if unsubscriber != nil {
unsubscriber()
}
}
func (m *simpleMultiplexer) callback(topic string, data interface{}) {
// Since the callback functions have arbitrary code, don't hold the
// mutex for the duration of the calls.
m.mu.Lock()
outputs := make([]simpleElement, len(m.outputs))
copy(outputs, m.outputs)
m.mu.Unlock()
for _, element := range outputs {
if element.matcher(topic) {
element.handler(topic, data)
}
}
}
// If any of the topic matchers added for the handlers match the topic, the
// simpleMultiplexer matches.
func (m *simpleMultiplexer) match(topic string) bool {
// Here we explicitly don't make a copy of the outputs as the match
// function is going to be called much more often than the callback func.
m.mu.Lock()
defer m.mu.Unlock()
for _, element := range m.outputs {
if element.matcher(topic) {
return true
}
}
return false
}