1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00
go-micro/client/retry.go

36 lines
821 B
Go
Raw Normal View History

2016-11-07 10:40:11 +02:00
package client
2018-03-03 13:53:52 +02:00
import (
"context"
"github.com/micro/go-micro/errors"
2018-03-03 13:53:52 +02:00
)
2016-11-07 18:51:28 +02:00
// note that returning either false or a non-nil error will result in the call not being retried
type RetryFunc func(ctx context.Context, req Request, retryCount int, err error) (bool, error)
2016-11-07 10:40:11 +02:00
// RetryAlways always retry on error
func RetryAlways(ctx context.Context, req Request, retryCount int, err error) (bool, error) {
return true, nil
2016-11-07 10:40:11 +02: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
}
}