2016-11-07 09:40:11 +01:00
|
|
|
package client
|
|
|
|
|
2018-03-03 11:53:52 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2018-07-22 17:41:58 +01:00
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/errors"
|
2018-03-03 11:53:52 +00:00
|
|
|
)
|
2016-11-07 17:10:40 +01:00
|
|
|
|
2020-08-25 14:33:36 +03:00
|
|
|
// RetryFunc that returning either false or a non-nil error will result in the call not being retried
|
2016-11-07 17:10:40 +01:00
|
|
|
type RetryFunc func(ctx context.Context, req Request, retryCount int, err error) (bool, error)
|
2016-11-07 09:40:11 +01:00
|
|
|
|
2018-07-22 17:41:58 +01:00
|
|
|
// RetryAlways always retry on error
|
|
|
|
func RetryAlways(ctx context.Context, req Request, retryCount int, err error) (bool, error) {
|
2016-11-07 17:39:05 +01:00
|
|
|
return true, nil
|
2016-11-07 09:40:11 +01:00
|
|
|
}
|
2018-07-22 17:41:58 +01:00
|
|
|
|
2020-12-07 21:54:45 +03:00
|
|
|
// RetryNever never retry on error
|
|
|
|
func RetryNever(ctx context.Context, req Request, retryCount int, err error) (bool, error) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-03-27 00:16:22 +03:00
|
|
|
// RetryOnError retries a request on a 500 or 408 (timeout) error
|
2021-12-16 15:03:42 +03:00
|
|
|
func RetryOnError(_ context.Context, _ Request, _ int, err error) (bool, error) {
|
2018-07-22 17:41:58 +01:00
|
|
|
if err == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-12-07 21:54:45 +03:00
|
|
|
me := errors.FromError(err)
|
|
|
|
switch me.Code {
|
2018-07-22 17:41:58 +01:00
|
|
|
// retry on timeout or internal server error
|
|
|
|
case 408, 500:
|
|
|
|
return true, nil
|
|
|
|
}
|
2020-12-07 21:54:45 +03:00
|
|
|
return false, nil
|
2018-07-22 17:41:58 +01:00
|
|
|
}
|
2022-03-27 00:16:22 +03:00
|
|
|
|
|
|
|
// RetryOnErrors retries a request on specified error codes
|
|
|
|
func RetryOnErrors(codes ...int32) RetryFunc {
|
|
|
|
return func(_ context.Context, _ Request, _ int, err error) (bool, error) {
|
|
|
|
if err == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
me := errors.FromError(err)
|
|
|
|
for _, code := range codes {
|
|
|
|
if me.Code == code {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|