Skip to content

Commit c6135dd

Browse files
committed
improve timeouts config
1 parent 97eec7a commit c6135dd

File tree

6 files changed

+61
-44
lines changed

6 files changed

+61
-44
lines changed

packages/replay/src/replay.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
ReplayContainer as ReplayContainerInterface,
2222
ReplayPluginOptions,
2323
Session,
24+
Timeouts,
2425
} from './types';
2526
import { addEvent } from './util/addEvent';
2627
import { addGlobalListeners } from './util/addGlobalListeners';
@@ -58,10 +59,10 @@ export class ReplayContainer implements ReplayContainerInterface {
5859
* These are here so we can overwrite them in tests etc.
5960
* @hidden
6061
*/
61-
public timeouts = {
62+
public readonly timeouts: Timeouts = {
6263
sessionIdle: SESSION_IDLE_DURATION,
6364
maxSessionLife: MAX_SESSION_LIFE,
64-
};
65+
} as const;
6566

6667
/**
6768
* Options to pass to `rrweb.record()`
@@ -417,8 +418,7 @@ export class ReplayContainer implements ReplayContainerInterface {
417418
*/
418419
private _loadAndCheckSession(): boolean {
419420
const { type, session } = getSession({
420-
expiry: this.timeouts.sessionIdle,
421-
maxSessionLife: this.timeouts.maxSessionLife,
421+
timeouts: this.timeouts,
422422
stickySession: Boolean(this._options.stickySession),
423423
currentSession: this.session,
424424
sessionSampleRate: this._options.sessionSampleRate,
@@ -630,7 +630,7 @@ export class ReplayContainer implements ReplayContainerInterface {
630630
return;
631631
}
632632

633-
const expired = isSessionExpired(this.session, this.timeouts.maxSessionLife, this.timeouts.sessionIdle);
633+
const expired = isSessionExpired(this.session, this.timeouts);
634634

635635
if (breadcrumb && !expired) {
636636
this._createCustomBreadcrumb(breadcrumb);

packages/replay/src/session/getSession.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
import { logger } from '@sentry/utils';
22

3-
import type { Session, SessionOptions } from '../types';
3+
import type { Session, SessionOptions, Timeouts } from '../types';
44
import { isSessionExpired } from '../util/isSessionExpired';
55
import { createSession } from './createSession';
66
import { fetchSession } from './fetchSession';
77
import { makeSession } from './Session';
88

99
interface GetSessionParams extends SessionOptions {
10-
/**
11-
* The length of time (in ms) which we will consider the session to be expired.
12-
*/
13-
expiry: number;
14-
15-
/** How long a session may max. be. */
16-
maxSessionLife: number;
10+
timeouts: Timeouts;
1711

1812
/**
1913
* The current session (e.g. if stickySession is off)
@@ -25,8 +19,7 @@ interface GetSessionParams extends SessionOptions {
2519
* Get or create a session
2620
*/
2721
export function getSession({
28-
expiry,
29-
maxSessionLife,
22+
timeouts,
3023
currentSession,
3124
stickySession,
3225
sessionSampleRate,
@@ -39,7 +32,7 @@ export function getSession({
3932
// If there is a session, check if it is valid (e.g. "last activity" time
4033
// should be within the "session idle time", and "session started" time is
4134
// within "max session time").
42-
const isExpired = isSessionExpired(session, maxSessionLife, expiry);
35+
const isExpired = isSessionExpired(session, timeouts);
4336

4437
if (!isExpired) {
4538
return { type: 'saved', session };

packages/replay/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ export interface SendReplayData {
1818
options: ReplayPluginOptions;
1919
}
2020

21+
export interface Timeouts {
22+
sessionIdle: number;
23+
maxSessionLife: number;
24+
}
25+
2126
/**
2227
* The request payload to worker
2328
*/
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
import type { Session } from '../types';
1+
import type { Session, Timeouts } from '../types';
22
import { isExpired } from './isExpired';
33

44
/**
55
* Checks to see if session is expired
66
*/
7-
export function isSessionExpired(
8-
session: Session,
9-
maxSessionLife: number,
10-
idleTimeout: number,
11-
targetTime: number = +new Date(),
12-
): boolean {
7+
export function isSessionExpired(session: Session, timeouts: Timeouts, targetTime: number = +new Date()): boolean {
138
return (
149
// First, check that maximum session length has not been exceeded
15-
isExpired(session.started, maxSessionLife, targetTime) ||
10+
isExpired(session.started, timeouts.maxSessionLife, targetTime) ||
1611
// check that the idle timeout has not been exceeded (i.e. user has
1712
// performed an action within the last `idleTimeout` ms)
18-
isExpired(session.lastActivity, idleTimeout, targetTime)
13+
isExpired(session.lastActivity, timeouts.sessionIdle, targetTime)
1914
);
2015
}

packages/replay/test/unit/session/getSession.test.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MAX_SESSION_LIFE, WINDOW } from '../../../src/constants';
1+
import { MAX_SESSION_LIFE, SESSION_IDLE_DURATION, WINDOW } from '../../../src/constants';
22
import * as CreateSession from '../../../src/session/createSession';
33
import * as FetchSession from '../../../src/session/fetchSession';
44
import { getSession } from '../../../src/session/getSession';
@@ -42,8 +42,10 @@ describe('Unit | session | getSession', () => {
4242

4343
it('creates a non-sticky session when one does not exist', function () {
4444
const { session } = getSession({
45-
expiry: 900000,
46-
maxSessionLife: MAX_SESSION_LIFE,
45+
timeouts: {
46+
sessionIdle: SESSION_IDLE_DURATION,
47+
maxSessionLife: MAX_SESSION_LIFE,
48+
},
4749
stickySession: false,
4850
...SAMPLE_RATES,
4951
});
@@ -67,8 +69,10 @@ describe('Unit | session | getSession', () => {
6769
saveSession(createMockSession(new Date().getTime() - 10000));
6870

6971
const { session } = getSession({
70-
expiry: 1000,
71-
maxSessionLife: MAX_SESSION_LIFE,
72+
timeouts: {
73+
sessionIdle: 1000,
74+
maxSessionLife: MAX_SESSION_LIFE,
75+
},
7276
stickySession: false,
7377
...SAMPLE_RATES,
7478
});
@@ -81,8 +85,10 @@ describe('Unit | session | getSession', () => {
8185

8286
it('creates a non-sticky session, when one is expired', function () {
8387
const { session } = getSession({
84-
expiry: 1000,
85-
maxSessionLife: MAX_SESSION_LIFE,
88+
timeouts: {
89+
sessionIdle: 1000,
90+
maxSessionLife: MAX_SESSION_LIFE,
91+
},
8692
stickySession: false,
8793
...SAMPLE_RATES,
8894
currentSession: makeSession({
@@ -105,8 +111,10 @@ describe('Unit | session | getSession', () => {
105111
expect(FetchSession.fetchSession()).toBe(null);
106112

107113
const { session } = getSession({
108-
expiry: 900000,
109-
maxSessionLife: MAX_SESSION_LIFE,
114+
timeouts: {
115+
sessionIdle: SESSION_IDLE_DURATION,
116+
maxSessionLife: MAX_SESSION_LIFE,
117+
},
110118
stickySession: true,
111119
sessionSampleRate: 1.0,
112120
errorSampleRate: 0.0,
@@ -138,8 +146,10 @@ describe('Unit | session | getSession', () => {
138146
saveSession(createMockSession(now));
139147

140148
const { session } = getSession({
141-
expiry: 1000,
142-
maxSessionLife: MAX_SESSION_LIFE,
149+
timeouts: {
150+
sessionIdle: 1000,
151+
maxSessionLife: MAX_SESSION_LIFE,
152+
},
143153
stickySession: true,
144154
sessionSampleRate: 1.0,
145155
errorSampleRate: 0.0,
@@ -162,8 +172,10 @@ describe('Unit | session | getSession', () => {
162172
saveSession(createMockSession(new Date().getTime() - 2000));
163173

164174
const { session } = getSession({
165-
expiry: 1000,
166-
maxSessionLife: MAX_SESSION_LIFE,
175+
timeouts: {
176+
sessionIdle: 1000,
177+
maxSessionLife: MAX_SESSION_LIFE,
178+
},
167179
stickySession: true,
168180
...SAMPLE_RATES,
169181
});
@@ -179,8 +191,10 @@ describe('Unit | session | getSession', () => {
179191

180192
it('fetches a non-expired non-sticky session', function () {
181193
const { session } = getSession({
182-
expiry: 1000,
183-
maxSessionLife: MAX_SESSION_LIFE,
194+
timeouts: {
195+
sessionIdle: 1000,
196+
maxSessionLife: MAX_SESSION_LIFE,
197+
},
184198
stickySession: false,
185199
...SAMPLE_RATES,
186200
currentSession: makeSession({

packages/replay/test/unit/util/isSessionExpired.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,28 @@ function createSession(extra?: Record<string, any>) {
1515

1616
describe('Unit | util | isSessionExpired', () => {
1717
it('session last activity is older than expiry time', function () {
18-
expect(isSessionExpired(createSession(), MAX_SESSION_LIFE, 100, 200)).toBe(true); // Session expired at ts = 100
18+
expect(isSessionExpired(createSession(), { maxSessionLife: MAX_SESSION_LIFE, sessionIdle: 100 }, 200)).toBe(true); // Session expired at ts = 100
1919
});
2020

2121
it('session last activity is not older than expiry time', function () {
22-
expect(isSessionExpired(createSession({ lastActivity: 100 }), MAX_SESSION_LIFE, 150, 200)).toBe(false); // Session expires at ts >= 250
22+
expect(
23+
isSessionExpired(
24+
createSession({ lastActivity: 100 }),
25+
{ maxSessionLife: MAX_SESSION_LIFE, sessionIdle: 150 },
26+
200,
27+
),
28+
).toBe(false); // Session expires at ts >= 250
2329
});
2430

2531
it('session age is not older than max session life', function () {
26-
expect(isSessionExpired(createSession(), MAX_SESSION_LIFE, 1_800_000, 50_000)).toBe(false);
32+
expect(
33+
isSessionExpired(createSession(), { maxSessionLife: MAX_SESSION_LIFE, sessionIdle: 1_800_000 }, 50_000),
34+
).toBe(false);
2735
});
2836

2937
it('session age is older than max session life', function () {
30-
expect(isSessionExpired(createSession(), MAX_SESSION_LIFE, 1_800_000, 1_800_001)).toBe(true); // Session expires at ts >= 1_800_000
38+
expect(
39+
isSessionExpired(createSession(), { maxSessionLife: MAX_SESSION_LIFE, sessionIdle: 1_800_000 }, 1_800_001),
40+
).toBe(true); // Session expires at ts >= 1_800_000
3141
});
3242
});

0 commit comments

Comments
 (0)