fixed shutdown logic
This commit is contained in:
49
async.go
49
async.go
@@ -6,7 +6,9 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AsyncTopic allows any message T to be broadcast to all subscribers.
|
// AsyncTopic allows any message T to be broadcast to subscribers. Publishing as well as
|
||||||
|
// subscribing happens asynchronously. Due to the nature of async processes this cannot guarantee
|
||||||
|
// message delivery nor delivery order.
|
||||||
type AsyncTopic[T any] struct {
|
type AsyncTopic[T any] struct {
|
||||||
options AsyncTopicOptions
|
options AsyncTopicOptions
|
||||||
|
|
||||||
@@ -17,7 +19,7 @@ type AsyncTopic[T any] struct {
|
|||||||
subscribeCh chan Subscriber[T]
|
subscribeCh chan Subscriber[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAsyncTopic creates a Topic that publishes messages asynchronously.
|
// NewAsyncTopic creates an AsyncTopic that will be closed when the given context is cancelled.
|
||||||
func NewAsyncTopic[T any](ctx context.Context, opts ...AsyncTopicOption) *AsyncTopic[T] {
|
func NewAsyncTopic[T any](ctx context.Context, opts ...AsyncTopicOption) *AsyncTopic[T] {
|
||||||
options := AsyncTopicOptions{
|
options := AsyncTopicOptions{
|
||||||
onClose: func() {},
|
onClose: func() {},
|
||||||
@@ -40,8 +42,6 @@ func NewAsyncTopic[T any](ctx context.Context, opts ...AsyncTopicOption) *AsyncT
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *AsyncTopic[T]) run(ctx context.Context) {
|
func (t *AsyncTopic[T]) run(ctx context.Context) {
|
||||||
defer close(t.publishCh)
|
|
||||||
defer close(t.subscribeCh)
|
|
||||||
defer t.close()
|
defer t.close()
|
||||||
|
|
||||||
var subscribers []Subscriber[T]
|
var subscribers []Subscriber[T]
|
||||||
@@ -73,34 +73,65 @@ func (t *AsyncTopic[T]) run(ctx context.Context) {
|
|||||||
// Publish broadcasts a msg to all subscribers.
|
// Publish broadcasts a msg to all subscribers.
|
||||||
func (t *AsyncTopic[T]) Publish(msg T) error {
|
func (t *AsyncTopic[T]) Publish(msg T) error {
|
||||||
t.mu.RLock()
|
t.mu.RLock()
|
||||||
defer t.mu.RUnlock()
|
|
||||||
|
|
||||||
if t.closed {
|
if t.closed {
|
||||||
|
t.mu.RUnlock()
|
||||||
return fmt.Errorf("async topic publish: %w", ErrTopicClosed)
|
return fmt.Errorf("async topic publish: %w", ErrTopicClosed)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.publishCh <- msg
|
go func() {
|
||||||
|
t.publishCh <- msg
|
||||||
|
t.mu.RUnlock()
|
||||||
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe adds a Subscriber func that will consume future published messages.
|
// Subscribe adds a Subscriber func that will consume future published messages.
|
||||||
func (t *AsyncTopic[T]) Subscribe(fn Subscriber[T]) error {
|
func (t *AsyncTopic[T]) Subscribe(fn Subscriber[T]) error {
|
||||||
t.mu.RLock()
|
t.mu.RLock()
|
||||||
defer t.mu.RUnlock()
|
|
||||||
|
|
||||||
if t.closed {
|
if t.closed {
|
||||||
|
t.mu.RUnlock()
|
||||||
return fmt.Errorf("async topic subscribe: %w", ErrTopicClosed)
|
return fmt.Errorf("async topic subscribe: %w", ErrTopicClosed)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.subscribeCh <- fn
|
go func() {
|
||||||
|
t.subscribeCh <- fn
|
||||||
|
t.mu.RUnlock()
|
||||||
|
}()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *AsyncTopic[T]) close() {
|
func (t *AsyncTopic[T]) close() {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
wg.Add(2)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
for range t.publishCh {
|
||||||
|
// drain publishCh to release all read locks
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
for range t.subscribeCh {
|
||||||
|
// drain subscribeCh to release all read locks
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
t.closed = true
|
t.closed = true // no more subscribing or publishing
|
||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
|
|
||||||
|
close(t.publishCh)
|
||||||
|
close(t.subscribeCh)
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
t.options.onClose()
|
t.options.onClose()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
104
async_test.go
104
async_test.go
@@ -10,28 +10,34 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestAsyncTopic_SinglePublisherSingleSubscriber(t *testing.T) {
|
func TestAsyncTopic_SinglePublisherSingleSubscriber(t *testing.T) {
|
||||||
const msgCount = 5
|
const msgCount = 100
|
||||||
|
|
||||||
topic := NewAsyncTopic[int](context.Background())
|
subscriberReady := make(chan struct{})
|
||||||
|
defer close(subscriberReady)
|
||||||
|
|
||||||
feedback := make(chan int, msgCount)
|
topic := NewAsyncTopic[int](context.Background(), WithOnSubscribe(func(count int) {
|
||||||
|
subscriberReady <- struct{}{}
|
||||||
|
}))
|
||||||
|
|
||||||
|
feedback := make(chan struct{}, msgCount)
|
||||||
defer close(feedback)
|
defer close(feedback)
|
||||||
|
|
||||||
err := topic.Subscribe(func(i int) bool {
|
err := topic.Subscribe(func(i int) bool {
|
||||||
feedback <- i
|
feedback <- struct{}{}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
for i := 0; i < msgCount; i++ {
|
<-subscriberReady
|
||||||
|
|
||||||
|
for i := range msgCount {
|
||||||
require.NoError(t, topic.Publish(i))
|
require.NoError(t, topic.Publish(i))
|
||||||
}
|
}
|
||||||
|
|
||||||
count := 0
|
count := 0
|
||||||
for count < msgCount {
|
for count < msgCount {
|
||||||
select {
|
select {
|
||||||
case f := <-feedback:
|
case <-feedback:
|
||||||
assert.Equalf(t, count, f, "expected to get %d but got %d instead", count, f)
|
|
||||||
count++
|
count++
|
||||||
|
|
||||||
case <-time.After(time.Second):
|
case <-time.After(time.Second):
|
||||||
@@ -47,7 +53,14 @@ func TestAsyncTopic_MultiPublishersMultiSubscribers(t *testing.T) {
|
|||||||
msgCount = pubCount * 100 // total messages to publish (delivered to EACH subscriber)
|
msgCount = pubCount * 100 // total messages to publish (delivered to EACH subscriber)
|
||||||
)
|
)
|
||||||
|
|
||||||
topic := NewAsyncTopic[int](context.Background())
|
subscribersReady := make(chan struct{})
|
||||||
|
defer close(subscribersReady)
|
||||||
|
|
||||||
|
topic := NewAsyncTopic[int](context.Background(), WithOnSubscribe(func(count int) {
|
||||||
|
if count == subCount {
|
||||||
|
subscribersReady <- struct{}{}
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
expFeedbackCount := msgCount * subCount
|
expFeedbackCount := msgCount * subCount
|
||||||
feedback := make(chan int, expFeedbackCount)
|
feedback := make(chan int, expFeedbackCount)
|
||||||
@@ -67,6 +80,8 @@ func TestAsyncTopic_MultiPublishersMultiSubscribers(t *testing.T) {
|
|||||||
}
|
}
|
||||||
close(toDeliver)
|
close(toDeliver)
|
||||||
|
|
||||||
|
<-subscribersReady
|
||||||
|
|
||||||
for range pubCount {
|
for range pubCount {
|
||||||
go func() {
|
go func() {
|
||||||
for msg := range toDeliver {
|
for msg := range toDeliver {
|
||||||
@@ -135,52 +150,45 @@ func TestAsyncTopic_WithOnSubscribe(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAsyncTopic_SubscribeClosedTopicError(t *testing.T) {
|
func TestAsyncTopic_ClosedTopicError(t *testing.T) {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
testCases := []struct {
|
||||||
defer cancel()
|
name string
|
||||||
|
assertFn func(*AsyncTopic[int])
|
||||||
feedback := make(chan struct{})
|
}{
|
||||||
defer close(feedback)
|
{
|
||||||
|
name: "publishing returns error",
|
||||||
topic := NewAsyncTopic[int](ctx, WithOnClose(func() { feedback <- struct{}{} }))
|
assertFn: func(topic *AsyncTopic[int]) {
|
||||||
|
assert.Error(t, ErrTopicClosed, topic.Publish(1))
|
||||||
require.NoError(t, topic.Subscribe(func(i int) bool { return true }))
|
},
|
||||||
|
},
|
||||||
cancel() // this should close the topic, no more subscribers should be accepted
|
{
|
||||||
|
name: "subscribing returns error",
|
||||||
select {
|
assertFn: func(topic *AsyncTopic[int]) {
|
||||||
case <-feedback:
|
assert.Error(t, ErrTopicClosed, topic.Subscribe(func(i int) bool { return true }))
|
||||||
break
|
},
|
||||||
|
},
|
||||||
case <-time.After(time.Second):
|
|
||||||
t.Fatalf("expected feedback by now")
|
|
||||||
}
|
}
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
require.Error(t, ErrTopicClosed, topic.Subscribe(func(i int) bool { return true }))
|
feedback := make(chan struct{})
|
||||||
}
|
defer close(feedback)
|
||||||
|
|
||||||
func TestAsyncTopic_PublishClosedTopicError(t *testing.T) {
|
topic := NewAsyncTopic[int](ctx, WithOnClose(func() { feedback <- struct{}{} }))
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
feedback := make(chan struct{})
|
cancel() // this should close the topic, no more messages can be published
|
||||||
defer close(feedback)
|
|
||||||
|
|
||||||
topic := NewAsyncTopic[int](ctx, WithOnClose(func() { feedback <- struct{}{} }))
|
select {
|
||||||
|
case <-feedback:
|
||||||
|
break
|
||||||
|
|
||||||
require.NoError(t, topic.Subscribe(func(i int) bool { return true }))
|
case <-time.After(time.Second):
|
||||||
|
t.Fatalf("expected feedback by now")
|
||||||
|
}
|
||||||
|
|
||||||
require.NoError(t, topic.Publish(123))
|
tc.assertFn(topic)
|
||||||
|
})
|
||||||
cancel() // this should close the topic, no more messages can be published
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-feedback:
|
|
||||||
break
|
|
||||||
|
|
||||||
case <-time.After(time.Second):
|
|
||||||
t.Fatalf("expected feedback by now")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
require.Error(t, ErrTopicClosed, topic.Publish(1))
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user