2022-06-21 11:59:12 -04:00
|
|
|
package retry
|
|
|
|
|
|
|
|
import (
|
2022-07-18 08:00:57 -04:00
|
|
|
"errors"
|
2022-06-21 11:59:12 -04:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2022-07-21 09:20:12 -04:00
|
|
|
// ServiceIsUnavailable checks if the error is a grpc status with code Unavailable.
|
2022-06-29 08:28:37 -04:00
|
|
|
// In the special case of an authentication handshake failure, false is returned to prevent further retries.
|
2022-07-21 09:20:12 -04:00
|
|
|
func ServiceIsUnavailable(err error) bool {
|
2022-07-18 08:00:57 -04:00
|
|
|
// taken from google.golang.org/grpc/status.FromError
|
|
|
|
var targetErr interface {
|
|
|
|
GRPCStatus() *status.Status
|
|
|
|
Error() string
|
|
|
|
}
|
|
|
|
|
|
|
|
if !errors.As(err, &targetErr) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
statusErr, ok := status.FromError(targetErr)
|
2022-06-21 11:59:12 -04:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2022-07-18 08:00:57 -04:00
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
if statusErr.Code() != codes.Unavailable {
|
|
|
|
return false
|
|
|
|
}
|
2022-07-18 08:00:57 -04:00
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
// ideally we would check the error type directly, but grpc only provides a string
|
2022-06-29 08:28:37 -04:00
|
|
|
return !strings.HasPrefix(statusErr.Message(), `connection error: desc = "transport: authentication handshake failed`)
|
2022-06-21 11:59:12 -04:00
|
|
|
}
|