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

feat: add configurable retry interval for queue retries #144

Merged
merged 1 commit into from
Mar 13, 2025
Merged
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
36 changes: 23 additions & 13 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package queue
import (
"context"
"runtime"
"time"

"github.com/golang-queue/queue/core"
)
Expand Down Expand Up @@ -80,26 +81,35 @@ func WithAfterFn(afterFn func()) Option {
})
}

// WithRetryInterval sets the retry interval
func WithRetryInterval(d time.Duration) Option {
return OptionFunc(func(q *Options) {
q.retryInterval = d
})
}

// Options for custom args in Queue
type Options struct {
workerCount int64
logger Logger
queueSize int
worker core.Worker
fn func(context.Context, core.TaskMessage) error
afterFn func()
metric Metric
workerCount int64
logger Logger
queueSize int
worker core.Worker
fn func(context.Context, core.TaskMessage) error
afterFn func()
metric Metric
retryInterval time.Duration
}

// NewOptions initialize the default value for the options
func NewOptions(opts ...Option) *Options {
o := &Options{
workerCount: defaultWorkerCount,
queueSize: defaultCapacity,
logger: defaultNewLogger,
worker: nil,
fn: defaultFn,
metric: defaultMetric,
workerCount: defaultWorkerCount,
queueSize: defaultCapacity,
logger: defaultNewLogger,
worker: nil,
fn: defaultFn,
metric: defaultMetric,
retryInterval: time.Second,
}

// Loop through each option
Expand Down
44 changes: 44 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package queue

import (
"testing"
"time"
)

func TestWithRetryInterval(t *testing.T) {
tests := []struct {
name string
duration time.Duration
want time.Duration
}{
{
name: "Set 2 seconds retry interval",
duration: 2 * time.Second,
want: 2 * time.Second,
},
{
name: "Set 500ms retry interval",
duration: 500 * time.Millisecond,
want: 500 * time.Millisecond,
},
{
name: "Set zero retry interval",
duration: 0,
want: 0,
},
{
name: "Set negative retry interval",
duration: -1 * time.Second,
want: -1 * time.Second,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := NewOptions(WithRetryInterval(tt.duration))
if opts.retryInterval != tt.want {
t.Errorf("WithRetryInterval() = %v, want %v", opts.retryInterval, tt.want)
}
})
}
}
43 changes: 23 additions & 20 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ type (
// A Queue is a message queue.
Queue struct {
sync.Mutex
metric *metric
logger Logger
workerCount int64
routineGroup *routineGroup
quit chan struct{}
ready chan struct{}
worker core.Worker
stopOnce sync.Once
stopFlag int32
afterFn func()
metric *metric
logger Logger
workerCount int64
routineGroup *routineGroup
quit chan struct{}
ready chan struct{}
worker core.Worker
stopOnce sync.Once
stopFlag int32
afterFn func()
retryInterval time.Duration
}
)

Expand All @@ -40,14 +41,15 @@ var ErrMissingWorker = errors.New("missing worker module")
func NewQueue(opts ...Option) (*Queue, error) {
o := NewOptions(opts...)
q := &Queue{
routineGroup: newRoutineGroup(),
quit: make(chan struct{}),
ready: make(chan struct{}, 1),
workerCount: o.workerCount,
logger: o.logger,
worker: o.worker,
metric: &metric{},
afterFn: o.afterFn,
routineGroup: newRoutineGroup(),
quit: make(chan struct{}),
ready: make(chan struct{}, 1),
workerCount: o.workerCount,
logger: o.logger,
worker: o.worker,
metric: &metric{},
afterFn: o.afterFn,
retryInterval: o.retryInterval,
}

if q.worker == nil {
Expand Down Expand Up @@ -296,6 +298,8 @@ func (q *Queue) schedule() {
// start to start all worker
func (q *Queue) start() {
tasks := make(chan core.TaskMessage, 1)
ticker := time.NewTicker(q.retryInterval)
defer ticker.Stop()

for {
// check worker number
Expand All @@ -320,8 +324,7 @@ func (q *Queue) start() {
close(tasks)
return
}
case <-time.After(time.Second):
// sleep 1 second to fetch new task
case <-ticker.C:
}
}
}
Expand Down
Loading