File tree 3 files changed +16
-12
lines changed
3 files changed +16
-12
lines changed Original file line number Diff line number Diff line change @@ -116,11 +116,7 @@ export class ConnectionManager
116
116
...options
117
117
} ;
118
118
119
- this . keepAliveManager = new KeepAliveManager (
120
- libp2p . peerStore ,
121
- keepAliveOptions ,
122
- relay
123
- ) ;
119
+ this . keepAliveManager = new KeepAliveManager ( keepAliveOptions , relay ) ;
124
120
125
121
this . run ( )
126
122
. then ( ( ) => log ( `Connection Manager is now running` ) )
@@ -344,7 +340,11 @@ export class ConnectionManager
344
340
void ( async ( ) => {
345
341
const peerId = evt . detail ;
346
342
347
- this . keepAliveManager . start ( peerId , this . libp2p . services . ping ) ;
343
+ this . keepAliveManager . start (
344
+ peerId ,
345
+ this . libp2p . services . ping ,
346
+ this . libp2p . peerStore
347
+ ) ;
348
348
349
349
const isBootstrap = ( await this . getTagNamesForPeer ( peerId ) ) . includes (
350
350
Tags . BOOTSTRAP
Original file line number Diff line number Diff line change @@ -16,17 +16,19 @@ export class KeepAliveManager {
16
16
private relayKeepAliveTimers : Map < PeerId , ReturnType < typeof setInterval > > ;
17
17
private options : KeepAliveOptions ;
18
18
private relay ?: IRelay ;
19
- private peerStore : PeerStore ;
20
19
21
- constructor ( peerStore : PeerStore , options : KeepAliveOptions , relay ?: IRelay ) {
20
+ constructor ( options : KeepAliveOptions , relay ?: IRelay ) {
22
21
this . pingKeepAliveTimers = new Map ( ) ;
23
22
this . relayKeepAliveTimers = new Map ( ) ;
24
23
this . options = options ;
25
24
this . relay = relay ;
26
- this . peerStore = peerStore ;
27
25
}
28
26
29
- public start ( peerId : PeerId , libp2pPing : PingService ) : void {
27
+ public start (
28
+ peerId : PeerId ,
29
+ libp2pPing : PingService ,
30
+ peerStore : PeerStore
31
+ ) : void {
30
32
// Just in case a timer already exists for this peer
31
33
this . stop ( peerId ) ;
32
34
@@ -37,11 +39,13 @@ export class KeepAliveManager {
37
39
38
40
if ( pingPeriodSecs !== 0 ) {
39
41
const interval = setInterval ( ( ) => {
42
+ // ping the peer for keep alive
43
+ // also update the peer store with the latency
40
44
libp2pPing
41
45
. ping ( peerId )
42
46
. then ( ( ping ) => {
43
47
log ( `Ping succeeded (${ peerIdStr } )` , ping ) ;
44
- this . peerStore
48
+ peerStore
45
49
. patch ( peerId , {
46
50
metadata : {
47
51
ping : utf8ToBytes ( ping . toString ( ) )
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ export function selectRandomPeer(peers: Peer[]): Peer | undefined {
20
20
21
21
/**
22
22
* Returns the peer with the lowest latency.
23
- * @param getPing - A function that returns the latency for a given peer
23
+ * @param peerStore - The Libp2p PeerStore
24
24
* @param peers - The list of peers to choose from
25
25
* @returns The peer with the lowest latency, or undefined if no peer could be reached
26
26
*/
You can’t perform that action at this time.
0 commit comments