chore: renewal doesnt disconnect, only removes

This commit is contained in:
Danish Arora 2024-09-26 12:08:16 +05:30
parent 3647ec97ed
commit 4d7b997d4c
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E
2 changed files with 10 additions and 29 deletions

View File

@ -54,16 +54,7 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
public async renewPeer(peerToDisconnect: PeerId): Promise<Peer | undefined> {
this.log.info(`Attempting to renew peer ${peerToDisconnect}`);
const success = await this.peerManager.disconnectPeer(peerToDisconnect);
if (!success) {
this.log.warn(`Failed to disconnect from peer ${peerToDisconnect}`);
return undefined;
}
this.log.debug(
`Successfully disconnected from peer ${peerToDisconnect}, searching for a new peer`
);
const newPeer = await this.peerManager.findAndAddPeers(1);
const newPeer = await this.peerManager.findPeers(1);
if (newPeer.length === 0) {
this.log.error(
"Failed to find a new peer to replace the disconnected one"
@ -71,7 +62,13 @@ export class BaseProtocolSDK implements IBaseProtocolSDK {
return undefined;
}
this.log.info(`Successfully renewed peer. New peer: ${newPeer[0].id}`);
await Promise.all([
this.peerManager.removePeer(peerToDisconnect),
this.peerManager.addPeer(newPeer[0])
]);
this.log.debug(`Successfully renewed peer. New peer: ${newPeer[0].id}`);
return newPeer[0];
}

View File

@ -73,22 +73,6 @@ export class PeerManager {
}
}
public async disconnectPeer(peerId: PeerId): Promise<boolean> {
try {
this.writeLockHolder = `disconnectPeer: ${peerId.toString()}`;
this.log.info(`Disconnecting peer: ${peerId}`);
await this.connectionManager.dropConnection(peerId);
await this.removePeer(peerId);
this.log.info(`Disconnected peer: ${peerId}`);
this.writeLockHolder = null;
return true;
} catch (error) {
this.log.error("Error disconnecting peer:", error);
this.writeLockHolder = null;
return false;
}
}
/**
* Finds and adds new peers to the peers list.
* @param numPeers The number of peers to find and add.
@ -106,7 +90,7 @@ export class PeerManager {
* Finds additional peers.
* @param numPeers The number of peers to find.
*/
private async findPeers(numPeers: number): Promise<Peer[]> {
public async findPeers(numPeers: number): Promise<Peer[]> {
const connectedPeers = await this.core.getPeers();
return this.readMutex.runExclusive(async () => {
@ -118,7 +102,7 @@ export class PeerManager {
});
}
private async addMultiplePeers(peers: Peer[]): Promise<Peer[]> {
public async addMultiplePeers(peers: Peer[]): Promise<Peer[]> {
const addedPeers: Peer[] = [];
for (const peer of peers) {
await this.addPeer(peer);