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
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
"github.com/micro/go-micro/v3/errors"
|
2018-03-03 11:53:52 +00:00
|
|
|
)
|
2016-11-07 17:10:40 +01:00
|
|
|
|
2016-11-07 16:51:28 +00:00
|
|
|
// note 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
|
|
|
|
|
|
|
// RetryOnError retries a request on a 500 or timeout error
|
|
|
|
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 {
|
|
|
|
// retry on timeout or internal server error
|
|
|
|
case 408, 500:
|
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|