Skip to content

Commit 08f36ff

Browse files
minor improvements
1 parent 51c91cd commit 08f36ff

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

packages/core/src/lib/connection_manager.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,7 @@ export class ConnectionManager
116116
...options
117117
};
118118

119-
this.keepAliveManager = new KeepAliveManager(
120-
libp2p.peerStore,
121-
keepAliveOptions,
122-
relay
123-
);
119+
this.keepAliveManager = new KeepAliveManager(keepAliveOptions, relay);
124120

125121
this.run()
126122
.then(() => log(`Connection Manager is now running`))
@@ -344,7 +340,11 @@ export class ConnectionManager
344340
void (async () => {
345341
const peerId = evt.detail;
346342

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+
);
348348

349349
const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
350350
Tags.BOOTSTRAP

packages/core/src/lib/keep_alive_manager.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ export class KeepAliveManager {
1616
private relayKeepAliveTimers: Map<PeerId, ReturnType<typeof setInterval>>;
1717
private options: KeepAliveOptions;
1818
private relay?: IRelay;
19-
private peerStore: PeerStore;
2019

21-
constructor(peerStore: PeerStore, options: KeepAliveOptions, relay?: IRelay) {
20+
constructor(options: KeepAliveOptions, relay?: IRelay) {
2221
this.pingKeepAliveTimers = new Map();
2322
this.relayKeepAliveTimers = new Map();
2423
this.options = options;
2524
this.relay = relay;
26-
this.peerStore = peerStore;
2725
}
2826

29-
public start(peerId: PeerId, libp2pPing: PingService): void {
27+
public start(
28+
peerId: PeerId,
29+
libp2pPing: PingService,
30+
peerStore: PeerStore
31+
): void {
3032
// Just in case a timer already exists for this peer
3133
this.stop(peerId);
3234

@@ -37,11 +39,13 @@ export class KeepAliveManager {
3739

3840
if (pingPeriodSecs !== 0) {
3941
const interval = setInterval(() => {
42+
// ping the peer for keep alive
43+
// also update the peer store with the latency
4044
libp2pPing
4145
.ping(peerId)
4246
.then((ping) => {
4347
log(`Ping succeeded (${peerIdStr})`, ping);
44-
this.peerStore
48+
peerStore
4549
.patch(peerId, {
4650
metadata: {
4751
ping: utf8ToBytes(ping.toString())

packages/utils/src/libp2p/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function selectRandomPeer(peers: Peer[]): Peer | undefined {
2020

2121
/**
2222
* 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
2424
* @param peers - The list of peers to choose from
2525
* @returns The peer with the lowest latency, or undefined if no peer could be reached
2626
*/

0 commit comments

Comments
 (0)