2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-19 09:57:50 -05:00
|
|
|
// Package retry provides functions to check if a gRPC error is retryable.
|
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-09-26 03:57:40 -04:00
|
|
|
const (
|
|
|
|
authEOFErr = `connection error: desc = "transport: authentication handshake failed: EOF"`
|
|
|
|
authReadTCPErr = `connection error: desc = "transport: authentication handshake failed: read tcp`
|
|
|
|
authHandshakeErr = `connection error: desc = "transport: authentication handshake failed`
|
|
|
|
)
|
|
|
|
|
|
|
|
// grpcErr is the error type that is returned by the grpc client.
|
|
|
|
// taken from google.golang.org/grpc/status.FromError.
|
|
|
|
type grpcErr interface {
|
|
|
|
GRPCStatus() *status.Status
|
|
|
|
Error() string
|
|
|
|
}
|
|
|
|
|
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-09-26 03:57:40 -04:00
|
|
|
// Since the GCP proxy loadbalancer may error with an authentication handshake failure if no available backends are ready,
|
|
|
|
// the special handshake errors caused by the GCP LB (e.g. "read tcp", "EOF") are retried.
|
2022-07-21 09:20:12 -04:00
|
|
|
func ServiceIsUnavailable(err error) bool {
|
2022-09-26 03:57:40 -04:00
|
|
|
var targetErr grpcErr
|
2022-07-18 08:00:57 -04:00
|
|
|
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-09-26 03:57:40 -04:00
|
|
|
// retry if GCP proxy LB isn't available
|
|
|
|
if strings.HasPrefix(statusErr.Message(), authEOFErr) {
|
2022-08-31 21:40:29 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-12 18:23:50 -04:00
|
|
|
// retry if GCP proxy LB isn't fully available yet
|
2022-09-26 03:57:40 -04:00
|
|
|
if strings.HasPrefix(statusErr.Message(), authReadTCPErr) {
|
2022-09-12 18:23:50 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-09-26 03:57:40 -04:00
|
|
|
return !strings.HasPrefix(statusErr.Message(), authHandshakeErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadbalancerIsNotReady checks if the error was caused by a GCP LB not being ready yet.
|
|
|
|
func LoadbalancerIsNotReady(err error) bool {
|
|
|
|
var targetErr grpcErr
|
|
|
|
if !errors.As(err, &targetErr) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
statusErr, ok := status.FromError(targetErr)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if statusErr.Code() != codes.Unavailable {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// retry if GCP proxy LB isn't fully available yet
|
|
|
|
return strings.HasPrefix(statusErr.Message(), authReadTCPErr)
|
2022-06-21 11:59:12 -04:00
|
|
|
}
|