Skip to content

Commit

Permalink
⏰ Call getImageSize with timeout (#8044) (#8189)
Browse files Browse the repository at this point in the history
refs #8041

Calls `getImageSize` with an timeout of 6sec. and adds a default timeout (in case, function is called without optional timeout) of 10sec, to prevent node from using its default timeout of 2minutes 😱
  • Loading branch information
kirrg001 authored and ErisDS committed Mar 20, 2017
1 parent b24b196 commit 4e3e1bd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
16 changes: 8 additions & 8 deletions core/server/utils/cached-image-size-from-url.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var imageSizeCache = {},
size = require('./image-size-from-url'),
Promise = require('bluebird'),
getImageSizeFromUrl = size.getImageSizeFromUrl;
var Promise = require('bluebird'),
size = require('./image-size-from-url'),
logging = require('../logging'),
getImageSizeFromUrl = size.getImageSizeFromUrl,
imageSizeCache = {};

/**
* Get cached image size from URL
Expand All @@ -18,13 +19,12 @@ function getCachedImageSizeFromUrl(url) {

// image size is not in cache
if (!imageSizeCache[url]) {
return getImageSizeFromUrl(url).then(function (res) {
return getImageSizeFromUrl(url, 6000).then(function (res) {
imageSizeCache[url] = res;

return Promise.resolve(imageSizeCache[url]);
}).catch(function () {
// @ToDo: add real error handling here as soon as we have error logging
// logger.error({err:err});
}).catch(function (err) {
logging.error(err);

// in case of error we just attach the url
return Promise.resolve(imageSizeCache[url] = url);
Expand Down
31 changes: 19 additions & 12 deletions core/server/utils/image-size-from-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var sizeOf = require('image-size'),
http = require('http'),
https = require('https'),
utils = require('../utils'),
errors = require('../errors'),
dimensions,
request,
requestHandler;
Expand All @@ -35,6 +36,9 @@ module.exports.getImageSizeFromUrl = function getImageSizeFromUrl(imagePath, tim
var imageObject = {},
options;

// set default timeout if called without option. Otherwise node will use default timeout of 120 sec.
timeout = timeout ? timeout : 10000;

imageObject.url = imagePath;

// check if we got an url without any protocol
Expand Down Expand Up @@ -71,30 +75,33 @@ module.exports.getImageSizeFromUrl = function getImageSizeFromUrl(imagePath, tim

return resolve(imageObject);
} catch (err) {
// @ToDo: add real error handling here as soon as we have error logging
return reject(err);
return reject(new errors.InternalServerError({
code: 'IMAGE_SIZE',
err: err,
context: imagePath
}));
}
} else {
// @ToDo: add real error handling here as soon as we have error logging
var err = new Error();
err.message = imagePath;
err.statusCode = res.statusCode;

return reject(err);
return reject(new errors.InternalServerError({
code: 'IMAGE_SIZE',
statusCode: res.statusCode,
context: imagePath
}));
}
});
}).on('socket', function (socket) {
// don't set timeout if no timeout give as argument
if (timeout) {
socket.setTimeout(timeout);
socket.on('timeout', function () {
request.abort();
});
}
}).on('error', function (err) {
// @ToDo: add real error handling here as soon as we have error logging

return reject(err);
return reject(new errors.InternalServerError({
code: 'IMAGE_SIZE',
err: err,
context: imagePath
}));
});
});
};

0 comments on commit 4e3e1bd

Please sign in to comment.