2016-11-07 10:40:11 +02:00
|
|
|
package client
|
|
|
|
|
2018-03-03 13:53:52 +02:00
|
|
|
import (
|
|
|
|
"context"
|
2018-07-22 18:41:58 +02:00
|
|
|
|
2024-06-04 22:40:43 +02:00
|
|
|
"go-micro.dev/v5/errors"
|
2018-03-03 13:53:52 +02:00
|
|
|
)
|
2016-11-07 18:10:40 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// note that returning either false or a non-nil error will result in the call not being retried.
|
2016-11-07 18:10:40 +02:00
|
|
|
type RetryFunc func(ctx context.Context, req Request, retryCount int, err error) (bool, error)
|
2016-11-07 10:40:11 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// RetryAlways always retry on error.
|
2018-07-22 18:41:58 +02:00
|
|
|
func RetryAlways(ctx context.Context, req Request, retryCount int, err error) (bool, error) {
|
2016-11-07 18:39:05 +02:00
|
|
|
return true, nil
|
2016-11-07 10:40:11 +02:00
|
|
|
}
|
2018-07-22 18:41:58 +02:00
|
|
|
|
2022-09-30 16:27:07 +02:00
|
|
|
// RetryOnError retries a request on a 500 or timeout error.
|
2018-07-22 18:41:58 +02:00
|
|
|
func RetryOnError(ctx context.Context, req Request, retryCount int, err error) (bool, error) {
|
|
|
|
if err == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
e := errors.Parse(err.Error())
|
|
|
|
if e == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch e.Code {
|
2022-10-20 13:00:50 +02:00
|
|
|
// Retry on timeout, not on 500 internal server error, as that is a business
|
|
|
|
// logic error that should be handled by the user.
|
|
|
|
case 408:
|
2018-07-22 18:41:58 +02:00
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|