-
Notifications
You must be signed in to change notification settings - Fork 151
Solving race condition between flushes. #316
Solving race condition between flushes. #316
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @felipe-najson-ntf for the PR. Looks like there is a CI failure, can you please look into it? Also do we need to do a major version bump for this change?
FYI, this pull does not solve #309 unless the expectation is that the consumer must now Namely, every call to I believe this violates users' expectations. Users expect to be able to:
As such, I believe making these methods async is not the right solution. Instead, the library should track in-progress flushes and provide a method that awaits all in-progress flushes. This could be as simple as maintaining an array of all axios promises and then Further, making these methods async would cause existing code to violate ESLint's I hope you don't take this the wrong way. Async queues can be a hard problem. Luckily they are a solved problem so there is no need to reinvent the wheel. Instead of implementing this yourself, I would use an existing solution. For example, you could use async's cargo queue like so: class Analytics {
constructor() {
const maxConcurrentRequests = 10;
const maxMessagesPerRequest = 100;
this.cargoQueue = async.cargoQueue((messages, cb) => {
sendMessagesToServer(messages, err => {
// ...
});
}, maxConcurrentRequests, maxMessagesPerRequest);
}
enqueue(message, cb) {
this.cargoQueue.push(message, cb);
}
flush(cb) {
this.cargoQueue.drain(cb);
}
} Close in functionality to your current implementation. If you wanted finer control you could manually |
Enqueue's flush now awaits before continuing.
There are cases where race conditions can be given like the examples presented in issue #309. Implementing it asynchronously we make sure that the in-flight flushes ended before continuing.
This problem is also related to the strange behavior that the user has in issue #303