Skip to content

Commit

Permalink
retry: add IgnoreErrorsWithin(), ErrorsToLogger()
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-fi committed Mar 15, 2023
1 parent 496bf23 commit f861136
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package retry
import (
"context"
"fmt"
"log"
"time"

"github.com/function61/gokit/app/backoff"
Expand Down Expand Up @@ -47,3 +48,27 @@ func Retry(
func DefaultBackoff() backoff.Func {
return backoff.ExponentialWithCappedMax(100*time.Millisecond, 1*time.Second)
}

// creates `Retry()` compatible error handler that only starts reacting to errors after a specific initial duration
func IgnoreErrorsWithin(expectErrorsWithin time.Duration, handleError func(error)) func(error) {
retryGroupStarted := time.Now()

return func(err error) {
// during this time window attempts are expected to fail and thus we should not confuse the user.
if time.Since(retryGroupStarted) <= expectErrorsWithin {
return
}

// now in this time window we have passes the expected failures and the failures have likely
// become unexpected. inform ther user.

handleError(err)
}
}

// creates `Retry()` compatible error handler function that just pushes errors to a logger
func ErrorsToLogger(logger *log.Logger) func(error) {
return func(err error) {
logger.Println(err.Error())
}
}

0 comments on commit f861136

Please sign in to comment.