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