fix potential issue with closing asyn topic
This commit is contained in:
33
async.go
33
async.go
@@ -6,12 +6,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// AsyncTopic allows any message T to be broadcast to subscribers. Publishing as well as
|
// AsyncTopic allows any message T to be broadcast to subscribers. Publishing as well as
|
||||||
// subscribing happens asynchronously.
|
// subscribing happens asynchronously (as non-blocking as possible).
|
||||||
// This guarantees that every published message will be delivered but does NOT guarantee delivery
|
// Closing the topic guarantees that published message will be delivered and no further messages
|
||||||
// order.
|
// nor subscribers will be accepted.
|
||||||
// In the unlikely scenario where subscribers are being queued very aggressively it is possible
|
// Delivery order is NOT guaranteed.
|
||||||
// that some might never actually receive any message. Subscriber registration order is also not
|
|
||||||
// guaranteed.
|
|
||||||
type AsyncTopic[T any] struct {
|
type AsyncTopic[T any] struct {
|
||||||
options TopicOptions
|
options TopicOptions
|
||||||
|
|
||||||
@@ -23,12 +21,11 @@ type AsyncTopic[T any] struct {
|
|||||||
subscribeCh chan Subscriber[T]
|
subscribeCh chan Subscriber[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAsyncTopic creates an AsyncTopic that will be closed when the given context is cancelled.
|
// NewAsyncTopic creates an AsyncTopic.
|
||||||
// After closed calls to Publish or Subscribe will return an error.
|
|
||||||
func NewAsyncTopic[T any](opts ...TopicOption) *AsyncTopic[T] {
|
func NewAsyncTopic[T any](opts ...TopicOption) *AsyncTopic[T] {
|
||||||
options := TopicOptions{
|
options := TopicOptions{
|
||||||
onClose: func() {}, // Called after the Topic is closed and all messages have been delivered.
|
onClose: func() {}, // Called after the Topic is closed and all messages have been delivered.
|
||||||
onSubscribe: func(count int) {}, // Called everytime a new subscriber is added
|
onSubscribe: func() {}, // Called everytime a new subscriber is added
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@@ -53,13 +50,19 @@ func (t *AsyncTopic[T]) Close() {
|
|||||||
t.mu.RLock()
|
t.mu.RLock()
|
||||||
closing := t.closing
|
closing := t.closing
|
||||||
t.mu.RUnlock()
|
t.mu.RUnlock()
|
||||||
|
|
||||||
if closing {
|
if closing {
|
||||||
// It's either already closed or it's closing.
|
// It's either already closed or it's closing.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
|
if closing {
|
||||||
|
// It is possible that 2 go routines attempted to aquire the lock to close this topic.
|
||||||
|
t.mu.Unlock()
|
||||||
|
<-t.closed
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
t.closing = true // no more subscribing or publishing
|
t.closing = true // no more subscribing or publishing
|
||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
|
|
||||||
@@ -78,7 +81,7 @@ func (t *AsyncTopic[T]) run() {
|
|||||||
defer func() {
|
defer func() {
|
||||||
// There is only one way to get here: the topic is now closing!
|
// There is only one way to get here: the topic is now closing!
|
||||||
// Because both `subscribeCh` and `publishCh` channels are closed when the topic is closed
|
// Because both `subscribeCh` and `publishCh` channels are closed when the topic is closed
|
||||||
// this will always eventually return.
|
// we can assume this will always eventually return.
|
||||||
// This will deliver any potential queued message thus fulfilling the message delivery
|
// This will deliver any potential queued message thus fulfilling the message delivery
|
||||||
// promise.
|
// promise.
|
||||||
for msg := range t.publishCh {
|
for msg := range t.publishCh {
|
||||||
@@ -94,7 +97,7 @@ func (t *AsyncTopic[T]) run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribers = append(subscribers, newCallback)
|
subscribers = append(subscribers, newCallback)
|
||||||
t.options.onSubscribe(len(subscribers))
|
t.options.onSubscribe()
|
||||||
|
|
||||||
case msg, more := <-t.publishCh:
|
case msg, more := <-t.publishCh:
|
||||||
if !more {
|
if !more {
|
||||||
@@ -106,10 +109,12 @@ func (t *AsyncTopic[T]) run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Publish broadcasts a msg to all subscribers.
|
// Publish broadcasts a msg to all subscribers asynchronously.
|
||||||
func (t *AsyncTopic[T]) Publish(msg T) error {
|
func (t *AsyncTopic[T]) Publish(msg T) error {
|
||||||
t.mu.RLock()
|
t.mu.RLock()
|
||||||
|
|
||||||
|
// We hold the Read lock until we are done with publishing to avoid panic due to a closed channel.
|
||||||
|
|
||||||
if t.closing {
|
if t.closing {
|
||||||
t.mu.RUnlock()
|
t.mu.RUnlock()
|
||||||
return fmt.Errorf("async topic publish: %w", ErrTopicClosed)
|
return fmt.Errorf("async topic publish: %w", ErrTopicClosed)
|
||||||
@@ -123,7 +128,7 @@ func (t *AsyncTopic[T]) Publish(msg T) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe adds a Subscriber func that will consume future published messages.
|
// Subscribe registers a Subscriber func asynchronously.
|
||||||
func (t *AsyncTopic[T]) Subscribe(fn Subscriber[T]) error {
|
func (t *AsyncTopic[T]) Subscribe(fn Subscriber[T]) error {
|
||||||
t.mu.RLock()
|
t.mu.RLock()
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package gubgub
|
package gubgub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync/atomic"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -98,50 +97,24 @@ func TestAsyncTopic_MultiPublishersMultiSubscribers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAsyncTopic_WithOnClose(t *testing.T) {
|
func TestAsyncTopic_CloseIsIdempotent(t *testing.T) {
|
||||||
feedback := make(chan struct{}, 1)
|
topic := NewAsyncTopic[int]()
|
||||||
defer close(feedback)
|
|
||||||
|
|
||||||
topic := NewAsyncTopic[int](WithOnClose(func() { feedback <- struct{}{} }))
|
|
||||||
|
|
||||||
|
feedback := make(chan struct{})
|
||||||
|
go func() {
|
||||||
topic.Close()
|
topic.Close()
|
||||||
|
topic.Close()
|
||||||
|
close(feedback)
|
||||||
|
}()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-feedback:
|
case <-feedback:
|
||||||
break
|
return
|
||||||
|
|
||||||
case <-testTimer(t, time.Second).C:
|
case <-testTimer(t, time.Second).C:
|
||||||
t.Fatalf("expected feedback by now")
|
t.Fatalf("expected feedback by now")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAsyncTopic_WithOnSubscribe(t *testing.T) {
|
|
||||||
const totalSub = 10
|
|
||||||
|
|
||||||
feedback := make(chan int, totalSub)
|
|
||||||
defer close(feedback)
|
|
||||||
|
|
||||||
topic := NewAsyncTopic[int](WithOnSubscribe(func(count int) { feedback <- count }))
|
|
||||||
|
|
||||||
for range totalSub {
|
|
||||||
topic.Subscribe(func(i int) bool { return true })
|
|
||||||
}
|
|
||||||
|
|
||||||
count := 0
|
|
||||||
timeout := testTimer(t, time.Second)
|
|
||||||
|
|
||||||
for count < totalSub {
|
|
||||||
select {
|
|
||||||
case c := <-feedback:
|
|
||||||
count++
|
|
||||||
assert.Equal(t, count, c, "expected %d but got %d instead", count, c)
|
|
||||||
|
|
||||||
case <-timeout.C:
|
|
||||||
t.Fatalf("expected %d feedback items by now but only got %d", totalSub, count)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAsyncTopic_ClosedTopicError(t *testing.T) {
|
func TestAsyncTopic_ClosedTopicError(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -231,11 +204,10 @@ func withNotifyOnNthSubscriber(t testing.TB, n int64) (TopicOption, <-chan struc
|
|||||||
close(notify)
|
close(notify)
|
||||||
})
|
})
|
||||||
|
|
||||||
var counter atomic.Int64
|
var counter int64
|
||||||
|
return WithOnSubscribe(func() {
|
||||||
return WithOnSubscribe(func(count int) {
|
counter++
|
||||||
c := counter.Add(1)
|
if counter == n {
|
||||||
if c == n {
|
|
||||||
notify <- struct{}{}
|
notify <- struct{}{}
|
||||||
}
|
}
|
||||||
}), notify
|
}), notify
|
||||||
|
|||||||
Reference in New Issue
Block a user