Skip to content

Commit 23abcbd

Browse files
committed
Coalesce overlapping automatic session starts
1 parent e66c52d commit 23abcbd

3 files changed

Lines changed: 59 additions & 3 deletions

File tree

‎.agents/skills/exceptionless-javascript/references/sessions.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ await Exceptionless.startup((config) => {
2828

2929
Session lifetime in the browser and React Native:
3030

31-
- `startup()` sends a session start once. Resuming after a hidden tab continues the same session (and its heartbeats) unless the client was suspended for more than 5 minutes, after which the server has closed the session and a new one starts.
31+
- `startup()` sends a session start once. Resuming after a hidden tab continues the same session (and its heartbeats) unless the client was suspended for more than 5 minutes, after which the server has closed the session and a new one starts. Overlapping startups share the automatic session start. If a plugin cancels it, a later startup can retry.
3232
- The browser ends the session on `pagehide` and starts a new one if the page is restored from the back/forward cache. React Native ends it when the app goes to the background and starts a new one when it becomes active.
3333

3434
Disable heartbeat traffic while still using session events:

‎packages/core/src/ExceptionlessClient.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class ExceptionlessClient {
1313
private _intervalId: ReturnType<typeof setInterval> | undefined;
1414
private _timeoutId: ReturnType<typeof setTimeout> | undefined;
1515
private _sessionActive = false;
16+
private _startingSession: Promise<EventPluginContext> | undefined;
1617
private _suspendedAt: number | undefined;
1718
/** The server closes sessions that have had no activity for this long. */
1819
private readonly SESSION_INACTIVE_PERIOD = 5 * 60 * 1000;
@@ -66,8 +67,15 @@ export class ExceptionlessClient {
6667
const suspendedAt = this._suspendedAt;
6768
this._suspendedAt = undefined;
6869
const sessionExpired = suspendedAt !== undefined && Date.now() - suspendedAt > this.SESSION_INACTIVE_PERIOD;
69-
if (this.config.sessionsEnabled && (!this._sessionActive || sessionExpired)) {
70-
await this.submitSessionStart();
70+
if (sessionExpired) {
71+
this._sessionActive = false;
72+
}
73+
74+
if (this.config.sessionsEnabled && !this._sessionActive) {
75+
this._startingSession ??= this.submitSessionStart().finally(() => {
76+
this._startingSession = undefined;
77+
});
78+
await this._startingSession;
7179
}
7280
}
7381

‎packages/core/test/ExceptionlessClientLifecycle.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,54 @@ describe("ExceptionlessClient lifecycle", () => {
114114
expect(client.config.apiKey).toBe("UNIT_TEST_API_KEY");
115115
});
116116

117+
test("concurrent startups share the session start", async () => {
118+
// Arrange
119+
const { client, enqueued } = createClient();
120+
track(client);
121+
client.config.apiKey = "UNIT_TEST_API_KEY";
122+
client.config.useSessions(false, undefined, true);
123+
124+
// Act
125+
await Promise.all([client.startup(), client.startup()]);
126+
127+
// Assert
128+
expect(enqueued.filter((event) => event.type === "session")).toHaveLength(1);
129+
});
130+
131+
test("a cancelled start after session expiry can be retried", async () => {
132+
// Arrange
133+
vi.useFakeTimers({
134+
toFake: ["Date"]
135+
});
136+
const { client, enqueued } = createClient();
137+
track(client);
138+
client.config.apiKey = "UNIT_TEST_API_KEY";
139+
client.config.useSessions(false, undefined, true);
140+
await client.startup();
141+
await client.suspend();
142+
vi.setSystemTime(Date.now() + 6 * 60 * 1000);
143+
let attempts = 0;
144+
client.config.addPlugin({
145+
name: "CancelFirstStart",
146+
priority: 1,
147+
run(context) {
148+
if (context.event.type === "session") {
149+
context.cancelled = ++attempts === 1;
150+
}
151+
return Promise.resolve();
152+
}
153+
});
154+
155+
// Act
156+
await Promise.all([client.startup(), client.startup()]);
157+
expect(attempts).toBe(1);
158+
await client.startup();
159+
160+
// Assert
161+
expect(attempts).toBe(2);
162+
expect(enqueued.filter((event) => event.type === "session")).toHaveLength(2);
163+
});
164+
117165
test("resuming after a short suspension continues the current session", async () => {
118166
const { client, enqueued } = createClient();
119167
track(client);

0 commit comments

Comments
 (0)