forked from livekit/psrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus_local.go
165 lines (139 loc) · 2.92 KB
/
bus_local.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
package psrpc
import (
"context"
"sync"
"google.golang.org/protobuf/proto"
)
type localMessageBus struct {
sync.RWMutex
subs map[string]*localSubList
queues map[string]*localSubList
}
func NewLocalMessageBus() MessageBus {
return &localMessageBus{
subs: make(map[string]*localSubList),
queues: make(map[string]*localSubList),
}
}
func (l *localMessageBus) Publish(_ context.Context, channel string, msg proto.Message) error {
b, err := serialize(msg)
if err != nil {
return err
}
l.RLock()
subs := l.subs[channel]
queues := l.queues[channel]
l.RUnlock()
if subs != nil {
subs.publish(b)
}
if queues != nil {
queues.publish(b)
}
return nil
}
func (l *localMessageBus) Subscribe(_ context.Context, channel string, size int) (subInternal, error) {
return l.subscribe(l.subs, channel, size, false)
}
func (l *localMessageBus) SubscribeQueue(_ context.Context, channel string, size int) (subInternal, error) {
return l.subscribe(l.queues, channel, size, true)
}
func (l *localMessageBus) subscribe(subLists map[string]*localSubList, channel string, size int, queue bool) (subInternal, error) {
l.Lock()
defer l.Unlock()
subList := subLists[channel]
if subList == nil {
subList = &localSubList{queue: queue}
subList.onUnsubscribe = func(index int) {
// lock localMessageBus before localSubList
l.Lock()
subList.Lock()
close(subList.subs[index])
subList.subs[index] = nil
subList.subCount--
if subList.subCount == 0 {
delete(subLists, channel)
}
subList.Unlock()
l.Unlock()
}
subLists[channel] = subList
}
return subList.create(size), nil
}
type localSubList struct {
sync.RWMutex // locking while holding localMessageBus lock is allowed
subs []chan []byte
subCount int
queue bool
next int
onUnsubscribe func(int)
}
func (l *localSubList) create(size int) *localSubscription {
msgChan := make(chan []byte, size)
l.Lock()
defer l.Unlock()
l.subCount++
added := false
index := 0
for i, s := range l.subs {
if s == nil {
added = true
index = i
l.subs[i] = msgChan
break
}
}
if !added {
index = len(l.subs)
l.subs = append(l.subs, msgChan)
}
return &localSubscription{
msgChan: msgChan,
onClose: func() {
l.onUnsubscribe(index)
},
}
}
func (l *localSubList) publish(b []byte) {
if l.queue {
l.Lock()
defer l.Unlock()
// round-robin
for i := 0; i <= len(l.subs); i++ {
if l.next >= len(l.subs) {
l.next = 0
}
s := l.subs[l.next]
l.next++
if s != nil {
s <- b
return
}
}
} else {
l.RLock()
defer l.RUnlock()
// send to all
for _, s := range l.subs {
if s != nil {
s <- b
}
}
}
}
type localSubscription struct {
msgChan chan []byte
onClose func()
}
func (l *localSubscription) read() ([]byte, bool) {
msg, ok := <-l.msgChan
if !ok {
return nil, false
}
return msg, true
}
func (l *localSubscription) Close() error {
l.onClose()
return nil
}