Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Sep 7, 2023
1 parent 51c91cd commit 08f36ff
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
12 changes: 6 additions & 6 deletions packages/core/src/lib/connection_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,7 @@ export class ConnectionManager
...options
};

this.keepAliveManager = new KeepAliveManager(
libp2p.peerStore,
keepAliveOptions,
relay
);
this.keepAliveManager = new KeepAliveManager(keepAliveOptions, relay);

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

this.keepAliveManager.start(peerId, this.libp2p.services.ping);
this.keepAliveManager.start(
peerId,
this.libp2p.services.ping,
this.libp2p.peerStore
);

const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
Tags.BOOTSTRAP
Expand Down
14 changes: 9 additions & 5 deletions packages/core/src/lib/keep_alive_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ export class KeepAliveManager {
private relayKeepAliveTimers: Map<PeerId, ReturnType<typeof setInterval>>;
private options: KeepAliveOptions;
private relay?: IRelay;
private peerStore: PeerStore;

constructor(peerStore: PeerStore, options: KeepAliveOptions, relay?: IRelay) {
constructor(options: KeepAliveOptions, relay?: IRelay) {
this.pingKeepAliveTimers = new Map();
this.relayKeepAliveTimers = new Map();
this.options = options;
this.relay = relay;
this.peerStore = peerStore;
}

public start(peerId: PeerId, libp2pPing: PingService): void {
public start(
peerId: PeerId,
libp2pPing: PingService,
peerStore: PeerStore
): void {
// Just in case a timer already exists for this peer
this.stop(peerId);

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

if (pingPeriodSecs !== 0) {
const interval = setInterval(() => {
// ping the peer for keep alive
// also update the peer store with the latency
libp2pPing
.ping(peerId)
.then((ping) => {
log(`Ping succeeded (${peerIdStr})`, ping);
this.peerStore
peerStore
.patch(peerId, {
metadata: {
ping: utf8ToBytes(ping.toString())
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/libp2p/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function selectRandomPeer(peers: Peer[]): Peer | undefined {

/**
* Returns the peer with the lowest latency.
* @param getPing - A function that returns the latency for a given peer
* @param peerStore - The Libp2p PeerStore
* @param peers - The list of peers to choose from
* @returns The peer with the lowest latency, or undefined if no peer could be reached
*/
Expand Down

0 comments on commit 08f36ff

Please sign in to comment.