A reliable and powerful retry library for Golang to build fault-tolerant system.
$ go get github.com/rbrahul/retry
import("github.com/rbrahul/retry")
err := retry.Retry(func() bool {
operationError := doesHeavyLifting()
if operationError != nil {
return true // retry operation
}
return false // No need to retry
}, 10, 3*time.Second)
if err != nil {
fmt.Error("Maxium retry exceeded")
}
RandomBackoff returns a random integer value between lower and upper range. And will be considered as the delay in the number of seconds.
For Example:
RandomBackoff(2,10) will return a dealy between 2 and 10
err := retry.Retry(func() bool {
operationError := doesHeavyLifting()
if operationError != nil {
return true // retry operation
}
return false // No need to retry
}, 1 * time.Minute, retry.RandomBackoff(2, 10))
if err == retry.ErrDeadlineExceeded {
fmt.Error("Retry deadline exceeded")
}
Retry failed operations with a deadline of 1 minute and with ExponentialBackoff. Every retry the delay will be twice compared to the previous delay. But the maximum delay will be 10 seconds.
ExponentialBackoff returns an exponential delay as number of seconds for each retry execution. If delay is > maxBackoff then maxBackoff will be the used as delay for next retry.
For Example:
ExponentialBackoff(10) will return the following intervals in number seconds as long as the retry is executed.
[1 -> 2 -> 4 -> 8 -> 10 -> 10 ...]
err := retry.Retry(func() bool {
operationError := doesHeavyLifting()
if operationError != nil {
return true // retry operation
}
return false // No need to retry
}, 1 * time.Minute, retry.ExponentialBackoff(10))
if err == retry.ErrDeadlineExceeded {
fmt.Error("Retry deadline exceeded")
}
Retry failed operations with a maximum 10 retries and with a custom Backoff function. Delay will be 1.5 times compared to the previous delay.
err := retry.Retry(func() bool {
operationError := doesHeavyLifting()
if operationError != nil {
return true // retry operation
}
return false // No need to retry
}, 10, func(previousDelay uint64) uint64 {
return previousDelay * 1.5
})
if err == retry.ErrMaximumRetryExceeded {
fmt.Error("Maxium retry exceeded")
}
For every delay there is a Jitter applied to avoid collisions among schedules to resolve Thundering herd problem. The extended delay could be between +10% to +30%.