Skip to content

fix: proper cleanup of timers on Stop or Reset for #156 #157

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

Open
wants to merge 1 commit into
base: v3
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
21 changes: 12 additions & 9 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (c *Consumer) StopTimeout(timeout time.Duration) error {
}()

timer := time.NewTimer(timeout)
defer timer.Stop()
defer internal.CleanupTimer(timer)

done := make(chan struct{}, 1)
go func() {
Expand Down Expand Up @@ -375,6 +375,7 @@ func (c *Consumer) reserveOne(ctx context.Context) (*Message, error) {
func (c *Consumer) fetcher(ctx context.Context, fetcherID int32) {
timer := time.NewTimer(time.Minute)
timer.Stop()
defer internal.CleanupTimer(timer)

fetchTimeout := c.opt.ReservationTimeout
fetchTimeout -= fetchTimeout / 10
Expand Down Expand Up @@ -428,7 +429,9 @@ func (c *Consumer) fetchMessages(
c.voteQueueFull()
}

internal.CleanupTimer(timer)
timer.Reset(timeout)
defer internal.CleanupTimer(timer)
for i := range msgs {
msg := &msgs[i]

Expand All @@ -442,10 +445,6 @@ func (c *Consumer) fetchMessages(
}
}

if !timer.Stop() {
<-timer.C
}

return false, nil
}

Expand All @@ -459,6 +458,7 @@ func (c *Consumer) worker(ctx context.Context, workerID int32) {

timer := time.NewTimer(time.Minute)
timer.Stop()
defer internal.CleanupTimer(timer)

for {
if workerID >= atomic.LoadInt32(&c.numWorker) {
Expand Down Expand Up @@ -492,12 +492,11 @@ func (c *Consumer) waitMessage(ctx context.Context, timer *time.Timer) *Message

c.ensureFetcher(ctx)

internal.CleanupTimer(timer)
timer.Reset(workerIdleTimeout)
defer internal.CleanupTimer(timer)
select {
case msg := <-c.buffer:
if !timer.Stop() {
<-timer.C
}
return msg
case <-timer.C:
c.voteQueueEmpty()
Expand Down Expand Up @@ -712,6 +711,7 @@ func (c *Consumer) lockWorker(

timer := time.NewTimer(time.Minute)
timer.Stop()
defer internal.CleanupTimer(timer)

for {
var err error
Expand All @@ -733,6 +733,7 @@ func (c *Consumer) lockWorker(
lock = nil
}

internal.CleanupTimer(timer)
timer.Reset(500 * time.Millisecond)
select {
case <-timer.C:
Expand Down Expand Up @@ -791,9 +792,10 @@ func (c *Consumer) queueEmpty() bool {

func (c *Consumer) autotune(ctx context.Context, cfg *consumerConfig) {
timer := time.NewTimer(time.Hour)
defer timer.Stop()
defer internal.CleanupTimer(timer)

for c.timing() == 0 {
internal.CleanupTimer(timer)
timer.Reset(250 * time.Millisecond)
select {
case <-timer.C:
Expand All @@ -804,6 +806,7 @@ func (c *Consumer) autotune(ctx context.Context, cfg *consumerConfig) {
}

for {
internal.CleanupTimer(timer)
timer.Reset(c.autotuneInterval())
select {
case <-timer.C:
Expand Down
14 changes: 14 additions & 0 deletions internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"encoding/ascii85"
"errors"
"time"
)

func MaxEncodedLen(n int) int {
Expand All @@ -27,3 +28,16 @@ func DecodeString(src string) ([]byte, error) {
}
return dst[:ndst], nil
}

//------------------------------------------------------------------------------

// CleanupTimer will stop a timer and purge the timer's channel to ensure
// all timer related resources are cleaned up.
func CleanupTimer(timer *time.Timer) {
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
}
8 changes: 3 additions & 5 deletions memqueue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ func (q *scheduler) Schedule(msg *taskq.Message, fn func()) {

timer := time.AfterFunc(msg.Delay, func() {
// Remove our entry from the map
q.timerLock.Lock()
delete(q.timerMap, msg)
q.timerLock.Unlock()
q.Remove(msg)

fn()
})
Expand All @@ -43,7 +41,7 @@ func (q *scheduler) Remove(msg *taskq.Message) {

timer, ok := q.timerMap[msg]
if ok {
timer.Stop()
internal.CleanupTimer(timer)
delete(q.timerMap, msg)
}
}
Expand All @@ -54,7 +52,7 @@ func (q *scheduler) Purge() int {

// Stop all delayed items
for _, timer := range q.timerMap {
timer.Stop()
internal.CleanupTimer(timer)
}

n := len(q.timerMap)
Expand Down