Skip to content

Commit 5ddd58e

Browse files
committed
[pubsub] publish in parallel in memory implementation
1 parent 687442c commit 5ddd58e

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

pkg/pubsub/memory.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,26 @@ func (m *memoryPubSub) Publish(ctx context.Context, topic string, data []byte) e
4949
return nil
5050
}
5151

52+
wg := &sync.WaitGroup{}
5253
msg := Message{Topic: topic, Data: data}
54+
5355
for _, ch := range subscribers {
54-
select {
55-
case ch <- msg:
56-
case <-ctx.Done():
57-
return ctx.Err()
58-
case <-m.closeCh:
59-
return ErrPubSubClosed
60-
default:
61-
// subscriber buffer is full
62-
}
56+
wg.Add(1)
57+
go func(ch chan Message) {
58+
defer wg.Done()
59+
60+
select {
61+
case ch <- msg:
62+
case <-ctx.Done():
63+
return
64+
case <-m.closeCh:
65+
return
66+
}
67+
}(ch)
6368
}
6469

70+
wg.Wait()
71+
6572
return nil
6673
}
6774

0 commit comments

Comments
 (0)