@@ -23,7 +23,6 @@ import { display } from '../../tools/display'
2323import { isSampled } from '../sampler'
2424import { TelemetryMetrics , addTelemetryMetrics } from '../telemetry'
2525import { monitorError } from '../../tools/monitor'
26- import { SESSION_TIME_OUT_DELAY } from './sessionConstants'
2726import type { SessionState } from './sessionState'
2827import {
2928 expandOnly ,
@@ -38,7 +37,10 @@ import { getSessionStoreStrategy, selectSessionStoreStrategyType } from './sessi
3837
3938export interface SessionManager {
4039 findSession : ( startTime ?: RelativeTime , options ?: { returnInactive : boolean } ) => SessionContext | undefined
41- findTrackedSession : ( startTime ?: RelativeTime , options ?: { returnInactive : boolean } ) => SessionContext | undefined
40+ findTrackedSession : (
41+ startTime ?: RelativeTime ,
42+ options ?: { returnInactive ?: boolean ; maxAge ?: number }
43+ ) => SessionContext | undefined
4244 renewObservable : Observable < void >
4345 expireObservable : Observable < void >
4446 expire : ( ) => void
@@ -53,7 +55,12 @@ export interface SessionContext {
5355}
5456
5557export const VISIBILITY_CHECK_DELAY = ONE_MINUTE
56- const SESSION_CONTEXT_TIMEOUT_DELAY = SESSION_TIME_OUT_DELAY
58+
59+ // Arbitrary value to cap memory consumption for very long-lived pages with many session
60+ // renewals. Entries are *not* evicted based on elapsed time: an idle session's sole (closed)
61+ // entry must survive indefinitely so browser-logs can keep sending logs after it expires, with
62+ // or without a session attached (see browser-logs/src/domain/contexts/sessionContext.ts).
63+ export const MAX_SESSION_CONTEXT_HISTORY_ENTRIES = 1000
5764
5865// Maximum duration for which we can send data related to a session.
5966//
@@ -84,7 +91,7 @@ export async function startSessionManager(
8491 const strategy = mockable ( getSessionStoreStrategy ) ( sessionStoreStrategyType , configuration )
8592
8693 const sessionContextHistory = createValueHistory < SessionContext > ( {
87- expireDelay : SESSION_CONTEXT_TIMEOUT_DELAY ,
94+ maxEntries : MAX_SESSION_CONTEXT_HISTORY_ENTRIES ,
8895 } )
8996 stopCallbacks . push ( ( ) => sessionContextHistory . stop ( ) )
9097
@@ -216,14 +223,14 @@ export async function startSessionManager(
216223 function buildSessionManager ( ) : SessionManager {
217224 return {
218225 findSession : ( startTime , options ) => sessionContextHistory . find ( startTime , options ) ,
219- findTrackedSession : ( startTime , options ) => {
220- const session = sessionContextHistory . find ( startTime , options )
226+ findTrackedSession : ( startTime , { returnInactive = false , maxAge = TRACKED_SESSION_MAX_AGE } = { } ) => {
227+ const session = sessionContextHistory . find ( startTime , { returnInactive } )
221228
222229 if ( ! session || session . id === 'invalid' || ! isSampled ( session . id , configuration . sessionSampleRate ) ) {
223230 return
224231 }
225232
226- if ( dateNow ( ) - session . createdAt > TRACKED_SESSION_MAX_AGE ) {
233+ if ( dateNow ( ) - session . createdAt > maxAge ) {
227234 return
228235 }
229236
0 commit comments