forked from TrueCloudLab/frostfs-sdk-go
[#XX] api: RPC client conn alive check
Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
parent
cb813e27a8
commit
bae7303d86
2 changed files with 13 additions and 1 deletions
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue