fix(collection): avoid buffering if creating a collection during a connection interruption #15187
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.
Summary
Should be a fix for #14971.
In Mongoose 7+8, buffering should only happen before
mongoose.connect()
has been called. The reason why Mongoose still has buffering is that the MongoDB node driver will error out if you try to send a database operation on a MongoClient instance that you haven't calledconnect()
on yet. But once the MongoDB Node driver has connected successfully to MongoDB at least once, database operations likefind()
etc. handle retries and disconnection via an internal server selection process, so Mongoose buffering is unnecessary.When investigating #14971, I found that Mongoose may still buffer if the collection is created during a connection interruption. For example, in the following script,
console.log('Buffer', TestModel.collection.buffer);
will print "Buffer true" if you shut down the MongoDB server on 127.0.0.1:27017 when "Waiting 5 seconds, shut down the MongoDB server now" printsWith this fix, buffering will not kick in, the above script will print "Buffer false". Which should prevent unintended buffering in the future.
_hasOpened
is a property that Mongoose sets on the connection the first time the connection'sreadyState
is set to1
(connected)In the interest of being cautious, I'm putting this and #14971 in the 8.10 milestone.
Examples