Skip to content

Commit 937d34e

Browse files
refactor: Optimize startup time with adaptive server pinging (pgadmin-org#9782)
* Fix exponential backoff logic and ping race condition in desktop startup * Adaptive polling mechanism in startDesktopMode(). - Fix exponential backoff to cap at 1000ms instead of cycling back to 100ms - Move pingInProgress flag before the async pingServer() call to prevent race conditions * Update showErrorDialog to use clearTimeout consistent with setTimeout-based scheduling
1 parent ec3e641 commit 937d34e

1 file changed

Lines changed: 23 additions & 12 deletions

File tree

runtime/src/js/pgadmin.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ function openConfigure() {
159159
}
160160
}
161161

162-
function showErrorDialog(intervalID) {
162+
function showErrorDialog(timeoutID) {
163163
if(!splashWindow.isVisible()) {
164164
return;
165165
}
166-
clearInterval(intervalID);
166+
clearTimeout(timeoutID);
167167
splashWindow.close();
168168

169169
new BrowserWindow({
@@ -222,7 +222,7 @@ function startDesktopMode() {
222222
if (pgadminServerProcess != null)
223223
return;
224224

225-
let pingIntervalID;
225+
let pingTimeoutID;
226226
// Set the environment variables so that pgAdmin 4 server
227227
// starts listening on the appropriate port.
228228
process.env.PGADMIN_INT_PORT = serverPort;
@@ -271,7 +271,7 @@ function startDesktopMode() {
271271
// Log the error into the log file if process failed to launch
272272
misc.writeServerLog('Failed to launch pgAdmin4. Error:');
273273
misc.writeServerLog(err);
274-
showErrorDialog(pingIntervalID);
274+
showErrorDialog(pingTimeoutID);
275275
});
276276

277277
let spawnEndTime = (new Date).getTime();
@@ -299,22 +299,27 @@ function startDesktopMode() {
299299
let midTime1 = currentTime + (connectionTimeout / 2);
300300
let midTime2 = currentTime + (connectionTimeout * 2 / 3);
301301
let pingInProgress = false;
302+
let currentPingInterval = 100;
302303

303-
// ping pgAdmin server every 1 second.
304+
// ping pgAdmin server with adaptive polling.
304305
let pingStartTime = (new Date).getTime();
305-
pingIntervalID = setInterval(function () {
306+
307+
function performPing() {
306308
// If ping request is already send and response is not
307309
// received no need to send another request.
308-
if (pingInProgress)
310+
if (pingInProgress) {
311+
pingTimeoutID = setTimeout(performPing, currentPingInterval);
309312
return;
313+
}
310314

315+
pingInProgress = true;
311316
pingServer().then(() => {
312317
pingInProgress = false;
313318
splashWindow.webContents.executeJavaScript('document.getElementById(\'loader-text-status\').innerHTML = \'pgAdmin 4 started\';', true);
314319
// Set the pgAdmin process object to misc
315320
misc.setProcessObject(pgadminServerProcess);
316321

317-
clearInterval(pingIntervalID);
322+
clearTimeout(pingTimeoutID);
318323
let appEndTime = (new Date).getTime();
319324
misc.writeServerLog('------------------------------------------');
320325
misc.writeServerLog('Total time taken to ping pgAdmin4 server: ' + (appEndTime - pingStartTime) / 1000 + ' Sec');
@@ -328,7 +333,8 @@ function startDesktopMode() {
328333
// if the connection timeout has lapsed then throw an error
329334
// and stop pinging the server.
330335
if (curTime >= endTime) {
331-
showErrorDialog(pingIntervalID);
336+
showErrorDialog(pingTimeoutID);
337+
return;
332338
}
333339

334340
if (curTime > midTime1) {
@@ -338,10 +344,15 @@ function startDesktopMode() {
338344
splashWindow.webContents.executeJavaScript('document.getElementById(\'loader-text-status\').innerHTML = \'Almost there...\';', true);
339345
}
340346
}
341-
});
342347

343-
pingInProgress = true;
344-
}, 1000);
348+
pingTimeoutID = setTimeout(performPing, currentPingInterval);
349+
if (currentPingInterval < 1000) {
350+
currentPingInterval = Math.min(currentPingInterval * 2, 1000);
351+
}
352+
});
353+
}
354+
355+
performPing();
345356
}
346357

347358
// This function is used to hide the splash screen and create/launch

0 commit comments

Comments
 (0)