[#XX] api: RPC client conn alive check

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
Airat Arifullin 2024-12-03 09:59:14 +03:00
parent cb813e27a8
commit 9866f6afc6
2 changed files with 16 additions and 1 deletions

View file

@ -4,12 +4,14 @@ import (
"io"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
)
// Conn is an interface for grpc client connection.
type Conn interface {
grpc.ClientConnInterface
io.Closer
GetState() connectivity.State
}
// Conn returns underlying connection.

View file

@ -8,12 +8,13 @@ import (
"net/url"
grpcstd "google.golang.org/grpc"
health "google.golang.org/grpc/health/grpc_health_v1"
)
var errInvalidEndpoint = errors.New("invalid endpoint options")
func (c *Client) openGRPCConn(ctx context.Context, dialer func(ctx context.Context, cc grpcstd.ClientConnInterface) error) error {
if c.conn != nil {
if c.conn != nil && c.checkConnAlive(ctx) == nil {
return nil
}
@ -70,3 +71,15 @@ func ParseURI(s string) (string, bool, error) {
return uri.Host, uri.Scheme == grpcTLSScheme, nil
}
func (c *Client) checkConnAlive(ctx context.Context) error {
healthClient := health.NewHealthClient(c.conn)
resp, err := healthClient.Check(ctx, &health.HealthCheckRequest{})
if err != nil {
return err
}
if resp.Status != health.HealthCheckResponse_SERVING {
return fmt.Errorf("unhealthy connection")
}
return nil
}