mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-10-14 00:59:33 +00:00

* [NOD-1120] Removed closure in NetAdapter.onConnectedHanlder * [NOD-1120] Implement all connection manager methods * [NOD-1120] Integrated connmanager into kaspad + added call for dnsseeder * [NOD-1120] Allow buffer to not be bytes.Buffer * [NOD-1120] Added timeout to connect * [NOD-1120] Don't enter connections to add loop if none needed * [NOD-1120] Add call for addressManager.Good * [NOD-1120] Minor bug fixes * [NOD-1120] Remove errChan from grpcConnection * [NOD-1120] Add comments to exported methods * [NOD-1120] cancel the context for DialContext in gRPCServer.Connect * [NOD-1120] Don't try to remove from connSet a connection that doesn't exist * [NOD-1120] add ok bool to connectionSet.get * [NOD-1120] Remove overuse of if-else in checkConnectionRequests * [NOD-1120] Made some order in ConnectionManager * [NOD-1120] Moved checkIncomingConnections to it's own file * [NOD-1120] cleanup in checkOutgoingConnections * [NOD-1120] Cleanup in SeedDNS, and move call outside of connection manager * [NOD-1120] Add check that both --connect and --addpeer aren't used * [NOD-1120] Move dial timeout to constant * [NOD-1120] Enhance comment * [NOD-1120] Log connection failure out of initiateConnection * [NOD-1148] Reshuffle checkRequestedConnections to make more sense * [NOD-1120] Move continue to correct place + reshuffle logging code * [NOD-1120] Don't expose server.Connection outside netAdapter - expose a wrapper instead * [NOD-1120] Add comments * [NOD-1120] Don't return the connection from netAdapter.Connect() * [NOD-1120] Use .Address as key for connectionSet * [NOD-1120] Fix minRetryDuration usage * [NOD-1120] Remove the correct number of incoming connections * [NOD-1120] Add comment * [NOD-1120] Rename connSet -> incomingConnectionSet * [NOD-1120] fix grammar
109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
package connmanager
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/kaspanet/kaspad/addrmgr"
|
|
"github.com/kaspanet/kaspad/netadapter"
|
|
|
|
"github.com/kaspanet/kaspad/config"
|
|
)
|
|
|
|
// connectionRequest represents a user request (either through CLI or RPC) to connect to a certain node
|
|
type connectionRequest struct {
|
|
address string
|
|
isPermanent bool
|
|
nextAttempt time.Time
|
|
retryDuration time.Duration
|
|
}
|
|
|
|
// ConnectionManager monitors that the current active connections satisfy the requirements of
|
|
// outgoing, requested and incoming connections
|
|
type ConnectionManager struct {
|
|
netAdapter *netadapter.NetAdapter
|
|
addressManager *addrmgr.AddrManager
|
|
|
|
activeRequested map[string]*connectionRequest
|
|
pendingRequested map[string]*connectionRequest
|
|
activeOutgoing map[string]struct{}
|
|
targetOutgoing int
|
|
activeIncoming map[string]struct{}
|
|
maxIncoming int
|
|
|
|
stop uint32
|
|
connectionRequestsLock sync.Mutex
|
|
}
|
|
|
|
// New instantiates a new instance of a ConnectionManager
|
|
func New(netAdapter *netadapter.NetAdapter, addressManager *addrmgr.AddrManager) (*ConnectionManager, error) {
|
|
c := &ConnectionManager{
|
|
netAdapter: netAdapter,
|
|
addressManager: addressManager,
|
|
activeRequested: map[string]*connectionRequest{},
|
|
pendingRequested: map[string]*connectionRequest{},
|
|
activeOutgoing: map[string]struct{}{},
|
|
activeIncoming: map[string]struct{}{},
|
|
}
|
|
|
|
cfg := config.ActiveConfig()
|
|
connectPeers := cfg.AddPeers
|
|
if len(cfg.ConnectPeers) > 0 {
|
|
connectPeers = cfg.ConnectPeers
|
|
}
|
|
|
|
c.maxIncoming = cfg.MaxInboundPeers
|
|
c.targetOutgoing = cfg.TargetOutboundPeers
|
|
|
|
for _, connectPeer := range connectPeers {
|
|
c.pendingRequested[connectPeer] = &connectionRequest{
|
|
address: connectPeer,
|
|
isPermanent: true,
|
|
}
|
|
}
|
|
|
|
return c, nil
|
|
}
|
|
|
|
// Start begins the operation of the ConnectionManager
|
|
func (c *ConnectionManager) Start() {
|
|
spawn(c.connectionsLoop)
|
|
}
|
|
|
|
// Stop halts the operation of the ConnectionManager
|
|
func (c *ConnectionManager) Stop() {
|
|
atomic.StoreUint32(&c.stop, 1)
|
|
|
|
for _, connection := range c.netAdapter.Connections() {
|
|
_ = c.netAdapter.Disconnect(connection) // Ignore errors since connection might be in the midst of disconnecting
|
|
}
|
|
}
|
|
|
|
func (c *ConnectionManager) initiateConnection(address string) error {
|
|
log.Infof("Connecting to %s", address)
|
|
return c.netAdapter.Connect(address)
|
|
}
|
|
|
|
const connectionsLoopInterval = 30 * time.Second
|
|
|
|
func (c *ConnectionManager) connectionsLoop() {
|
|
for atomic.LoadUint32(&c.stop) == 0 {
|
|
connections := c.netAdapter.Connections()
|
|
|
|
// We convert the connections list to a set, so that connections can be found quickly
|
|
// Then we go over the set, classifying connection by category: requested, outgoing or incoming.
|
|
// Every step removes all matching connections so that once we get to checkIncomingConnections -
|
|
// the only connections left are the incoming ones
|
|
connSet := convertToSet(connections)
|
|
|
|
c.checkRequestedConnections(connSet)
|
|
|
|
c.checkOutgoingConnections(connSet)
|
|
|
|
c.checkIncomingConnections(connSet)
|
|
|
|
<-time.Tick(connectionsLoopInterval)
|
|
}
|
|
}
|