chore: minor improvements

This commit is contained in:
Danish Arora 2024-10-03 16:34:40 +05:30
parent 7b7d691fc3
commit 8e88fadbb7
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E
3 changed files with 2 additions and 22 deletions

View File

@ -46,7 +46,6 @@ export class BaseProtocol implements IBaseProtocolCore {
return this.streamManager.getStream(peer); return this.streamManager.getStream(peer);
} }
//TODO: move to SDK
/** /**
* Returns known peers from the address book (`libp2p.peerStore`) that support * Returns known peers from the address book (`libp2p.peerStore`) that support
* the class protocol. Waku may or may not be currently connected to these * the class protocol. Waku may or may not be currently connected to these
@ -56,17 +55,12 @@ export class BaseProtocol implements IBaseProtocolCore {
return getPeersForProtocol(this.components.peerStore, [this.multicodec]); return getPeersForProtocol(this.components.peerStore, [this.multicodec]);
} }
public async connectedPeers(withOpenStreams = false): Promise<Peer[]> { public async connectedPeers(): Promise<Peer[]> {
const peers = await this.allPeers(); const peers = await this.allPeers();
return peers.filter((peer) => { return peers.filter((peer) => {
const connections = this.components.connectionManager.getConnections( const connections = this.components.connectionManager.getConnections(
peer.id peer.id
); );
if (withOpenStreams) {
return connections.some((c) =>
c.streams.some((s) => s.protocol === this.multicodec)
);
}
return connections.length > 0; return connections.length > 0;
}); });
} }

View File

@ -38,7 +38,6 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
this.log.info( this.log.info(
`Initializing BaseProtocolSDK with numPeersToUse: ${this.numPeersToUse}, maintainPeersInterval: ${maintainPeersInterval}ms` `Initializing BaseProtocolSDK with numPeersToUse: ${this.numPeersToUse}, maintainPeersInterval: ${maintainPeersInterval}ms`
); );
// void this.setupEventListeners();
void this.startMaintainPeersInterval(maintainPeersInterval); void this.startMaintainPeersInterval(maintainPeersInterval);
} }
@ -83,18 +82,6 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
} }
} }
//TODO: validate if adding event listeners for peer connect and disconnect is needed
// private setupEventListeners(): void {
// this.core.addLibp2pEventListener(
// "peer:connect",
// () => void this.maintainPeers()
// );
// this.core.addLibp2pEventListener(
// "peer:disconnect",
// () => void this.maintainPeers()
// );
// }
/** /**
* Checks if there are sufficient peers to send a message to. * Checks if there are sufficient peers to send a message to.
* If `forceUseAllPeers` is `false` (default), returns `true` if there are any connected peers. * If `forceUseAllPeers` is `false` (default), returns `true` if there are any connected peers.
@ -162,7 +149,6 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
`Starting maintain peers interval with ${interval}ms interval` `Starting maintain peers interval with ${interval}ms interval`
); );
try { try {
// await this.maintainPeers();
this.maintainPeersIntervalId = setInterval(() => { this.maintainPeersIntervalId = setInterval(() => {
this.log.debug("Running scheduled peer maintenance"); this.log.debug("Running scheduled peer maintenance");
this.maintainPeers().catch((error) => { this.maintainPeers().catch((error) => {

View File

@ -144,6 +144,7 @@ export class SubscriptionManager implements ISubscription {
} }
public async ping(peerId?: PeerId): Promise<SDKProtocolResult> { public async ping(peerId?: PeerId): Promise<SDKProtocolResult> {
log.info("Sending keep-alive ping");
const peers = peerId ? [peerId] : this.getPeers().map((peer) => peer.id); const peers = peerId ? [peerId] : this.getPeers().map((peer) => peer.id);
const promises = peers.map((peerId) => this.pingSpecificPeer(peerId)); const promises = peers.map((peerId) => this.pingSpecificPeer(peerId));
@ -303,7 +304,6 @@ export class SubscriptionManager implements ISubscription {
} }
this.keepAliveTimer = setInterval(() => { this.keepAliveTimer = setInterval(() => {
log.info("Sending keep-alive ping");
void this.ping() void this.ping()
.then(() => log.info("Keep-alive ping successful")) .then(() => log.info("Keep-alive ping successful"))
.catch((error) => log.error("Error in keep-alive ping cycle:", error)); .catch((error) => log.error("Error in keep-alive ping cycle:", error));