Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up resources in Go channel pub sub [Memory leak] #407

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions pubsub/gochannel/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ func (g *GoChannel) Publish(topic string, messages ...*message.Message) error {
g.subscribersLock.RLock()
defer g.subscribersLock.RUnlock()

subLock, _ := g.subscribersByTopicLock.LoadOrStore(topic, &sync.Mutex{})
subLock, loaded := g.subscribersByTopicLock.LoadOrStore(topic, &sync.Mutex{})
subLock.(*sync.Mutex).Lock()

if !loaded {
defer g.subscribersByTopicLock.Delete(topic)
}
defer subLock.(*sync.Mutex).Unlock()

if g.config.Persistent {
Expand Down Expand Up @@ -205,7 +209,14 @@ func (g *GoChannel) Subscribe(ctx context.Context, topic string) (<-chan *messag
s.Close()

g.subscribersLock.Lock()
defer g.subscribersLock.Unlock()
defer func() {
// if there are no subscribers, clean up any resources related to the topic
if len(g.subscribers[topic]) == 0 {
delete(g.subscribers, topic)
g.subscribersByTopicLock.Delete(topic)
}
g.subscribersLock.Unlock()
}()

subLock, _ := g.subscribersByTopicLock.Load(topic)
subLock.(*sync.Mutex).Lock()
Expand Down
77 changes: 77 additions & 0 deletions pubsub/gochannel/pubsub_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package gochannel

import (
"context"
"log"
"strconv"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
)

func TestSubscribe_clean_subscriber_data(t *testing.T) {
subCount := 100
pubSub := NewGoChannel(
Config{OutputChannelBuffer: int64(subCount)},
watermill.NewStdLogger(false, false),
)
topicName := "test_topic"

allClosed := sync.WaitGroup{}

for i := 0; i < subCount; i++ {
ctx, cancel := context.WithCancel(context.Background())
_, err := pubSub.Subscribe(ctx, topicName+"_index_"+strconv.Itoa(i))
require.NoError(t, err)

allClosed.Add(1)
go func() {
cancel()
allClosed.Done()
}()
}

log.Println("waiting for all closed")
allClosed.Wait()

assert.Len(t, pubSub.subscribers, 0)
lockCount := 0
pubSub.subscribersByTopicLock.Range(func(_, _ any) bool {
lockCount++
return true
})
assert.Equal(t, 0, lockCount)

assert.NoError(t, pubSub.Close())
}

func TestPublish_clean_lock_data(t *testing.T) {
messageCount := 100
pubSub := NewGoChannel(
Config{OutputChannelBuffer: int64(messageCount)},
watermill.NewStdLogger(false, false),
)
topicName := "test_topic"

_, err := pubSub.Subscribe(context.Background(), topicName+"_index_"+strconv.Itoa(0))
require.NoError(t, err)

for i := 0; i < messageCount; i++ {
err := pubSub.Publish(topicName+"_index_"+strconv.Itoa(i), message.NewMessage(watermill.NewShortUUID(), nil))
require.NoError(t, err)
}

lockCount := 0
pubSub.subscribersByTopicLock.Range(func(_, _ any) bool {
lockCount++
return true
})
assert.Equal(t, 1, lockCount)

assert.NoError(t, pubSub.Close())
}