Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve AWS upload mechanism and allow to override registry timeout settings #516

Merged
merged 1 commit into from
Jun 12, 2017

Conversation

matteofigus
Copy link
Member

@matteofigus matteofigus commented Jun 11, 2017

Fixes #514

This Pull Request aims to address some necessary optimisations to the overall publish strategy, specifically with dealing with big components (including many and/or big files) and possibly slow connections.

In this perspective, here

  1. We change the strategy for uploading to S3 from putObject to upload method. This allows uploads into multiple chunks and is necessary for files that reach big dimensions
  2. We make customisable the default timeout of the API, at the moment set to 2m, so that when a publish takes a long time (because of the many requests to S3) the request is not terminated and handled correctly.

See inline comments for details about the implementation.

@@ -60,7 +60,7 @@
"dependencies": {
"accept-language-parser": "1.4.0",
"async": "2.4.1",
"aws-sdk": "2.62.0",
"aws-sdk": "2.67.0",
Copy link
Member Author

@matteofigus matteofigus Jun 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bug was preventing the upload() S3 function to properly upload all the chunks. Latest version fixes 👌

@@ -50,6 +48,7 @@ module.exports = function(input) {
.map(s => s.toLowerCase());

options.port = process.env.PORT || options.port;
options.timeout = options.timeout || 1000 * 60 * 2;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the timeout for the http server, after which a request gets terminated with a "ECONNRESET".
When trying to upload a big file, I changed this 10m and worked fine. I opted for a 2m default (which is the node default) but left customisable by the user.


callback(null, 'ok');
}
callback
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async.each actually fires the error as soon as it gets the first. This means that the "compact" is not necessary as the error is just 1. When testing locally, I was seeing a error with a [Object object] result here prior of this fix in case of S3 issues.


getClient().putObject(obj, callback);
const upload = getClient().upload(obj);
upload.send(callback);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For large files, the recommended approach is to use upload over putObject. Upload automatically processes the req into multiple chunks and its Body property accepts both string (making this change retrocompatible) and streams (see change in the putFile method).

});
try {
const stream = fs.createReadStream(filePath);
putFileContent(stream, fileName, isPrivate, callback);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we just create a stream reader instead of reading the whole content. For big files, this is actually necessary for not blocking the event loop with large GCs operations.

_.each(defaults, (value, property) => {
it(`should set ${property} to ${JSON.stringify(value)}`, () => {
expect(sanitise(options)[property]).to.eql(value);
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to test timeout returning the default, but I just ended up testing each property of the snapshot. When migrating to jest, this will be so much prettier ❤️

'header1',
'header-two',
'header3'
]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a prettier initiative.

putFileContent(fileContent, fileName, isPrivate, callback);
});
try {
const stream = fs.createReadStream(filePath);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now inside a try/catch because a exception will be fired in case of file not found.

@matteofigus matteofigus changed the title Allow big files uploads Improve AWS upload mechanism and allow to override registry timeout settings Jun 11, 2017
nickbalestra
nickbalestra previously approved these changes Jun 12, 2017
Copy link
Contributor

@nickbalestra nickbalestra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
Nice job. I like the move to upload over putObject

@nickbalestra nickbalestra dismissed their stale review June 12, 2017 13:06

found a little thing to be asked

@nickbalestra nickbalestra merged commit 591a984 into master Jun 12, 2017
@nickbalestra nickbalestra deleted the huge-uploads branch June 12, 2017 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants