diff --git a/client/xclient.go b/client/xclient.go index b1984851..0ab4a5ed 100644 --- a/client/xclient.go +++ b/client/xclient.go @@ -267,6 +267,7 @@ func (c *xClient) selectClient(ctx context.Context, servicePath, serviceMethod s c.mu.Lock() if c.option.Sticky && client != nil { + safeCloseClient(c.stickyRPCClient) c.stickyK = k c.stickyRPCClient = client } @@ -275,6 +276,18 @@ func (c *xClient) selectClient(ctx context.Context, servicePath, serviceMethod s return k, client, err } +func safeCloseClient(client RPCClient) { + if client == nil { + return + } + + defer func() { + _ = recover() + }() + + client.Close() +} + func (c *xClient) getCachedClient(k string, servicePath, serviceMethod string, args interface{}) (RPCClient, error) { // TODO: improve the lock var client RPCClient