constellation/internal/grpc/retry/retry.go

36 lines
881 B
Go
Raw Normal View History

2022-06-21 11:59:12 -04:00
package retry
import (
"errors"
2022-06-21 11:59:12 -04:00
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// 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.
func ServiceIsUnavailable(err error) bool {
// 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-06-21 11:59:12 -04:00
if statusErr.Code() != codes.Unavailable {
return false
}
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
}