-
Notifications
You must be signed in to change notification settings - Fork 290
/
subscriber.go
196 lines (166 loc) · 4.4 KB
/
subscriber.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
193
194
195
196
package store
import (
"context"
"fmt"
"sync"
)
// A subscriber is notified whenever the state changes.
//
// Subscribers do not need to be thread-safe. The Store will only
// call OnChange for a given subscriber when the last call completes.
//
// Subscribers are only allowed to read state. If they want to
// modify state, they should call store.Dispatch()
type Subscriber interface {
OnChange(ctx context.Context, st RStore)
}
// Some subscribers need to do SetUp or TearDown.
//
// Both hold the subscriber lock, so should return quickly.
//
// SetUp and TearDown are called in serial.
// SetUp is called in FIFO order while TearDown is LIFO so that
// inter-subscriber dependencies are respected.
type SetUpper interface {
// Initialize the subscriber.
//
// Any errors will trigger an ErrorAction.
SetUp(ctx context.Context, st RStore) error
}
type TearDowner interface {
TearDown(ctx context.Context)
}
// Convenience interface for subscriber fulfilling both SetUpper and TearDowner
type SubscriberLifecycle interface {
SetUpper
TearDowner
}
type subscriberList struct {
subscribers []*subscriberEntry
setup bool
mu sync.Mutex
}
func (l *subscriberList) Add(ctx context.Context, st RStore, s Subscriber) error {
l.mu.Lock()
defer l.mu.Unlock()
e := &subscriberEntry{
subscriber: s,
}
l.subscribers = append(l.subscribers, e)
if l.setup {
// the rest of the subscriberList has already been set up, so set up this subscriber directly
return e.maybeSetUp(ctx, st)
}
return nil
}
func (l *subscriberList) Remove(ctx context.Context, s Subscriber) error {
l.mu.Lock()
defer l.mu.Unlock()
for i, current := range l.subscribers {
if s == current.subscriber {
l.subscribers = append(l.subscribers[:i], l.subscribers[i+1:]...)
if l.setup {
current.maybeTeardown(ctx)
}
return nil
}
}
return fmt.Errorf("Subscriber not found: %T: %+v", s, s)
}
func (l *subscriberList) SetUp(ctx context.Context, st RStore) error {
l.mu.Lock()
subscribers := append([]*subscriberEntry{}, l.subscribers...)
l.setup = true
l.mu.Unlock()
for _, s := range subscribers {
err := s.maybeSetUp(ctx, st)
if err != nil {
return err
}
}
return nil
}
// TeardownAll removes subscribes in the reverse order as they were subscribed.
func (l *subscriberList) TeardownAll(ctx context.Context) {
l.mu.Lock()
subscribers := append([]*subscriberEntry{}, l.subscribers...)
l.setup = false
l.mu.Unlock()
for i := len(subscribers) - 1; i >= 0; i-- {
subscribers[i].maybeTeardown(ctx)
}
}
func (l *subscriberList) NotifyAll(ctx context.Context, store *Store) {
l.mu.Lock()
subscribers := append([]*subscriberEntry{}, l.subscribers...)
l.mu.Unlock()
for _, s := range subscribers {
s := s
isPending := s.claimPending()
if isPending {
SafeGo(store, func() {
s.notify(ctx, store)
})
}
}
}
type subscriberEntry struct {
subscriber Subscriber
// At any given time, there are at most two goroutines
// notifying the subscriber: a pending goroutine and an active goroutine.
hasPending bool
hasActive bool
// The active mutex is held by the goroutine currently notifying the
// subscriber. It may be held for a long time if the subscriber
// takes a long time.
activeMu sync.Mutex
// The state mutex is just for updating the hasPending/hasActive state.
// It should never be held a long time.
stateMu sync.Mutex
}
// Returns true if this is the pending goroutine.
// Returns false to do nothing.
func (e *subscriberEntry) claimPending() bool {
e.stateMu.Lock()
defer e.stateMu.Unlock()
if e.hasPending {
return false
}
e.hasPending = true
return true
}
func (e *subscriberEntry) movePendingToActive() {
e.stateMu.Lock()
defer e.stateMu.Unlock()
e.hasPending = false
e.hasActive = true
}
func (e *subscriberEntry) clearActive() {
e.stateMu.Lock()
defer e.stateMu.Unlock()
e.hasActive = false
}
func (e *subscriberEntry) notify(ctx context.Context, store *Store) {
e.activeMu.Lock()
defer e.activeMu.Unlock()
e.movePendingToActive()
e.subscriber.OnChange(ctx, store)
e.clearActive()
}
func (e *subscriberEntry) maybeSetUp(ctx context.Context, st RStore) error {
s, ok := e.subscriber.(SetUpper)
if ok {
e.activeMu.Lock()
defer e.activeMu.Unlock()
return s.SetUp(ctx, st)
}
return nil
}
func (e *subscriberEntry) maybeTeardown(ctx context.Context) {
s, ok := e.subscriber.(TearDowner)
if ok {
e.activeMu.Lock()
defer e.activeMu.Unlock()
s.TearDown(ctx)
}
}