[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:
Ori Newman 2020-07-15 14:42:17 +03:00 committed by GitHub
parent 04b578cee1
commit eaa8515442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 4 deletions

View File

@ -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
}

View File

@ -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")

View File

@ -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)
}