mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-10-14 00:59:33 +00:00
[NOD-1305] Close client connection on disconnect (#909)
This commit is contained in:
parent
5da957f16e
commit
a32a9011c7
@ -1,10 +1,10 @@
|
|||||||
package ping
|
package ping
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/kaspanet/kaspad/app/protocol/common"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/app/appmessage"
|
"github.com/kaspanet/kaspad/app/appmessage"
|
||||||
"github.com/kaspanet/kaspad/app/protocol/common"
|
|
||||||
peerpkg "github.com/kaspanet/kaspad/app/protocol/peer"
|
peerpkg "github.com/kaspanet/kaspad/app/protocol/peer"
|
||||||
"github.com/kaspanet/kaspad/app/protocol/protocolerrors"
|
"github.com/kaspanet/kaspad/app/protocol/protocolerrors"
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/router"
|
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/router"
|
||||||
|
@ -13,11 +13,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type gRPCConnection struct {
|
type gRPCConnection struct {
|
||||||
server *gRPCServer
|
server *gRPCServer
|
||||||
address *net.TCPAddr
|
address *net.TCPAddr
|
||||||
isOutbound bool
|
stream grpcStream
|
||||||
stream grpcStream
|
router *router.Router
|
||||||
router *router.Router
|
lowLevelClientConnection *grpc.ClientConn
|
||||||
|
|
||||||
// streamLock protects concurrent access to stream.
|
// streamLock protects concurrent access to stream.
|
||||||
// Note that it's an RWMutex. Despite what the name
|
// Note that it's an RWMutex. Despite what the name
|
||||||
@ -34,14 +34,16 @@ type gRPCConnection struct {
|
|||||||
isConnected uint32
|
isConnected uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func newConnection(server *gRPCServer, address *net.TCPAddr, isOutbound bool, stream grpcStream) *gRPCConnection {
|
func newConnection(server *gRPCServer, address *net.TCPAddr, stream grpcStream,
|
||||||
|
lowLevelClientConnection *grpc.ClientConn) *gRPCConnection {
|
||||||
|
|
||||||
connection := &gRPCConnection{
|
connection := &gRPCConnection{
|
||||||
server: server,
|
server: server,
|
||||||
address: address,
|
address: address,
|
||||||
isOutbound: isOutbound,
|
stream: stream,
|
||||||
stream: stream,
|
stopChan: make(chan struct{}),
|
||||||
stopChan: make(chan struct{}),
|
isConnected: 1,
|
||||||
isConnected: 1,
|
lowLevelClientConnection: lowLevelClientConnection,
|
||||||
}
|
}
|
||||||
|
|
||||||
return connection
|
return connection
|
||||||
@ -83,7 +85,7 @@ func (c *gRPCConnection) SetOnInvalidMessageHandler(onInvalidMessageHandler serv
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *gRPCConnection) IsOutbound() bool {
|
func (c *gRPCConnection) IsOutbound() bool {
|
||||||
return c.isOutbound
|
return c.lowLevelClientConnection != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disconnect disconnects the connection
|
// Disconnect disconnects the connection
|
||||||
@ -98,7 +100,7 @@ func (c *gRPCConnection) Disconnect() {
|
|||||||
|
|
||||||
close(c.stopChan)
|
close(c.stopChan)
|
||||||
|
|
||||||
if c.isOutbound {
|
if c.IsOutbound() {
|
||||||
c.closeSend()
|
c.closeSend()
|
||||||
log.Debugf("Disconnected from %s", c)
|
log.Debugf("Disconnected from %s", c)
|
||||||
}
|
}
|
||||||
@ -138,5 +140,8 @@ func (c *gRPCConnection) closeSend() {
|
|||||||
defer c.streamLock.Unlock()
|
defer c.streamLock.Unlock()
|
||||||
|
|
||||||
clientStream := c.stream.(protowire.P2P_MessageStreamClient)
|
clientStream := c.stream.(protowire.P2P_MessageStreamClient)
|
||||||
_ = clientStream.CloseSend() // ignore error because we don't really know what's the status of the connection
|
|
||||||
|
// ignore error because we don't really know what's the status of the connection
|
||||||
|
_ = clientStream.CloseSend()
|
||||||
|
_ = c.lowLevelClientConnection.Close()
|
||||||
}
|
}
|
||||||
|
@ -90,12 +90,12 @@ func (s *gRPCServer) Connect(address string) (server.Connection, error) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), dialTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), dialTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
gRPCConnection, err := grpc.DialContext(ctx, address, grpc.WithInsecure(), grpc.WithBlock())
|
gRPCClientConnection, err := grpc.DialContext(ctx, address, grpc.WithInsecure(), grpc.WithBlock())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "error connecting to %s", address)
|
return nil, errors.Wrapf(err, "error connecting to %s", address)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := protowire.NewP2PClient(gRPCConnection)
|
client := protowire.NewP2PClient(gRPCClientConnection)
|
||||||
stream, err := client.MessageStream(context.Background(), grpc.UseCompressor(gzip.Name))
|
stream, err := client.MessageStream(context.Background(), grpc.UseCompressor(gzip.Name))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "error getting client stream for %s", address)
|
return nil, errors.Wrapf(err, "error getting client stream for %s", address)
|
||||||
@ -110,7 +110,7 @@ func (s *gRPCServer) Connect(address string) (server.Connection, error) {
|
|||||||
return nil, errors.Errorf("non-tcp addresses are not supported")
|
return nil, errors.Errorf("non-tcp addresses are not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
connection := newConnection(s, tcpAddress, true, stream)
|
connection := newConnection(s, tcpAddress, stream, gRPCClientConnection)
|
||||||
|
|
||||||
err = s.onConnectedHandler(connection)
|
err = s.onConnectedHandler(connection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -29,7 +29,7 @@ func (p *p2pServer) MessageStream(stream protowire.P2P_MessageStreamServer) erro
|
|||||||
return errors.Errorf("non-tcp connections are not supported")
|
return errors.Errorf("non-tcp connections are not supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
connection := newConnection(p.server, tcpAddress, false, stream)
|
connection := newConnection(p.server, tcpAddress, stream, nil)
|
||||||
|
|
||||||
err := p.server.onConnectedHandler(connection)
|
err := p.server.onConnectedHandler(connection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user