Attempt to stabilize channel reconnecting

This commit is contained in:
Raven Scott
2025-12-20 19:09:23 -05:00
parent 575d1c359d
commit 3deb968553
2 changed files with 34 additions and 2 deletions
+6 -1
View File
@@ -1306,9 +1306,14 @@ function performHealthCheck() {
const peerChannel = channelInfo.peerChannels?.get(peerId);
if (!peerChannel) {
// Peer is connected but missing this channel - create it
logDebug('ChannelManager', `Peer ${peerId.substring(0, 16)}... connected but missing channel ${pluginDomain}-${protocol}, creating it`);
const isReconnection = state.peerHistory?.get(peerId)?.some(event => event.type === 'connect') || false;
const reconnectionNote = isReconnection ? ' (reconnection)' : '';
logDebug('ChannelManager', `Peer ${peerId.substring(0, 16)}... connected but missing channel ${pluginDomain}-${protocol}${reconnectionNote}, creating it`);
try {
createPluginChannelsForPeer(peerId, peerChannelInfo.conn, peerChannelInfo.mux);
if (isReconnection) {
logInfo('ChannelManager', `Successfully recreated missing channel ${pluginDomain}-${protocol} for reconnected peer ${peerId.substring(0, 16)}...`);
}
} catch (err) {
logError('ChannelManager', `Error creating missing channel for peer ${peerId.substring(0, 16)}...: ${err.message}`);
}
+28 -1
View File
@@ -1688,13 +1688,38 @@ async function main() {
});
// Create all plugin channels for this peer connection (including core p2ns channels)
const isReconnection = isPeerReconnection(peerId);
try {
channelManager.createPluginChannelsForPeer(peerId, conn, mux);
logDebug('Swarm', `Created channels for peer ${peerId} via channel-manager`);
const connectionType = isReconnection ? 'reconnected' : 'new';
const nodeType = isMaster ? 'master' : 'joiner';
logDebug('Swarm', `Created channels for ${connectionType} ${nodeType} peer ${peerId.substring(0, 16)}... via channel-manager`);
} catch (err) {
logError('Swarm', `Error creating plugin channels for peer ${peerId}: ${err.message}`);
}
// Register peer connection with channel manager for proper tracking
try {
channelManager.registerPeerConnection(peerId, conn, mux);
logDebug('Swarm', `Registered peer connection with channel manager: ${peerId.substring(0, 16)}...`);
// For reconnections, run health check immediately to ensure channels are properly recreated
if (isPeerReconnection(peerId)) {
const nodeType = isMaster ? 'master' : 'joiner';
logInfo('Swarm', `${nodeType} peer ${peerId.substring(0, 16)}... reconnected - ensuring channels are properly recreated`);
setTimeout(() => {
try {
channelManager.performHealthCheck();
logDebug('Swarm', `Ran immediate health check for reconnected ${nodeType} peer ${peerId.substring(0, 16)}...`);
} catch (err) {
logWarn('Swarm', `Error running immediate health check for reconnected ${nodeType} peer: ${err.message}`);
}
}, 100); // Small delay to allow connection to stabilize
}
} catch (err) {
logWarn('Swarm', `Error registering peer connection with channel manager: ${err.message}`);
}
// Master node: Send proactive invite to new peers or reconnected peers
if (isMaster && state.dnsPass) {
const pass = state.dnsPass;
@@ -1930,6 +1955,8 @@ async function main() {
try {
const channelManager = require('./includes/plugins/channel-manager');
channelManager.handlePeerDisconnect(peerId);
channelManager.unregisterPeerConnection(peerId);
logDebug('Swarm', `Unregistered peer connection from channel manager: ${peerId.substring(0, 16)}...`);
} catch (err) {
logError('Swarm', `Error handling plugin channel disconnect for peer ${peerId}: ${err.message}`);
}