Only close if conn exists
For internal services like cri, close will panic Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
9c238a64e9
commit
c47cbe7b57
17
client.go
17
client.go
@ -23,6 +23,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
containersapi "github.com/containerd/containerd/api/services/containers/v1"
|
containersapi "github.com/containerd/containerd/api/services/containers/v1"
|
||||||
@ -148,6 +149,7 @@ func NewWithConn(conn *grpc.ClientConn, opts ...ClientOpt) (*Client, error) {
|
|||||||
// using a uniform interface
|
// using a uniform interface
|
||||||
type Client struct {
|
type Client struct {
|
||||||
services
|
services
|
||||||
|
connMu sync.Mutex
|
||||||
conn *grpc.ClientConn
|
conn *grpc.ClientConn
|
||||||
runtime string
|
runtime string
|
||||||
connector func() (*grpc.ClientConn, error)
|
connector func() (*grpc.ClientConn, error)
|
||||||
@ -158,6 +160,8 @@ func (c *Client) Reconnect() error {
|
|||||||
if c.connector == nil {
|
if c.connector == nil {
|
||||||
return errors.New("unable to reconnect to containerd, no connector available")
|
return errors.New("unable to reconnect to containerd, no connector available")
|
||||||
}
|
}
|
||||||
|
c.connMu.Lock()
|
||||||
|
defer c.connMu.Unlock()
|
||||||
c.conn.Close()
|
c.conn.Close()
|
||||||
conn, err := c.connector()
|
conn, err := c.connector()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -174,9 +178,12 @@ func (c *Client) Reconnect() error {
|
|||||||
// connection. A timeout can be set in the context to ensure it returns
|
// connection. A timeout can be set in the context to ensure it returns
|
||||||
// early.
|
// early.
|
||||||
func (c *Client) IsServing(ctx context.Context) (bool, error) {
|
func (c *Client) IsServing(ctx context.Context) (bool, error) {
|
||||||
|
c.connMu.Lock()
|
||||||
if c.conn == nil {
|
if c.conn == nil {
|
||||||
|
c.connMu.Unlock()
|
||||||
return false, errors.New("no grpc connection available")
|
return false, errors.New("no grpc connection available")
|
||||||
}
|
}
|
||||||
|
c.connMu.Unlock()
|
||||||
r, err := c.HealthService().Check(ctx, &grpc_health_v1.HealthCheckRequest{}, grpc.FailFast(false))
|
r, err := c.HealthService().Check(ctx, &grpc_health_v1.HealthCheckRequest{}, grpc.FailFast(false))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
@ -424,7 +431,12 @@ func (c *Client) Subscribe(ctx context.Context, filters ...string) (ch <-chan *e
|
|||||||
|
|
||||||
// Close closes the clients connection to containerd
|
// Close closes the clients connection to containerd
|
||||||
func (c *Client) Close() error {
|
func (c *Client) Close() error {
|
||||||
return c.conn.Close()
|
c.connMu.Lock()
|
||||||
|
defer c.connMu.Unlock()
|
||||||
|
if c.conn != nil {
|
||||||
|
return c.conn.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NamespaceService returns the underlying Namespaces Store
|
// NamespaceService returns the underlying Namespaces Store
|
||||||
@ -524,9 +536,12 @@ type Version struct {
|
|||||||
|
|
||||||
// Version returns the version of containerd that the client is connected to
|
// Version returns the version of containerd that the client is connected to
|
||||||
func (c *Client) Version(ctx context.Context) (Version, error) {
|
func (c *Client) Version(ctx context.Context) (Version, error) {
|
||||||
|
c.connMu.Lock()
|
||||||
if c.conn == nil {
|
if c.conn == nil {
|
||||||
|
c.connMu.Unlock()
|
||||||
return Version{}, errors.New("no grpc connection available")
|
return Version{}, errors.New("no grpc connection available")
|
||||||
}
|
}
|
||||||
|
c.connMu.Unlock()
|
||||||
response, err := c.VersionService().Version(ctx, &ptypes.Empty{})
|
response, err := c.VersionService().Version(ctx, &ptypes.Empty{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Version{}, err
|
return Version{}, err
|
||||||
|
Loading…
Reference in New Issue
Block a user