constellation/internal/grpc/retry/retry.go

47 lines
1.1 KiB
Go
Raw Normal View History

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
2022-06-21 15:59:12 +00:00
package retry
import (
"errors"
2022-06-21 15:59:12 +00: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 12:28:37 +00: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 15:59:12 +00:00
if !ok {
return false
}
2022-06-21 15:59:12 +00:00
if statusErr.Code() != codes.Unavailable {
return false
}
2022-09-01 01:40:29 +00:00
// retry if GCP proxy LB isn't fully available yet
if strings.HasPrefix(statusErr.Message(), `connection error: desc = "transport: authentication handshake failed: EOF"`) {
return true
}
2022-06-21 15:59:12 +00:00
// ideally we would check the error type directly, but grpc only provides a string
2022-06-29 12:28:37 +00:00
return !strings.HasPrefix(statusErr.Message(), `connection error: desc = "transport: authentication handshake failed`)
2022-06-21 15:59:12 +00:00
}