mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
[NOD-1150] Add in netadapter function to disconnect router (#797)
* [NOD-1150] Add in netadapter function to disconnect router * [NOD-1150] Fix comment
This commit is contained in:
parent
04b578cee1
commit
eaa8515442
@ -101,6 +101,9 @@ func (na *NetAdapter) newOnConnectedHandler() server.OnConnectedHandler {
|
||||
router.SetOnRouteCapacityReachedHandler(func() {
|
||||
err := connection.Disconnect()
|
||||
if err != nil {
|
||||
if !errors.Is(err, server.ErrNetwork) {
|
||||
panic(err)
|
||||
}
|
||||
log.Warnf("Failed to disconnect from %s", connection)
|
||||
}
|
||||
})
|
||||
@ -222,3 +225,16 @@ func (na *NetAdapter) GetBestLocalAddress() (*wire.NetAddress, error) {
|
||||
}
|
||||
return nil, errors.New("no address was found")
|
||||
}
|
||||
|
||||
// DisconnectAssociatedConnection disconnects from the connection associated with the given router.
|
||||
func (na *NetAdapter) DisconnectAssociatedConnection(router *routerpkg.Router) error {
|
||||
connection := na.routersToConnections[router]
|
||||
err := connection.Disconnect()
|
||||
if err != nil {
|
||||
if !errors.Is(err, server.ErrNetwork) {
|
||||
return err
|
||||
}
|
||||
log.Warnf("Error disconnecting from %s: %s", connection, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/kaspanet/kaspad/netadapter/router"
|
||||
"github.com/pkg/errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
@ -35,3 +36,7 @@ type Connection interface {
|
||||
SetOnDisconnectedHandler(onDisconnectedHandler OnDisconnectedHandler)
|
||||
Address() net.Addr
|
||||
}
|
||||
|
||||
// ErrNetwork is an error related to the internals of the connection, and not an error that
|
||||
// came from outside (e.g. from OnDisconnectedHandler).
|
||||
var ErrNetwork = errors.New("network error")
|
||||
|
@ -60,12 +60,18 @@ func newRouterInitializer(netAdapter *netadapter.NetAdapter,
|
||||
// TODO(libp2p) Ban peer
|
||||
panic("unimplemented")
|
||||
}
|
||||
// TODO(libp2p) Disconnect from peer
|
||||
panic("unimplemented")
|
||||
err = netAdapter.DisconnectAssociatedConnection(router)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if errors.Is(err, routerpkg.ErrTimeout) {
|
||||
// TODO(libp2p) Disconnect peer
|
||||
panic("unimplemented")
|
||||
err = netAdapter.DisconnectAssociatedConnection(router)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user