-
Notifications
You must be signed in to change notification settings - Fork 122
/
subscriptionmgr.go
167 lines (132 loc) · 3.07 KB
/
subscriptionmgr.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
// Copyright 2021-2024 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package storage
import (
"context"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
const eventBufferSize = 32
type SubscriptionManager struct {
eventChan chan Event
subscribers map[string]Subscriber
mu sync.RWMutex
once sync.Once
}
func NewSubscriptionManager(ctx context.Context) *SubscriptionManager {
sm := &SubscriptionManager{
eventChan: make(chan Event, eventBufferSize),
subscribers: make(map[string]Subscriber),
}
go sm.handleEvents(ctx)
return sm
}
func (sm *SubscriptionManager) handleEvents(ctx context.Context) {
for {
select {
case <-ctx.Done():
sm.shutdown()
return
case evt, ok := <-sm.eventChan:
if !ok {
return
}
sm.distributeEvent(evt)
}
}
}
func (sm *SubscriptionManager) distributeEvent(evt Event) {
sm.mu.RLock()
defer sm.mu.RUnlock()
for _, sub := range sm.subscribers {
// TODO(cell) Use work pool to notify multiple subscribers in parallel.
sub.OnStorageEvent(evt)
}
}
// Notify sends the events to all subscribers.
func (sm *SubscriptionManager) NotifySubscribers(events ...Event) {
for _, evt := range events {
if evt.Kind == EventNop {
continue
}
// TODO(cell) drop event if not published within a reasonable time period.
sm.eventChan <- evt
}
}
func (sm *SubscriptionManager) Subscribe(s Subscriber) {
if sm == nil {
return
}
sm.mu.Lock()
defer sm.mu.Unlock()
sm.subscribers[s.SubscriberID()] = s
}
func (sm *SubscriptionManager) Unsubscribe(s Subscriber) {
if sm == nil {
return
}
sm.mu.Lock()
defer sm.mu.Unlock()
delete(sm.subscribers, s.SubscriberID())
}
func (sm *SubscriptionManager) shutdown() {
sm.once.Do(func() { close(sm.eventChan) })
}
// TestSubscription is a helper to test subscriptions.
func TestSubscription(s Subscribable) func(*testing.T, time.Duration, ...Event) {
stream := make(chan Event, eventBufferSize)
sub := &subscriber{stream: stream}
s.Subscribe(sub)
return func(t *testing.T, timeout time.Duration, wantEvents ...Event) {
t.Helper()
timer := time.NewTimer(timeout)
t.Cleanup(func() {
close(stream)
timer.Stop()
})
var haveEvents []Event
for len(haveEvents) < len(wantEvents) {
select {
case evt := <-stream:
haveEvents = append(haveEvents, evt)
case <-timer.C:
t.Errorf("Timeout: expected %d events but only received %d", len(wantEvents), len(haveEvents))
}
}
s.Unsubscribe(sub)
require.ElementsMatch(t, wantEvents, haveEvents)
}
}
type subscriber struct {
stream chan Event
events []Event
mu sync.RWMutex
}
func (s *subscriber) SubscriberID() string {
return "test"
}
func (s *subscriber) OnStorageEvent(evt ...Event) {
s.mu.Lock()
s.events = append(s.events, evt...)
s.mu.Unlock()
for _, e := range evt {
select {
case s.stream <- e:
default:
}
}
}
func (s *subscriber) Events() []Event {
s.mu.RLock()
defer s.mu.RUnlock()
events := make([]Event, len(s.events))
copy(events, s.events)
return events
}
func (s *subscriber) Clear() {
s.mu.Lock()
defer s.mu.Unlock()
s.events = nil
}