Skip to content

feat(retry): add OnRetriesExhausted hook for cleanup actions#670

Open
Br1an67 wants to merge 1 commit intoThreeDotsLabs:masterfrom
Br1an67:fix/issue-545-retry-add-onretriesexhausted
Open

feat(retry): add OnRetriesExhausted hook for cleanup actions#670
Br1an67 wants to merge 1 commit intoThreeDotsLabs:masterfrom
Br1an67:fix/issue-545-retry-add-onretriesexhausted

Conversation

@Br1an67
Copy link

@Br1an67 Br1an67 commented Mar 8, 2026

Fixes #545

Motivation / Background

This PR adds an OnRetriesExhausted hook to the Retry middleware, allowing users to execute cleanup actions when all retry attempts are exhausted. This addresses the need for performing actions like sending notifications (e.g., to Discord) or updating database records when a message handler consistently fails after the maximum number of retries.

Detail

Added a new optional field OnRetriesExhausted to the Retry struct that accepts a function with signature func(err error, retryNum int). This hook is called when:

  • The maximum number of retries is reached
  • The handler still returns an error

The hook is NOT called when:

  • The handler succeeds
  • ShouldRetry returns false (which results in a permanent error)

Changes:

  • Added OnRetriesExhausted func(err error, retryNum int) field to the Retry struct
  • Call the hook after backoff.Retry completes with an error (excluding permanent errors)
  • Added tests to verify the hook is called when retries are exhausted and not called on success

Alternative approaches considered

Considered naming the hook OnCleanUpHook (as suggested in the issue), but chose OnRetriesExhausted to better describe when the hook is triggered - when retries are actually exhausted, not just for general cleanup.

Checklist

  • I wrote tests for the changes.
  • All tests are passing.
  • Code has no breaking changes.
 message/router/middleware/retry.go      |  7 ++++++
 message/router/middleware/retry_test.go | 43 +++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

Add OnRetriesExhausted hook to the Retry middleware that is called when
all retry attempts are exhausted. This allows users to perform cleanup
actions like sending notifications when a message handler consistently
fails after maximum retries.
@zhjdhx
Copy link

zhjdhx commented Mar 8, 2026 via email

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.

Is there any way to write clean up hook after max retries?

2 participants