Skip to content

Commit 891a49b

Browse files
committed
Make session expiry checking function more generic, allow custom timeout
1 parent 4ce696e commit 891a49b

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

glean/src/core/internal_metrics.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { generateUUIDv4, isWindowObjectUnavailable } from "./utils.js";
1313
import { Lifetime } from "./metrics/lifetime.js";
1414
import log, { LoggingLevel } from "./log.js";
1515
import { Context } from "./context.js";
16-
import { hasSessionBeenInactiveForOverThirtyMinutes } from "./sessions.js";
16+
import { isSessionInactive } from "./sessions.js";
1717

1818
const LOG_TAG = "core.InternalMetrics";
1919

@@ -193,11 +193,10 @@ export class CoreMetrics {
193193
* 1. If this is the first session (there is no existing session ID),
194194
* then we set a new session ID and a lastActive timestamp.
195195
*
196-
* 2. If the lastActive time is under 30 minutes, then we only update
197-
* the lastActive time.
196+
* 2. If the session is not expired, then we only update the lastActive time.
198197
*
199-
* 3. If the lastActive time is over 30 minutes, then we update the
200-
* session ID, the session sequence number, and the lastActive time.
198+
* 3. If the session is expired (inactive threshold is more recent than lastActive)
199+
* then we update the session ID, the session sequence number, and the lastActive time.
201200
*/
202201
updateSessionInfo(): void {
203202
if (isWindowObjectUnavailable()) {
@@ -211,9 +210,8 @@ export class CoreMetrics {
211210

212211
if (existingSessionId) {
213212
try {
214-
// If over 30 minutes has passed since last session interaction,
215-
// then we create a new session.
216-
if (hasSessionBeenInactiveForOverThirtyMinutes()) {
213+
// If the session has timed out, then we create a new session.
214+
if (isSessionInactive()) {
217215
this.generateNewSession();
218216
}
219217
} catch (e) {

glean/src/core/sessions.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
/**
6-
* Check if the current session has been inactive for over thirty minutes. If
7-
* it has, then we create a new session.
6+
* Check if the current session is inactive - the default timeout is 30 minutes.
87
*
9-
* @returns {boolean} If the session has been inactive for over thirty minutes.
8+
* @param sessionLengthInMinutes Length of the session in minutes - defaults to 30.
9+
* @returns {boolean} If the current session is inactive.
1010
*/
11-
export function hasSessionBeenInactiveForOverThirtyMinutes(): boolean {
11+
export function isSessionInactive(sessionLengthInMinutes = 30): boolean {
1212
const lastActive = localStorage.getItem("glean_session_last_active");
1313
const lastActiveDate = new Date(Number(lastActive));
1414

15-
// Create a date 30 minutes ago to compare to our lastActiveDate.
16-
//
17-
// 60000 - number of milliseconds in a minute
18-
// 30 - the number of minutes that can pass before a session is inactive
19-
const thirtyMinutesAgo = new Date(Date.now() - (60000 * 30));
15+
// Subtract the session length from the current date
16+
const inactiveThreshold = new Date(Date.now() - (60000 * sessionLengthInMinutes));
2017

21-
// If the date we created from 30 minutes ago is more recent than the last
22-
// active date, then the current session has expired.
23-
return thirtyMinutesAgo > lastActiveDate;
18+
// If the inactiveThreshold is more recent than the lastActiveDate, then the
19+
// current session is expired.
20+
return inactiveThreshold > lastActiveDate;
2421
}

0 commit comments

Comments
 (0)