Skip to content

retry.DelayType gets inconsistent values #124

Closed
@haaawk

Description

@haaawk

Depending on whether retry.Attempts is set to 0 or something bigger than 0 the function given as retry.DelayType gets n either from 0,1,2,... or 1,2,3,....

Here's a reproducer:

/*
module go-retry-test

go 1.23

require github.com/avast/retry-go/v4 v4.6.0
*/
package main

import (
	"context"
	"errors"
	"time"
)
import "github.com/avast/retry-go/v4"

var count uint64 = 6

func action() error {
	if count == 0 {
		return nil
	}
	count -= 1
	return errors.New("something went wrong")
}

func main() {
	_ = retry.Do(action,
		retry.Attempts(3),
		retry.DelayType(func(n uint, err error, config *retry.Config) time.Duration {
			println("Delay with attempts != 0 -> ", n)
			return time.Second
		}),
		retry.LastErrorOnly(true),
		retry.Context(context.Background()))

	println("")

	_ = retry.Do(action,
		retry.Attempts(0),
		retry.DelayType(func(n uint, err error, config *retry.Config) time.Duration {
			println("Delay with attempts == 0 -> ", n)
			return time.Second
		}),
		retry.LastErrorOnly(true),
		retry.Context(context.Background()))
}

It prints:

Delay with attempts != 0 ->  0
Delay with attempts != 0 ->  1

Delay with attempts == 0 ->  1
Delay with attempts == 0 ->  2
Delay with attempts == 0 ->  3

I would expect the retry.DelayType function to either always get n from 0,1,2,... or always from 1,2,3,... but not one or the other depending on the other config parameters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions