Add visibility change beacon and circuit cleanup heuristic for Blazor Server #62789
+193
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
unload
event withpagehide
for sending disconnect beacon from the Blazor client to the server.visibilitychange
event fires on the document.PageHiddenAt
timestamp of the last change to thehidden
state for the session.CircuitHost
instance for the session.visible
state. Null value indicates that the page is currently visible and active (as far as the server knows).CircuitRegistry.DisconnectAsync
(which is called when the SignalR connection breaks).CircuitRegistry.DisconnectedCircuits
memory cache (and later into the persistent state storage).Motivation
We replace the
unload
event withpagehide
because the two should fire in (practically) the same situations but the latter is not deprecated and does not cause warnings for the users.Reasoning behind the visibility-based optimization is as such:
visibilitychange
event is the only one that fires somewhat reliably on mobile devices, in particular when switching tabs in the browser, switching to another app, or going to the home screen.and no other events are processed.
hidden
state, 2) depending on the timeout settings, the server recognizes at some point that the SignalR connection has disconnected.unload
/pagehide
event), we would move the disconnected circuit into theDisconnectedCircuits
memory cache, and later into the persistent state storage. This is wasteful for the mobile scenarios described above as circuits from closed/discarded tabs would never be restored anyway.Risks
pagehide
event fires mostly in the same situations as thevisibilitychange
- taking into account only situations where we actually want to dispose the circuit (closing the tab, navigating away, hard refresh, closing the browser).DisconnectedCircuits
, now we terminate and dispose it (because the server sees the same order of events as e.g. in the mobile app switch scenario) even though the circuit would be eligible for restoration when the device regains network connection and the user foregrounds the Blazor tab.Questions
CircuitOptions
? What should be the default value? Or, should we compute it based on someHubOptions
value (e.g. the client timeout)?Fixes #54793