Skip to content

Commit 1bed9b3

Browse files
fjlkaralabe
authored andcommitted
event: address review issues (multiple commits)
event: address Feed review issues event: clarify role of NewSubscription function event: more Feed review fixes * take sendLock after dropping f.mu * add constant for number of special cases event: fix subscribing/unsubscribing while Send is blocked
1 parent a2b4abd commit 1bed9b3

3 files changed

Lines changed: 93 additions & 16 deletions

File tree

‎event/feed.go‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ var errBadChannel = errors.New("event: Subscribe argument does not have sendable
3333
//
3434
// The zero value is ready to use.
3535
type Feed struct {
36-
sendLock chan struct{} // one-element buffer, empty when held
36+
// sendLock has a one-element buffer and is empty when held.
37+
// It protects sendCases.
38+
sendLock chan struct{}
3739
removeSub chan interface{} // interrupts Send
3840
sendCases caseList // the active set of select cases used by Send
3941

@@ -44,6 +46,10 @@ type Feed struct {
4446
closed bool
4547
}
4648

49+
// This is the index of the first actual subscription channel in sendCases.
50+
// sendCases[0] is a SelectRecv case for the removeSub channel.
51+
const firstSubSendCase = 1
52+
4753
type feedTypeError struct {
4854
got, want reflect.Type
4955
op string
@@ -67,6 +73,7 @@ func (f *Feed) init() {
6773
// until the subscription is canceled. All channels added must have the same element type.
6874
//
6975
// The channel should have ample buffer space to avoid blocking other subscribers.
76+
// Slow subscribers are not dropped.
7077
func (f *Feed) Subscribe(channel interface{}) Subscription {
7178
chanval := reflect.ValueOf(channel)
7279
chantyp := chanval.Type()
@@ -125,13 +132,14 @@ func (f *Feed) remove(sub *feedSub) {
125132
func (f *Feed) Send(value interface{}) (nsent int) {
126133
f.mu.Lock()
127134
f.init()
135+
f.mu.Unlock()
136+
128137
<-f.sendLock
129-
// Add new subscriptions from the inbox, then clear it.
138+
139+
// Add new cases from the inbox after taking the send lock.
140+
f.mu.Lock()
130141
f.sendCases = append(f.sendCases, f.inbox...)
131-
for i := range f.inbox {
132-
f.inbox[i] = reflect.SelectCase{}
133-
}
134-
f.inbox = f.inbox[:0]
142+
f.inbox = nil
135143
f.mu.Unlock()
136144

137145
// Set the sent value on all channels.
@@ -140,7 +148,7 @@ func (f *Feed) Send(value interface{}) (nsent int) {
140148
f.sendLock <- struct{}{}
141149
panic(feedTypeError{op: "Send", got: rvalue.Type(), want: f.etype})
142150
}
143-
for i := 1; i < len(f.sendCases); i++ {
151+
for i := firstSubSendCase; i < len(f.sendCases); i++ {
144152
f.sendCases[i].Send = rvalue
145153
}
146154

@@ -150,13 +158,14 @@ func (f *Feed) Send(value interface{}) (nsent int) {
150158
// Fast path: try sending without blocking before adding to the select set.
151159
// This should usually succeed if subscribers are fast enough and have free
152160
// buffer space.
153-
for i := 1; i < len(cases); i++ {
161+
for i := firstSubSendCase; i < len(cases); i++ {
154162
if cases[i].Chan.TrySend(rvalue) {
155-
cases = cases.deactivate(i)
156163
nsent++
164+
cases = cases.deactivate(i)
165+
i--
157166
}
158167
}
159-
if len(cases) == 1 {
168+
if len(cases) == firstSubSendCase {
160169
break
161170
}
162171
// Select on all the receivers, waiting for them to unblock.
@@ -174,7 +183,7 @@ func (f *Feed) Send(value interface{}) (nsent int) {
174183
}
175184

176185
// Forget about the sent value and hand off the send lock.
177-
for i := 1; i < len(f.sendCases); i++ {
186+
for i := firstSubSendCase; i < len(f.sendCases); i++ {
178187
f.sendCases[i].Send = reflect.Value{}
179188
}
180189
f.sendLock <- struct{}{}

‎event/feed_test.go‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,74 @@ func TestFeedSubscribeSameChannel(t *testing.T) {
167167
done.Wait()
168168
}
169169

170+
func TestFeedSubscribeBlockedPost(t *testing.T) {
171+
var (
172+
feed Feed
173+
nsends = 2000
174+
ch1 = make(chan int)
175+
ch2 = make(chan int)
176+
wg sync.WaitGroup
177+
)
178+
defer wg.Wait()
179+
180+
feed.Subscribe(ch1)
181+
wg.Add(nsends)
182+
for i := 0; i < nsends; i++ {
183+
go func() {
184+
feed.Send(99)
185+
wg.Done()
186+
}()
187+
}
188+
189+
sub2 := feed.Subscribe(ch2)
190+
defer sub2.Unsubscribe()
191+
192+
// We're done when ch1 has received N times.
193+
// The number of receives on ch2 depends on scheduling.
194+
for i := 0; i < nsends; {
195+
select {
196+
case <-ch1:
197+
i++
198+
case <-ch2:
199+
}
200+
}
201+
}
202+
203+
func TestFeedUnsubscribeBlockedPost(t *testing.T) {
204+
var (
205+
feed Feed
206+
nsends = 200
207+
chans = make([]chan int, 2000)
208+
subs = make([]Subscription, len(chans))
209+
bchan = make(chan int)
210+
bsub = feed.Subscribe(bchan)
211+
wg sync.WaitGroup
212+
)
213+
for i := range chans {
214+
chans[i] = make(chan int, nsends)
215+
}
216+
217+
// Queue up some Sends. None of these can make progress while bchan isn't read.
218+
wg.Add(nsends)
219+
for i := 0; i < nsends; i++ {
220+
go func() {
221+
feed.Send(99)
222+
wg.Done()
223+
}()
224+
}
225+
// Subscribe the other channels.
226+
for i, ch := range chans {
227+
subs[i] = feed.Subscribe(ch)
228+
}
229+
// Unsubscribe them again.
230+
for _, sub := range subs {
231+
sub.Unsubscribe()
232+
}
233+
// Unblock the Sends.
234+
bsub.Unsubscribe()
235+
wg.Wait()
236+
}
237+
170238
func TestFeedUnsubscribeFromInbox(t *testing.T) {
171239
var (
172240
feed Feed

‎event/subscription.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ type Subscription interface {
4343
Unsubscribe() // cancels sending of events, closing the error channel
4444
}
4545

46-
// NewSubscription runs fn as a subscription in a new goroutine. The channel given to fn
47-
// is closed when Unsubscribe is called. If fn returns an error, it is sent on the
48-
// subscription's error channel.
49-
func NewSubscription(fn func(<-chan struct{}) error) Subscription {
46+
// NewSubscription runs a producer function as a subscription in a new goroutine. The
47+
// channel given to the producer is closed when Unsubscribe is called. If fn returns an
48+
// error, it is sent on the subscription's error channel.
49+
func NewSubscription(producer func(<-chan struct{}) error) Subscription {
5050
s := &funcSub{unsub: make(chan struct{}), err: make(chan error, 1)}
5151
go func() {
5252
defer close(s.err)
53-
err := fn(s.unsub)
53+
err := producer(s.unsub)
5454
s.mu.Lock()
5555
defer s.mu.Unlock()
5656
if !s.unsubscribed {

0 commit comments

Comments
 (0)