Skip to content

Commit

Permalink
feat: enable pinging connected peers by default (#1647)
Browse files Browse the repository at this point in the history
* enable pinging peers by default

* handle ping failure
  • Loading branch information
danisharora099 authored Oct 11, 2023
1 parent 124a29e commit 1d60c4b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions packages/core/src/lib/keep_alive_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,24 @@ export class KeepAliveManager {

const peerIdStr = peerId.toString();

// Ping the peer every pingPeriodSecs seconds
// if pingPeriodSecs is 0, don't ping the peer
if (pingPeriodSecs !== 0) {
const interval = setInterval(() => {
void (async () => {
let ping: number;
try {
// ping the peer for keep alive
// also update the peer store with the latency
const ping = await libp2pPing.ping(peerId);
log(`Ping succeeded (${peerIdStr})`, ping);
try {
ping = await libp2pPing.ping(peerId);
log(`Ping succeeded (${peerIdStr})`, ping);
} catch (error) {
log(`Ping failed for peer (${peerIdStr}).
Next ping will be attempted in ${pingPeriodSecs} seconds.
`);
return;
}

try {
await peerStore.patch(peerId, {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import debug from "debug";

import { ConnectionManager } from "./connection_manager.js";

export const DefaultPingKeepAliveValueSecs = 0;
export const DefaultPingKeepAliveValueSecs = 5 * 60;
export const DefaultRelayKeepAliveValueSecs = 5 * 60;
export const DefaultUserAgent = "js-waku";

Expand Down

0 comments on commit 1d60c4b

Please sign in to comment.