This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
flush: guarantee that all inflight messages are sent #353
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Create this PR to check if test pass without snyk
MichaelGHSeg
approved these changes
Nov 16, 2022
This does not resolve #309 as there are still multiple race conditions in this solution. Did you really not even bother to test if the solution fixed at least the example scenarios? The original example from #309 still fails: const analytics = new Analytics(writeKey, {flushAt: 2});
analytics.track(...); // This will trigger a flush (flush A) because line 215: flushed === false
// pendingFlush is now set to flush A's promise.
analytics.track(...);
analytics.track(...); // This will trigger a flush (flush B) because line 221: queue.length >= flushAt
// flush B awaits pendingFlush (flush A's promise)
// The following will trigger a flush (flush C) because there is a message in the queue.
// (Flush B is still awaiting flush A and has not emptied the queue yet.)
// Flush C awaits pendingFlush (flush A's promise - NOT flush B's promise)
analytics.flush().then(() => {
// This calllback may be called before flush B completes.
// The process may exit while the message from flush B is still inflight.
console.log('done');
process.exit(0);
});
// Once flush A's promise resolves, there are now 2 concurrent flushes (B and C) running in parallel.
// Further, flush C contains 0 events.
// Waiting on flush C creates a race condition as it may finish before flush B. And this slightly modified version of the second example from #309 still fails: const analytics = new Analytics(writeKey, {flushAt: 2});
analytics.track(...); // This will trigger a flush (flush A) because line 215: flushed === false
// pendingFlush is now set to flush A's promise.
analytics.track(...);
analytics.track(...); // This will trigger a flush (flush B) because line 221: queue.length >= flushAt
// Flush B awaits pendingFlush (flush A's promise)
analytics.track(...);
// The following will trigger a flush (flush C) because there is a message in the queue.
// Flush C awaits pendingFlush (flush A's promise - NOT flush B's promise)
analytics.flush().then(() => {
// This calllback may be called before flush B completes.
// The process may exit while the message from flush B is still inflight.
console.log('done');
process.exit(0);
});
// Once flush A's promise resolves, there are now 2 concurrent flushes (B and C) running in parallel.
// Waiting on flush C creates a race condition as it may finish before flush B. Here is an even simpler example: const analytics = new Analytics(writeKey);
analytics.flushed = true;
analytics.track('A');
analytics.flush();
analytics.track('B');
analytics.flush();
analytics.track('C');
await analytics.flush();
process.exit(0);
|
Can we get this added as a proper test case and ensure it is covered? Having a |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Simple fix so flush waits for previous inflight messages to be sent.
Resolves #309