Skip to content

Commit

Permalink
Fix memory leaks caused by time.After
Browse files Browse the repository at this point in the history
  • Loading branch information
biningo committed Sep 22, 2024
1 parent d04888e commit 458619d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 5 additions & 2 deletions subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"sync"
"time"

"github.com/redis/go-redis/v9"
"github.com/hibiken/asynq/internal/base"
"github.com/hibiken/asynq/internal/log"
"github.com/redis/go-redis/v9"
)

type subscriber struct {
Expand Down Expand Up @@ -58,12 +58,15 @@ func (s *subscriber) start(wg *sync.WaitGroup) {
err error
)
// Try until successfully connect to Redis.
timer := time.NewTimer(s.retryTimeout)
defer timer.Stop()
for {
pubsub, err = s.broker.CancelationPubSub()
if err != nil {
s.logger.Errorf("cannot subscribe to cancelation channel: %v", err)
select {
case <-time.After(s.retryTimeout):
case <-timer.C:
timer.Reset(s.retryTimeout)
continue
case <-s.done:
s.logger.Debug("Subscriber done")
Expand Down
5 changes: 4 additions & 1 deletion syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func (s *syncer) start(wg *sync.WaitGroup) {
go func() {
defer wg.Done()
var requests []*syncRequest
timer := time.NewTimer(s.interval)
defer timer.Stop()
for {
select {
case <-s.done:
Expand All @@ -70,7 +72,7 @@ func (s *syncer) start(wg *sync.WaitGroup) {
return
case req := <-s.requestsCh:
requests = append(requests, req)
case <-time.After(s.interval):
case <-timer.C:
var temp []*syncRequest
for _, req := range requests {
if req.deadline.Before(time.Now()) {
Expand All @@ -81,6 +83,7 @@ func (s *syncer) start(wg *sync.WaitGroup) {
}
}
requests = temp
timer.Reset(s.interval)
}
}
}()
Expand Down

0 comments on commit 458619d

Please sign in to comment.