Tag requests sent while the user is idle
What does this MR do and why?
This is the frontend half of stopping idle background polls from extending the user's session.
Any authenticated request refreshes the Rails session. A tab that only polls (MR widget, pipelines list) therefore never idle-times-out, even if nobody is using it.
The backend half is !258104 (merged) (draft). Classifying requests on the server by read vs write alone is too wide: in Vue single-page views, deliberate actions (route changes, filtering, paging, search) are also reads, so an active user could be signed out. This MR gives the server a signal about whether the user is actually present.
What changes:
- New
~/lib/utils/user_activity.jsrecords the time of the last user input:pointerdown,pointermove,keydown,wheel,scroll.- Listeners are passive and capture-phase on
window, so non-bubblingscrollon elements counts too. - Page load counts as activity.
isUserIdle()is true after 60 seconds without input.
- Listeners are passive and capture-phase on
- The existing axios request interceptor in
axios_utils.js(the one that counts pending requests) addsX-GitLab-Background-Request: 1to same-origin requests while the user is idle. It reuses that interceptor because a separate one would add an asynchronous step to every request and shift request timing app-wide (it broke a Vue 3 spec that way). Cross-origin requests are not tagged, because a custom header there needs the other server to allow it in CORS. - New Apollo link
~/lib/apollo/background_request_link.jsadds the same header to GraphQL requests. It sits in the link chain inlib/graphql.js, before the batch/HTTP links.
Effect on its own: none. The backend does not read the header yet.
When !258104 (merged) lands and its skip_session_extension_for_background_requests feature flag is on, the server skips the session write only if the header is present AND the request is a read. Writes and navigations always extend the session.
Notes:
- Requests that bypass axios and Apollo (direct
fetch,EventSource) are never tagged and keep today's behavior. - There is no frontend feature flag. The header is inert until the backend flag is on, and turning that flag off undoes the effect.
References
- Work item: #630607
- Backend half (draft, behind
skip_session_extension_for_background_requests): !258104 (merged) - Approach proposed to the reviewer: !258104 (comment 3957264693)
- Earlier, narrower attempt (ETag-cached routes only, closed as too narrow): !257397 (closed)
Screenshots or screen recordings
Not applicable: no visible UI change.
How to set up and validate locally
No feature flag is needed for this MR.
- Check out this branch.
- Open a page that polls, for example a merge request page (MR widget polling) or a project's Pipelines page.
- Open the browser DevTools Network tab and leave the tab without any input (mouse, keyboard, scroll, touch) for over a minute.
- Confirm the polling requests (JSON and
graphql) now carry the request headerX-GitLab-Background-Request: 1. - Move the mouse, click, or type. Confirm the next requests no longer carry that header.
- Confirm cross-origin requests never carry the header.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.