Skip to content

Conversation

StounhandJ
Copy link

This MR addresses an issue where a delay is still applied after the final attempt, even when the maximum number of attempts for a specific error type has been reached.

Problem:
When using the retry.AttemptsForError() option to define a custom attempt limit for a specific error type, the retry logic still applies the configured delay after the final permitted attempt for that error. This results in unnecessary wait time and inconsistent behavior.

Steps to Reproduce:

pacakge main

var count uint64 = 0
var currentTime = time.Now()

type TestError struct {
}

func (TestError) Error() string {
	return ""
}

func action() error {
	count += 1
	fmt.Printf("Attempts: %v; Time: %v\n", count, time.Since(currentTime).Round(time.Second))
	currentTime = time.Now()

	return TestError{}
}

func main() {
	_ = retry.Do(action,
		retry.Attempts(3),
		retry.Delay(2*time.Second),
		retry.DelayType(retry.FixedDelay),
		retry.AttemptsForError(2, TestError{}),
		retry.LastErrorOnly(true),
		retry.Context(context.Background()))

	fmt.Printf("End Time: %v\n", time.Since(currentTime).Round(time.Second))
}

Observed Output:

Attempts: 1; Time: 0s
Attempts: 2; Time: 2s
End Time: 2s

Expected Output:

Attempts: 1; Time: 0s
Attempts: 2; Time: 2s
End Time: 0s

tstromberg added a commit to codeGROOVE-dev/retry that referenced this pull request Jul 28, 2025
tstromberg added a commit to codeGROOVE-dev/retry that referenced this pull request Jul 28, 2025
merge: No delay after final retry on max attempts avast#129
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant