feat: enable pinging connected peers by default (#1647)

* enable pinging peers by default

* handle ping failure
This commit is contained in:
Danish Arora 2023-10-11 18:01:42 +05:30 committed by GitHub
parent 124a29ebba
commit 1d60c4ba44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View File

@ -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, {

View File

@ -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";