-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchsub.go
More file actions
233 lines (212 loc) · 5.22 KB
/
chsub.go
File metadata and controls
233 lines (212 loc) · 5.22 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Package chsub implements a channel subscriber pattern.
package chsub
import (
"errors"
"sync"
)
var (
// ErrRegistered indicates that the topic has already been registered.
ErrRegistered = errors.New("name has been registered")
// ErrNotRegistered indicates that the subscribed topic is not registered.
ErrNotRegistered = errors.New("name has not been registered")
// ErrTopicClosed indicates that the topic is closed.
ErrTopicClosed = errors.New("topic is closed")
// ErrChannelNotFound indicates that the channel is not found in subscribers.
ErrChannelNotFound = errors.New("channel not found in subscribers")
// ErrChannelFull indicates that the channel is full.
ErrChannelFull = errors.New("channel is full")
)
// Publish defines a function type for publishing data to a topic.
type Publish func(any) error
// subscriber represents a single subscriber to a topic.
// It holds the channel for receiving messages and the subscriber's configuration.
type subscriber struct {
ch chan any
cfg SubscriberConfig
}
// topic represents a topic instance with a source channel and destination channels.
type topic struct {
source chan any
subscribers []subscriber
subLock sync.RWMutex
closeChan chan struct{}
TopicOptions
}
func newTopic(opts ...TopicOption) *topic {
options := TopicOptions{
length: 1, // Default length
}
for _, opt := range opts {
opt(&options)
}
ch := make(chan any, options.length)
t := &topic{
source: ch,
subscribers: make([]subscriber, 0),
closeChan: make(chan struct{}),
TopicOptions: options,
}
go t.run()
return t
}
func (t *topic) run() {
defer close(t.source)
for {
select {
case v := <-t.source:
_ = t.publish(v)
case <-t.closeChan:
for _, sub := range t.subscribers {
close(sub.ch)
}
return
}
}
}
func (t *topic) publish(v any) error {
t.subLock.RLock()
defer t.subLock.RUnlock()
select {
case <-t.closeChan:
return ErrTopicClosed
default:
}
for _, sub := range t.subscribers {
select {
case sub.ch <- v:
default:
if sub.cfg.onDrop != nil {
sub.cfg.onDrop(v)
}
}
}
return nil
}
// Sub implements a simple publish/subscribe model.
type Sub struct {
topics map[string]*topic
lock sync.RWMutex
}
// NewSub creates a new Sub instance.
func NewSub() *Sub {
return &Sub{
topics: map[string]*topic{},
}
}
// NewTopic registers a new upstream channel for a topic.
// Returns a Publish function for publishing data to the topic and an error if the topic is already registered.
func (s *Sub) NewTopic(topic string, opts ...TopicOption) (Publish, error) {
s.lock.Lock()
defer s.lock.Unlock()
if _, ok := s.topics[topic]; ok {
return nil, ErrRegistered
}
t := newTopic(opts...)
s.topics[topic] = t
return publishFunc(t.source), nil
}
// Sub subscribes a downstream channel to a specified topic.
// Returns the subscribed channel and an error if the topic is not registered.
func (s *Sub) Sub(topic string, ch chan any, opts ...SubOption) error {
s.lock.RLock()
t, ok := s.topics[topic]
s.lock.RUnlock()
if !ok {
return ErrNotRegistered
}
cfg := SubscriberConfig{}
for _, opt := range opts {
opt(&cfg)
}
t.subLock.Lock()
t.subscribers = append(t.subscribers, subscriber{ch, cfg})
t.subLock.Unlock()
return nil
}
// Unsub removes a subscriber channel from a specified topic.
// Returns an error if the topic or channel is not found.
func (s *Sub) Unsub(topic string, ch chan any) error {
s.lock.Lock()
defer s.lock.Unlock()
t, ok := s.topics[topic]
if !ok {
return ErrNotRegistered
}
t.subLock.Lock()
defer t.subLock.Unlock()
for i, sub := range t.subscribers {
if sub.ch == ch {
t.subscribers = append(t.subscribers[:i], t.subscribers[i+1:]...)
return nil
}
}
return ErrChannelNotFound
}
// UnsubAll unsubscribes the given channel from all topics.
func (s *Sub) UnsubAll(ch chan any) {
s.lock.RLock()
topics := make([]*topic, 0, len(s.topics))
for _, t := range s.topics {
topics = append(topics, t)
}
s.lock.RUnlock()
for _, t := range topics {
t.subLock.Lock()
for i, sub := range t.subscribers {
if sub.ch == ch {
t.subscribers = append(t.subscribers[:i], t.subscribers[i+1:]...)
break
}
}
t.subLock.Unlock()
}
}
// Close terminates all topics and releases associated resources.
func (s *Sub) Close() {
s.lock.Lock()
defer s.lock.Unlock()
for _, t := range s.topics {
t.closeChan <- struct{}{}
}
s.topics = nil
}
// GetTopicsByChan returns the names of topics subscribed by the given channel.
func (s *Sub) GetTopicsByChan(ch chan any) []string {
s.lock.RLock()
defer s.lock.RUnlock()
var topics []string
for name, t := range s.topics {
t.subLock.RLock()
for _, sub := range t.subscribers {
if sub.ch == ch {
topics = append(topics, name)
break
}
}
t.subLock.RUnlock()
}
return topics
}
// CloseTopic closes a specified topic and releases its resources.
// Returns an error if the topic is not found.
func (s *Sub) CloseTopic(topic string) error {
s.lock.Lock()
defer s.lock.Unlock()
t, ok := s.topics[topic]
if !ok {
return ErrNotRegistered
}
t.closeChan <- struct{}{}
delete(s.topics, topic)
return nil
}
func publishFunc(ch chan any) Publish {
return func(i any) error {
select {
case ch <- i:
return nil
default:
return ErrChannelFull
}
}
}