fix spider proxy

pull/27/head
accius 4 days ago
parent 330c2ba14f
commit 424bc07111

@ -26,8 +26,8 @@ const CONFIG = {
], ],
callsign: process.env.CALLSIGN || 'OPENHAMCLOCK', callsign: process.env.CALLSIGN || 'OPENHAMCLOCK',
spotRetentionMs: 30 * 60 * 1000, // 30 minutes spotRetentionMs: 30 * 60 * 1000, // 30 minutes
reconnectDelayMs: 5000, // 5 seconds between reconnect attempts reconnectDelayMs: 10000, // 10 seconds between reconnect attempts
maxReconnectAttempts: 5, maxReconnectAttempts: 3,
cleanupIntervalMs: 60000, // 1 minute cleanupIntervalMs: 60000, // 1 minute
keepAliveIntervalMs: 120000 // 2 minutes - send keepalive keepAliveIntervalMs: 120000 // 2 minutes - send keepalive
}; };
@ -36,6 +36,7 @@ const CONFIG = {
let spots = []; let spots = [];
let client = null; let client = null;
let connected = false; let connected = false;
let connecting = false; // NEW: Prevent concurrent connection attempts
let currentNode = null; let currentNode = null;
let currentNodeIndex = 0; let currentNodeIndex = 0;
let reconnectAttempts = 0; let reconnectAttempts = 0;
@ -171,18 +172,29 @@ const cleanupSpots = () => {
// Connect to DX Spider // Connect to DX Spider
const connect = () => { const connect = () => {
if (client) { // Prevent concurrent connection attempts
try { if (connecting) {
client.destroy(); log('CONNECT', 'Connection attempt already in progress, skipping');
} catch (e) {} return;
client = null;
} }
connecting = true;
// Clear any pending reconnect timer
if (reconnectTimer) { if (reconnectTimer) {
clearTimeout(reconnectTimer); clearTimeout(reconnectTimer);
reconnectTimer = null; reconnectTimer = null;
} }
// Clean up existing client without triggering reconnect
if (client) {
try {
client.removeAllListeners(); // Remove listeners BEFORE destroy to prevent close->reconnect loop
client.destroy();
} catch (e) {}
client = null;
}
const node = CONFIG.nodes[currentNodeIndex]; const node = CONFIG.nodes[currentNodeIndex];
currentNode = node; currentNode = node;
@ -193,6 +205,7 @@ const connect = () => {
client.connect(node.port, node.host, () => { client.connect(node.port, node.host, () => {
connected = true; connected = true;
connecting = false;
reconnectAttempts = 0; reconnectAttempts = 0;
connectionStartTime = new Date(); connectionStartTime = new Date();
buffer = ''; buffer = '';
@ -233,16 +246,21 @@ const connect = () => {
client.on('timeout', () => { client.on('timeout', () => {
log('TIMEOUT', 'Connection timed out'); log('TIMEOUT', 'Connection timed out');
connecting = false;
handleDisconnect(); handleDisconnect();
}); });
client.on('error', (err) => { client.on('error', (err) => {
log('ERROR', `Connection error: ${err.message}`); log('ERROR', `Connection error: ${err.message}`);
connecting = false;
handleDisconnect(); handleDisconnect();
}); });
client.on('close', () => { client.on('close', () => {
if (connected) {
log('CLOSE', 'Connection closed'); log('CLOSE', 'Connection closed');
}
connecting = false;
handleDisconnect(); handleDisconnect();
}); });
}; };
@ -268,13 +286,24 @@ const startKeepAlive = () => {
// Handle disconnection and reconnection // Handle disconnection and reconnection
const handleDisconnect = () => { const handleDisconnect = () => {
// Prevent re-entrant calls
if (!connected && !connecting && reconnectTimer) {
return; // Already disconnected and reconnect scheduled
}
connected = false; connected = false;
connecting = false;
if (keepAliveTimer) { if (keepAliveTimer) {
clearInterval(keepAliveTimer); clearInterval(keepAliveTimer);
keepAliveTimer = null; keepAliveTimer = null;
} }
// Don't schedule another reconnect if one is already pending
if (reconnectTimer) {
return;
}
reconnectAttempts++; reconnectAttempts++;
if (reconnectAttempts >= CONFIG.maxReconnectAttempts) { if (reconnectAttempts >= CONFIG.maxReconnectAttempts) {
@ -284,9 +313,10 @@ const handleDisconnect = () => {
log('FAILOVER', `Switching to node: ${CONFIG.nodes[currentNodeIndex].name}`); log('FAILOVER', `Switching to node: ${CONFIG.nodes[currentNodeIndex].name}`);
} }
log('RECONNECT', `Attempting reconnect in ${CONFIG.reconnectDelayMs}ms (attempt ${reconnectAttempts + 1})`); log('RECONNECT', `Attempting reconnect in ${CONFIG.reconnectDelayMs}ms (attempt ${reconnectAttempts})`);
reconnectTimer = setTimeout(() => { reconnectTimer = setTimeout(() => {
reconnectTimer = null;
connect(); connect();
}, CONFIG.reconnectDelayMs); }, CONFIG.reconnectDelayMs);
}; };

Loading…
Cancel
Save

Powered by TurnKey Linux.