mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-10 16:16:47 +00:00

* [NOD-1130] Delete rpcadapters.go. * [NOD-1130] Delete p2p. Move rpc to top level. * [NOD-1130] Remove DAGParams from rpcserverConfig. * [NOD-1130] Remove rpcserverPeer, rpcserverConnManager, rpcserverSyncManager, and rpcserverConfig. * [NOD-1130] Remove wallet RPC commands. * [NOD-1130] Remove wallet RPC commands. * [NOD-1130] Remove connmgr and peer. * [NOD-1130] Move rpcmodel into rpc. * [NOD-1130] Implement ConnectionCount. * [NOD-1130] Remove ping and node RPC commands. * [NOD-1130] Dummify handleGetNetTotals. * [NOD-1130] Add NetConnection to Peer. * [NOD-1130] Fix merge errors. * [NOD-1130] Implement Peers. * [NOD-1130] Fix HandleGetConnectedPeerInfo. * [NOD-1130] Fix SendRawTransaction. * [NOD-1130] Rename addManualNode to connect and removeManualNode to disconnect. * [NOD-1130] Add a stub for AddBlock. * [NOD-1130] Fix tests. * [NOD-1130] Replace half-baked contents of RemoveConnection with a stub. * [NOD-1130] Fix merge errors. * [NOD-1130] Make golint happy. * [NOD-1130] Get rid of something weird. * [NOD-1130] Rename minerClient back to client. * [NOD-1130] Add a few fields to GetConnectedPeerInfoResult. * [NOD-1130] Rename oneTry to isPermanent. * [NOD-1130] Implement ConnectionCount in NetAdapter. * [NOD-1130] Move RawMempoolVerbose out of mempool. * [NOD-1130] Move isSynced into the mining package. * [NOD-1130] Fix a compilation error. * [NOD-1130] Make golint happy. * [NOD-1130] Fix merge errors.
115 lines
3.3 KiB
Go
115 lines
3.3 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 {
|
|
cfg *config.Config
|
|
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(cfg *config.Config, netAdapter *netadapter.NetAdapter, addressManager *addrmgr.AddrManager) (*ConnectionManager, error) {
|
|
c := &ConnectionManager{
|
|
cfg: cfg,
|
|
netAdapter: netAdapter,
|
|
addressManager: addressManager,
|
|
activeRequested: map[string]*connectionRequest{},
|
|
pendingRequested: map[string]*connectionRequest{},
|
|
activeOutgoing: map[string]struct{}{},
|
|
activeIncoming: map[string]struct{}{},
|
|
}
|
|
|
|
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("ConnectionManager.connectionsLoop", 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)
|
|
}
|
|
}
|
|
|
|
// ConnectionCount returns the count of the connected connections
|
|
func (c *ConnectionManager) ConnectionCount() int {
|
|
return c.netAdapter.ConnectionCount()
|
|
}
|