Skip to content

Commit d265071

Browse files
committed
Keep events when storage throws synchronously; wait for in-flight runs
- localStorage throws synchronously when its quota is full, which rejected enqueue() before the event was queued. Synchronous throws are now handled like rejections and the event is sent from memory. - process() returns the run that is already sending instead of resolving immediately, so callers like suspend() and flush() wait for in-flight submissions.
1 parent 405003a commit d265071

2 files changed

Lines changed: 54 additions & 10 deletions

File tree

‎packages/core/src/queue/DefaultEventQueue.ts‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ export class DefaultEventQueue implements IEventQueue {
4242
private _discardQueuedItemsUntil?: Date;
4343

4444
/**
45-
* Returns true if the queue is processing.
46-
* @type {boolean}
45+
* The queue run that is currently sending events, if any.
4746
* @private
4847
*/
49-
private _processingQueue = false;
48+
private _processing: Promise<void> | undefined;
5049

5150
/**
5251
* Processes the queue every xx seconds.
@@ -95,8 +94,9 @@ export class DefaultEventQueue implements IEventQueue {
9594
const queueNotProcessed = "The queue will not be processed";
9695
const { log } = this.config.services;
9796

98-
if (this._processingQueue) {
99-
return;
97+
if (this._processing) {
98+
// Callers such as flush() need to wait for the run that is already sending events.
99+
return this._processing;
100100
}
101101

102102
log.trace("Processing queue...");
@@ -115,7 +115,14 @@ export class DefaultEventQueue implements IEventQueue {
115115
return;
116116
}
117117

118-
this._processingQueue = true;
118+
this._processing = this.processBatches().finally(() => {
119+
this._processing = undefined;
120+
});
121+
return this._processing;
122+
}
123+
124+
private async processBatches(): Promise<void> {
125+
const { log } = this.config.services;
119126
try {
120127
if (this._loadPersistedEvents) {
121128
if (this.config.usePersistedQueueStorage) {
@@ -150,8 +157,6 @@ export class DefaultEventQueue implements IEventQueue {
150157
} catch (ex) {
151158
log.error(`Error processing queue: ${ex instanceof Error ? ex.message : ex + ""}`);
152159
await this.suspendProcessing();
153-
} finally {
154-
this._processingQueue = false;
155160
}
156161
}
157162

@@ -217,7 +222,7 @@ export class DefaultEventQueue implements IEventQueue {
217222
}
218223

219224
private async onProcessQueue(): Promise<void> {
220-
if (!this.isQueueProcessingSuspended() && !this._processingQueue) {
225+
if (!this.isQueueProcessingSuspended() && !this._processing) {
221226
await this.process();
222227
}
223228
}
@@ -425,7 +430,8 @@ export class DefaultEventQueue implements IEventQueue {
425430
};
426431
if (useStorage) {
427432
// Only an entry that was actually written counts as persisted; a failed write is still sent from memory.
428-
item.write = storage.setItem(file, JSON.stringify(event)).then(
433+
// The executor turns a synchronous throw (for example, a full localStorage quota) into a rejection.
434+
item.write = new Promise<void>((resolve) => resolve(storage.setItem(file, JSON.stringify(event)))).then(
429435
() => {
430436
item.persisted = true;
431437
},

‎packages/core/test/queue/DefaultEventQueue.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,42 @@ describe("DefaultEventQueue submission", () => {
376376
expect(client.submissions).toHaveLength(0);
377377
expect(await storedQueueKeys()).toEqual(["q:1000.json"]);
378378
});
379+
380+
test("sends an event from memory when storage throws synchronously", async () => {
381+
const storage = new InMemoryStorage();
382+
vi.spyOn(storage, "setItem").mockImplementation(() => {
383+
throw new Error("QuotaExceededError");
384+
});
385+
config.services.storage = storage;
386+
387+
await config.services.queue.enqueue(createEvent("event-0001"));
388+
await config.services.queue.process();
389+
390+
expect(client.submittedReferenceIds).toEqual(["event-0001"]);
391+
});
392+
393+
test("process waits for a run that is already sending", async () => {
394+
let finishSubmission: () => void = () => {};
395+
const submitEvents = client.submitEvents.bind(client);
396+
vi.spyOn(client, "submitEvents").mockImplementationOnce(
397+
(events) =>
398+
new Promise((resolve) => {
399+
finishSubmission = () => void submitEvents(events).then(resolve);
400+
})
401+
);
402+
await config.services.queue.enqueue(createEvent("event-0001"));
403+
404+
const first = config.services.queue.process();
405+
let secondFinished = false;
406+
const second = config.services.queue.process().then(() => {
407+
secondFinished = true;
408+
});
409+
await delay(10);
410+
expect(secondFinished).toBe(false);
411+
412+
finishSubmission();
413+
await Promise.all([first, second]);
414+
expect(secondFinished).toBe(true);
415+
expect(await storedQueueKeys()).toHaveLength(0);
416+
});
379417
});

0 commit comments

Comments
 (0)