-
Notifications
You must be signed in to change notification settings - Fork 8
/
publisher.go
228 lines (184 loc) · 5.04 KB
/
publisher.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
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
package kcache
import (
lifecycle "github.com/boz/go-lifecycle"
logutil "github.com/boz/go-logutil"
"github.com/boz/kcache/filter"
"github.com/pkg/errors"
)
type FilterController interface {
Controller
Refilter(filter.Filter) error
}
type publisher struct {
parent Subscription
subscribech chan chan<- Subscription
unsubscribech chan subscription
subscriptions map[subscription]struct{}
lc lifecycle.Lifecycle
log logutil.Log
}
func newPublisher(log logutil.Log, parent Subscription) Controller {
s := &publisher{
parent: parent,
subscribech: make(chan chan<- Subscription),
unsubscribech: make(chan subscription),
subscriptions: make(map[subscription]struct{}),
lc: lifecycle.New(),
log: log.WithComponent("publisher"),
}
go s.run()
return s
}
func (s *publisher) Ready() <-chan struct{} {
return s.parent.Ready()
}
func (s *publisher) Cache() CacheReader {
return s.parent.Cache()
}
func (s *publisher) Close() {
s.parent.Close()
}
func (s *publisher) Done() <-chan struct{} {
return s.lc.Done()
}
func (s *publisher) Error() error {
return s.lc.Error()
}
func (s *publisher) Subscribe() (Subscription, error) {
resultch := make(chan Subscription, 1)
select {
case <-s.lc.ShuttingDown():
return nil, errors.WithStack(ErrNotRunning)
case s.subscribech <- resultch:
return <-resultch, nil
}
}
func (s *publisher) SubscribeWithFilter(f filter.Filter) (FilterSubscription, error) {
sub, err := s.Subscribe()
if err != nil {
return nil, err
}
return newFilterSubscription(s.log, sub, f, false), nil
}
func (s *publisher) SubscribeForFilter() (FilterSubscription, error) {
sub, err := s.Subscribe()
if err != nil {
return nil, err
}
return newFilterSubscription(s.log, sub, filter.All(), true), nil
}
func (s *publisher) Clone() (Controller, error) {
sub, err := s.Subscribe()
if err != nil {
return nil, err
}
return newPublisher(s.log, sub), nil
}
func (s *publisher) CloneWithFilter(f filter.Filter) (FilterController, error) {
sub, err := s.SubscribeWithFilter(f)
if err != nil {
return nil, err
}
return newFilterPublisher(s.log, sub), nil
}
func (s *publisher) CloneForFilter() (FilterController, error) {
sub, err := s.SubscribeForFilter()
if err != nil {
return nil, err
}
return newFilterPublisher(s.log, sub), nil
}
func (s *publisher) run() {
defer s.lc.ShutdownCompleted()
loop:
for {
select {
case evt, ok := <-s.parent.Events():
if !ok {
s.log.Debugf("parent events closed")
s.lc.ShutdownInitiated(nil)
break loop
}
s.distributeEvent(evt)
case resultch := <-s.subscribech:
resultch <- s.createSubscription()
case sub := <-s.unsubscribech:
delete(s.subscriptions, sub)
}
}
for len(s.subscriptions) > 0 {
s.log.Debugf("draining: %v subscriptions", len(s.subscriptions))
select {
case sub := <-s.unsubscribech:
delete(s.subscriptions, sub)
}
}
<-s.parent.Done()
}
func (s *publisher) distributeEvent(evt Event) {
s.log.Debugf("distribute event: sending %v to %v subscriptions", evt, len(s.subscriptions))
for sub := range s.subscriptions {
sub.send(evt)
}
}
func (s *publisher) createSubscription() Subscription {
s.log.Debugf("create subscription: current count %v", len(s.subscriptions))
sub := newSubscription(s.log, s.lc.ShuttingDown(), s.parent.Ready(), s.parent.Cache())
s.subscriptions[sub] = struct{}{}
go func() {
select {
case <-sub.Done():
s.log.Debugf("create subscription: subscription done")
case <-s.lc.ShuttingDown():
s.log.Debugf("create subscription: shut down, killing subscription")
sub.Close()
<-sub.Done()
}
s.unsubscribech <- sub
s.log.Debugf("create subscription: unsubscribed")
}()
return sub
}
func newFilterPublisher(log logutil.Log, subscription FilterSubscription) FilterController {
return &filterController{subscription, newPublisher(log, subscription)}
}
type filterController struct {
subscription FilterSubscription
parent Controller
}
func (c *filterController) Cache() CacheReader {
return c.parent.Cache()
}
func (c *filterController) Ready() <-chan struct{} {
return c.parent.Ready()
}
func (c *filterController) Subscribe() (Subscription, error) {
return c.parent.Subscribe()
}
func (c *filterController) SubscribeWithFilter(f filter.Filter) (FilterSubscription, error) {
return c.parent.SubscribeWithFilter(f)
}
func (c *filterController) SubscribeForFilter() (FilterSubscription, error) {
return c.parent.SubscribeForFilter()
}
func (c *filterController) Clone() (Controller, error) {
return c.parent.Clone()
}
func (c *filterController) CloneWithFilter(f filter.Filter) (FilterController, error) {
return c.parent.CloneWithFilter(f)
}
func (c *filterController) CloneForFilter() (FilterController, error) {
return c.parent.CloneForFilter()
}
func (c *filterController) Done() <-chan struct{} {
return c.parent.Done()
}
func (c *filterController) Close() {
c.parent.Close()
}
func (c *filterController) Error() error {
return c.parent.Error()
}
func (c *filterController) Refilter(filter filter.Filter) error {
return c.subscription.Refilter(filter)
}