github.com/aergoio/aergo@v1.3.1/p2p/p2pcommon/pool.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2pcommon
     7  
     8  import (
     9  	"errors"
    10  	"github.com/aergoio/aergo/types"
    11  	"github.com/libp2p/go-libp2p-core/network"
    12  	"time"
    13  )
    14  
    15  const (
    16  	WaitingPeerManagerInterval = time.Minute >> 2
    17  
    18  	PolarisQueryInterval   = time.Minute * 10
    19  	PeerQueryInterval      = time.Hour
    20  	PeerFirstInterval      = time.Second * 4
    21  
    22  	MaxConcurrentHandshake = 5
    23  
    24  )
    25  
    26  var (
    27  	ErrNoWaitings = errors.New("no waiting peer exists")
    28  )
    29  
    30  type PeerEventListener interface {
    31  	OnPeerConnect(pid types.PeerID)
    32  	OnPeerDisconnect(peer RemotePeer)
    33  }
    34  
    35  // PeerFinder works for collecting peer candidate.
    36  // It queries to Polaris or other connected peer efficiently.
    37  // It determine if peer is
    38  // NOTE that this object is not thread safe by itself.
    39  type PeerFinder interface {
    40  	PeerEventListener
    41  
    42  	// Check if it need to discover more peers and send query request to polaris or other peers if needed.
    43  	CheckAndFill()
    44  }
    45  
    46  // WaitingPeerManager manage waiting peer pool and role to connect and handshaking of remote peer.
    47  type WaitingPeerManager interface {
    48  	PeerEventListener
    49  	// OnDiscoveredPeers is called when response of discover query came from polaris or other peer.
    50  	// It returns the count of previously unknown peers.
    51  	OnDiscoveredPeers(metas []PeerMeta) int
    52  	// OnWorkDone
    53  	OnWorkDone(result ConnWorkResult)
    54  
    55  	CheckAndConnect()
    56  
    57  	InstantConnect(meta PeerMeta)
    58  
    59  	OnInboundConn(s network.Stream)
    60  
    61  	OnInboundConnLegacy(s network.Stream)
    62  }
    63  //go:generate mockgen -source=pool.go -package=p2pmock -destination=../p2pmock/mock_peerfinder.go
    64  
    65  type WaitingPeer struct {
    66  	Meta      PeerMeta
    67  	TrialCnt  int
    68  	NextTrial time.Time
    69  
    70  	LastResult error
    71  }
    72  
    73  type ConnWorkResult struct {
    74  	Inbound bool
    75  	Seq     uint32
    76  	// TargetPeer is nil if Inbound is true
    77  	TargetPeer *WaitingPeer
    78  	Meta       PeerMeta
    79  
    80  	P2PVer uint32
    81  	Result error
    82  }