Skip to content

Commit a2a02ec

Browse files
committed
refactor: Update retry and maxRetry fields to use uint for task retries
1 parent 13cd930 commit a2a02ec

5 files changed

Lines changed: 9 additions & 9 deletions

File tree

backoff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var DefaultBackoffPolicy = BackoffPolicy{
1818
type Backoff interface {
1919
// Calculate returns the duration to wait before the next retry attempt.
2020
// The input parameter retries indicates how many times the task has already been retried.
21-
Calculate(retries int) time.Duration
21+
Calculate(retries uint) time.Duration
2222
}
2323

2424
// BackoffPolicy defines the configuration for handling retries with backoff logic.
@@ -32,7 +32,7 @@ type BackoffPolicy struct {
3232

3333
// Calculate calculates the delay before the next retry based on the backoff policy.
3434
// It considers the retry count, base delay, maximum delay, and jitter.
35-
func (b *BackoffPolicy) Calculate(retries int) time.Duration {
35+
func (b *BackoffPolicy) Calculate(retries uint) time.Duration {
3636
// Calculate the exponential backoff delay, doubling with each retry.
3737
delay := min(b.BaseDelay*time.Duration(1<<uint(retries-1)), b.MaxDelay)
3838

manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (m *Manager) RegisterTask(taskName string, handler TaskHandlerFunc) {
149149
// if err != nil {
150150
// log.Printf("Error publishing task: %v", err)
151151
// }
152-
func (m *Manager) PublishTask(taskName string, args TaskArgs, maxRetry int) error {
152+
func (m *Manager) PublishTask(taskName string, args TaskArgs, maxRetry uint) error {
153153
if taskName == "" {
154154
return ErrEmptyTaskName
155155
}

task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type TaskHandlerFunc func(TaskArgs) error
1616
type Task struct {
1717
Name string `json:"name"` // The name of the task, used to identify which handler to invoke.
1818
Args TaskArgs `json:"args"` // The arguments for the task, provided as a map of key-value pairs.
19-
Retry int `json:"retry"` // The current retry count for this task.
20-
MaxRetry int `json:"max_retry"` // The maximum number of retries before the task is considered failed.
19+
Retry uint `json:"retry"` // The current retry count for this task.
20+
MaxRetry uint `json:"max_retry"` // The maximum number of retries before the task is considered failed.
2121
Timestamp time.Time `json:"timestamp"` // The timestamp when the task was created or scheduled.
2222
}

tests/backoff_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestBackoffPolicy(t *testing.T) {
1212
tests := []struct {
1313
name string
1414
backoffPolicy *taskqueue.BackoffPolicy
15-
retries int
15+
retries uint
1616
expectedDelay time.Duration
1717
allowJitter bool
1818
}{

tests/worker_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestWorkerProcesses(t *testing.T) {
3030
return errors.New("simulated error")
3131
}
3232

33-
maxRetries := 3
33+
const maxRetries uint = 3
3434

3535
t.Run("should run task successfully", func(t *testing.T) {
3636
mockBroker := NewMockBroker(1)
@@ -93,7 +93,7 @@ func TestWorkerProcesses(t *testing.T) {
9393
cancel()
9494
wg.Wait()
9595

96-
assert.Equal(t, maxRetries+1, failCount)
96+
assert.Equal(t, maxRetries+1, uint(failCount))
9797
})
9898

9999
t.Run("should handle failing task with no backoff", func(t *testing.T) {
@@ -122,7 +122,7 @@ func TestWorkerProcesses(t *testing.T) {
122122
cancel()
123123
wg.Wait()
124124

125-
assert.Equal(t, maxRetries+1, failCount)
125+
assert.Equal(t, maxRetries+1, uint(failCount))
126126
})
127127

128128
t.Run("should not execute task with no handler", func(t *testing.T) {

0 commit comments

Comments
 (0)