Fix the RPCClient leaking connections (#1820)

* Fix the RPCClient leaking connections.

* Wrap the error return from GetInfo.
This commit is contained in:
stasatdaglabs 2021-08-16 15:59:35 +03:00 committed by GitHub
parent ce17348175
commit 65b5a080e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,18 +39,6 @@ func NewRPCClient(rpcAddress string) (*RPCClient, error) {
return nil, err return nil, err
} }
getInfoResponse, err := rpcClient.GetInfo()
if err != nil {
return nil, errors.Errorf("error making GetInfo request")
}
localVersion := version.Version()
remoteVersion := getInfoResponse.ServerVersion
if localVersion != remoteVersion {
return nil, errors.Errorf("Server version mismatch, expect: %s, got: %s", localVersion, remoteVersion)
}
return rpcClient, nil return rpcClient, nil
} }
@ -73,11 +61,23 @@ func (c *RPCClient) connect() error {
c.rpcRouter = rpcRouter c.rpcRouter = rpcRouter
log.Infof("Connected to %s", c.rpcAddress) log.Infof("Connected to %s", c.rpcAddress)
getInfoResponse, err := c.GetInfo()
if err != nil {
return errors.Wrapf(err, "error making GetInfo request")
}
localVersion := version.Version()
remoteVersion := getInfoResponse.ServerVersion
if localVersion != remoteVersion {
return errors.Errorf("Server version mismatch, expect: %s, got: %s", localVersion, remoteVersion)
}
return nil return nil
} }
func (c *RPCClient) disconnect() error { func (c *RPCClient) disconnect() error {
c.rpcRouter.router.Close()
err := c.GRPCClient.Disconnect() err := c.GRPCClient.Disconnect()
if err != nil { if err != nil {
return err return err
@ -129,8 +129,12 @@ func (c *RPCClient) Reconnect() error {
func (c *RPCClient) handleClientDisconnected() { func (c *RPCClient) handleClientDisconnected() {
atomic.StoreUint32(&c.isConnected, 0) atomic.StoreUint32(&c.isConnected, 0)
if atomic.LoadUint32(&c.isClosed) == 0 { if atomic.LoadUint32(&c.isClosed) == 0 {
err := c.disconnect()
if err != nil {
panic(err)
}
c.lastDisconnectedTime = time.Now() c.lastDisconnectedTime = time.Now()
err := c.Reconnect() err = c.Reconnect()
if err != nil { if err != nil {
panic(err) panic(err)
} }