Skip to content

Commit 406ebec

Browse files
committed
more fixes for idle timeout
1 parent 1e75018 commit 406ebec

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/PerfProblemSimulator/Hubs/MetricsHub.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,24 @@ public async Task WakeUp()
153153
};
154154
await Clients.Caller.ReceiveIdleState(idleData);
155155
}
156+
157+
/// <summary>
158+
/// Returns the server's current idle state without waking it.
159+
/// Called by the client after auto-reconnect to determine whether the
160+
/// server is idle (and therefore the client should disconnect again)
161+
/// or active (and the client should stay connected).
162+
/// </summary>
163+
[UsedImplicitly]
164+
public IdleStateData GetIdleState()
165+
{
166+
var isIdle = _idleStateService.IsIdle;
167+
return new IdleStateData
168+
{
169+
IsIdle = isIdle,
170+
Message = isIdle
171+
? "Application is idle, no health probes being sent."
172+
: "Application is active",
173+
Timestamp = DateTimeOffset.UtcNow
174+
};
175+
}
156176
}

src/PerfProblemSimulator/wwwroot/js/dashboard.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,34 @@ async function initializeSignalR() {
246246

247247
// Handle connection state changes
248248
state.connection.onreconnecting(error => {
249+
// If we intentionally disconnected for idle, suppress reconnect UI
250+
if (state.intentionalDisconnect) return;
249251
updateConnectionStatus('connecting', 'Reconnecting...');
250252
logEvent('system', 'Connection lost. Attempting to reconnect...');
251253
});
252254

253255
state.connection.onreconnected(async connectionId => {
254-
// Reset intentional disconnect flag - any successful connection means we're active
255256
state.intentionalDisconnect = false;
256-
257-
// WS is intentionally closed during idle, so any reconnect means we're active
258-
updateConnectionStatus('connected', 'Connected');
259257
logEvent('system', 'Reconnected to server');
258+
259+
// After auto-reconnect, check if the server is idle.
260+
// The server does NOT send idle state on connect (to avoid a race
261+
// with WakeUp on initial page load), so we must query it explicitly.
262+
try {
263+
const idleData = await state.connection.invoke('GetIdleState');
264+
if (idleData.isIdle) {
265+
// Server is idle — re-enter idle mode and disconnect
266+
handleIdleState(idleData);
267+
} else {
268+
// Server is active — show connected and resume chart
269+
state.isIdle = false;
270+
updateConnectionStatus('connected', 'Connected');
271+
startLatencyChartUpdates();
272+
}
273+
} catch (err) {
274+
// Query failed — assume connected
275+
updateConnectionStatus('connected', 'Connected');
276+
}
260277
});
261278

262279
state.connection.onclose(error => {

0 commit comments

Comments
 (0)