-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
bus.go
145 lines (113 loc) · 2.94 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
package messagebus
import (
"fmt"
"reflect"
"sync"
)
// MessageBus implements publish/subscribe messaging paradigm
type MessageBus interface {
// Publish publishes arguments to the given topic subscribers
// Publish block only when the buffer of one of the subscribers is full.
Publish(topic string, args ...interface{})
// Close unsubscribe all handlers from given topic
Close(topic string)
// Subscribe subscribes to the given topic
Subscribe(topic string, fn interface{}) error
// Unsubscribe unsubscribe handler from the given topic
Unsubscribe(topic string, fn interface{}) error
}
type handlersMap map[string][]*handler
type handler struct {
callback reflect.Value
queue chan []reflect.Value
}
type messageBus struct {
handlerQueueSize int
mtx sync.RWMutex
handlers handlersMap
}
func (b *messageBus) Publish(topic string, args ...interface{}) {
rArgs := buildHandlerArgs(args)
b.mtx.RLock()
defer b.mtx.RUnlock()
if hs, ok := b.handlers[topic]; ok {
for _, h := range hs {
h.queue <- rArgs
}
}
}
func (b *messageBus) Subscribe(topic string, fn interface{}) error {
if err := isValidHandler(fn); err != nil {
return err
}
h := &handler{
callback: reflect.ValueOf(fn),
queue: make(chan []reflect.Value, b.handlerQueueSize),
}
go func() {
for args := range h.queue {
h.callback.Call(args)
}
}()
b.mtx.Lock()
defer b.mtx.Unlock()
b.handlers[topic] = append(b.handlers[topic], h)
return nil
}
func (b *messageBus) Unsubscribe(topic string, fn interface{}) error {
if err := isValidHandler(fn); err != nil {
return err
}
rv := reflect.ValueOf(fn)
b.mtx.Lock()
defer b.mtx.Unlock()
if _, ok := b.handlers[topic]; ok {
for i, h := range b.handlers[topic] {
if h.callback == rv {
close(h.queue)
if len(b.handlers[topic]) == 1 {
delete(b.handlers, topic)
} else {
b.handlers[topic] = append(b.handlers[topic][:i], b.handlers[topic][i+1:]...)
}
}
}
return nil
}
return fmt.Errorf("topic %s doesn't exist", topic)
}
func (b *messageBus) Close(topic string) {
b.mtx.Lock()
defer b.mtx.Unlock()
if _, ok := b.handlers[topic]; ok {
for _, h := range b.handlers[topic] {
close(h.queue)
}
delete(b.handlers, topic)
return
}
}
func isValidHandler(fn interface{}) error {
if reflect.TypeOf(fn).Kind() != reflect.Func {
return fmt.Errorf("%s is not a reflect.Func", reflect.TypeOf(fn))
}
return nil
}
func buildHandlerArgs(args []interface{}) []reflect.Value {
reflectedArgs := make([]reflect.Value, 0)
for _, arg := range args {
reflectedArgs = append(reflectedArgs, reflect.ValueOf(arg))
}
return reflectedArgs
}
// New creates new MessageBus
// handlerQueueSize sets buffered channel length per subscriber
func New(handlerQueueSize int) MessageBus {
if handlerQueueSize == 0 {
panic("handlerQueueSize has to be greater then 0")
}
return &messageBus{
handlerQueueSize: handlerQueueSize,
handlers: make(handlersMap),
}
}