Skip to content

Commit 3c90e8c

Browse files
author
lixizan
committed
Fix thread safety issues
1 parent 82b03f0 commit 3c90e8c

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

event.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (c *EventEmitter) Publish(ctx context.Context, topic string, msg any) error
8585
defer t.Unlock()
8686

8787
for _, v := range t.subs {
88-
if cb, ok := v.topics[topic]; ok {
88+
if cb, exist := v.GetTopic(topic); exist {
8989
if err := t.Emit(ctx, msg, cb); err != nil {
9090
return err
9191
}

types.go

+7
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,10 @@ func (c *subscriberField) Delete(k string) int {
5858
c.Unlock()
5959
return n
6060
}
61+
62+
func (c *subscriberField) GetTopic(topic string) (cb eventCallback, exist bool) {
63+
c.Lock()
64+
cb, exist = c.topics[topic]
65+
c.Unlock()
66+
return
67+
}

0 commit comments

Comments
 (0)