mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-27 07:48:44 +00:00
Fixes an RPC client goroutine leak by properly closing the underlying connection
This commit is contained in:
parent
d2dc236f07
commit
aff0ac0d7d
@ -22,6 +22,7 @@ type OnDisconnectedHandler func()
|
||||
// GRPCClient is a gRPC-based RPC client
|
||||
type GRPCClient struct {
|
||||
stream protowire.RPC_MessageStreamClient
|
||||
connection *grpc.ClientConn
|
||||
onErrorHandler OnErrorHandler
|
||||
onDisconnectedHandler OnDisconnectedHandler
|
||||
}
|
||||
@ -43,7 +44,12 @@ func Connect(address string) (*GRPCClient, error) {
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error getting client stream for %s", address)
|
||||
}
|
||||
return &GRPCClient{stream: stream}, nil
|
||||
return &GRPCClient{stream: stream, connection: gRPCConnection}, nil
|
||||
}
|
||||
|
||||
// Close closes the underlying grpc connection
|
||||
func (c *GRPCClient) Close() error {
|
||||
return c.connection.Close()
|
||||
}
|
||||
|
||||
// Disconnect disconnects from the RPC server
|
||||
|
||||
@ -143,6 +143,9 @@ func (c *RPCClient) handleClientDisconnected() {
|
||||
}
|
||||
|
||||
func (c *RPCClient) handleClientError(err error) {
|
||||
if atomic.LoadUint32(&c.isClosed) == 1 {
|
||||
return
|
||||
}
|
||||
log.Warnf("Received error from client: %s", err)
|
||||
c.handleClientDisconnected()
|
||||
}
|
||||
@ -159,7 +162,7 @@ func (c *RPCClient) Close() error {
|
||||
return errors.Errorf("Cannot close a client that had already been closed")
|
||||
}
|
||||
c.rpcRouter.router.Close()
|
||||
return nil
|
||||
return c.GRPCClient.Close()
|
||||
}
|
||||
|
||||
// Address returns the address the RPC client connected to
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user