-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
192 lines (150 loc) · 3.87 KB
/
update.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
182
183
184
185
186
187
188
189
190
191
192
// Copyright (c) 2022, Janoš Guljaš <janos@resenje.org>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package feed
import (
"sync"
)
// Update defines a set of subscriptions per topic T which receive messages sent
// to the Update.
type Update[T comparable, M any] struct {
subscriptions map[T][]*updateSubscription[T, M]
mu sync.RWMutex
wg sync.WaitGroup
quit chan struct{}
quitOnce sync.Once
}
// NewFeed constructs new Feed with topic type T and message type M.
func NewUpdate[T comparable, M any]() *Update[T, M] {
return &Update[T, M]{
subscriptions: make(map[T][]*updateSubscription[T, M]),
quit: make(chan struct{}),
}
}
// Subscribe returns a channel from which messages M, that are sent to the Feed
// on the same topic, can be read from. Message delivery preserves ordering and
// is guaranteed, so the channel should be read to avoid keeping unread messages
// in memory. After cancel function call, all resources ang goroutines are
// released even if not all messages are read from channel.
func (u *Update[T, M]) Subscribe(topic T) (c <-chan M, cancel func()) {
channel := make(chan M)
select {
case <-u.quit:
close(channel)
return channel, func() {}
default:
}
u.mu.Lock()
defer u.mu.Unlock()
s := newUpdateSubscription(u, channel)
u.subscriptions[topic] = append(u.subscriptions[topic], s)
return channel, func() { u.unsubscribe(topic, s) }
}
func (u *Update[T, M]) unsubscribe(topic T, s *updateSubscription[T, M]) {
u.mu.Lock()
defer u.mu.Unlock()
for i, sub := range u.subscriptions[topic] {
if sub == s {
u.subscriptions[topic] = append(u.subscriptions[topic][:i], u.subscriptions[topic][i+1:]...)
s.close()
}
}
}
// Close terminates all subscriptions and releases acquired resources.
func (u *Update[T, M]) Close() error {
u.quitOnce.Do(func() {
close(u.quit)
})
u.wg.Wait()
u.mu.Lock()
defer u.mu.Unlock()
for topic, subscriptions := range u.subscriptions {
for _, s := range subscriptions {
s.close()
}
u.subscriptions[topic] = nil
}
return nil
}
// Send sends a message to all sunscribed channels to topic. Messages will be
// delivered to subscribers when each of them is ready to receive it, without
// blocking this method call. The returned integer is the number of subscribers
// that should receive the message.
func (u *Update[T, M]) Send(topic T, message M) (n int) {
u.mu.RLock()
defer u.mu.RUnlock()
for _, s := range u.subscriptions[topic] {
s.send(message)
n++
}
return n
}
type updateSubscription[T comparable, M any] struct {
feed *Update[T, M]
channel chan M
update chan M
updated chan struct{}
quit chan struct{}
wg sync.WaitGroup
}
func newUpdateSubscription[T comparable, M any](u *Update[T, M], channel chan M) *updateSubscription[T, M] {
return &updateSubscription[T, M]{
feed: u,
channel: channel,
update: make(chan M),
updated: make(chan struct{}),
quit: make(chan struct{}),
}
}
func (s *updateSubscription[T, M]) send(message M) {
select {
case s.channel <- message:
case s.update <- message:
select {
case <-s.updated:
case <-s.quit:
case <-s.feed.quit:
}
return
case <-s.quit:
return
case <-s.feed.quit:
return
default:
ready := make(chan struct{})
done := make(chan struct{})
channel := s.channel
s.wg.Add(1)
go func() {
defer s.wg.Done()
defer close(done)
for {
select {
case channel <- message:
return
case message = <-s.update:
channel = nil
case ready <- struct{}{}:
case s.updated <- struct{}{}:
channel = s.channel
case <-s.quit:
return
case <-s.feed.quit:
return
}
}
}()
select {
case <-ready:
case <-done:
case <-s.quit:
case <-s.feed.quit:
}
}
}
func (s *updateSubscription[T, M]) close() {
close(s.quit)
s.wg.Wait()
close(s.channel)
}