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.
This PR is related to #204. After changes made in that PR, downloading CloudFetch results sometimes started to fail with
ECONNRESET: socket hang up
error. After debugging, I've found a root cause. Even though all the library code is Promise-based, since Promises are microtasks, enqueueing a lot of promises may block macrotasks execution for a while. Usually, there are no much microtasks scheduled. However, when fetching a CloudFetch results, after first set of files are downloaded and being processed immediately one by one (and chunk by chunk), event loop easily gets clogged for enough time to break connection pool.http.Agent
stops receiving socket events, and marks all sockets invalid on the next attempt to use them. The fix allows to clean up a microtasks queue and allow Node to process macrotasks as well, allowing the normal operation of other code.This change has almost no effect prior to #204 changes, but after #204 the difference is really noticeable (see screenshots under the spoilers). Memory consumption and sockets usage remains basically the same, but event loop now is executed much more evenly, without long delays (which is also reflected on CPU graph):
Before
After
Also, another (smaller) improvement is increasing connection pool when CloudFetch is enabled, which allows to download data a bit faster.