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 3, 2024
1 parent d04888e commit b7cb257
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ func (s *subscriber) start(wg *sync.WaitGroup) {
err error
)
// Try until successfully connect to Redis.
timer := time.NewTimer(s.retryTimeout)
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:
continue
case <-s.done:
s.logger.Debug("Subscriber done")
Expand Down
3 changes: 2 additions & 1 deletion syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (s *syncer) start(wg *sync.WaitGroup) {
go func() {
defer wg.Done()
var requests []*syncRequest
timer := time.NewTimer(s.interval)
for {
select {
case <-s.done:
Expand All @@ -70,7 +71,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 Down

0 comments on commit b7cb257

Please sign in to comment.