[#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 bae7303d86
2 changed files with 13 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"
"google.golang.org/grpc/connectivity"
)
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.isConnAlive() {
return nil
}
@ -70,3 +71,12 @@ func ParseURI(s string) (string, bool, error) {
return uri.Host, uri.Scheme == grpcTLSScheme, nil
}
func (c *Client) isConnAlive() bool {
if c.conn == nil {
return false
}
state := c.conn.GetState()
return state != connectivity.Shutdown &&
state != connectivity.TransientFailure
}