Skip to content

Commit

Permalink
Merge pull request #210 from ryanccn/better-concurrency
Browse files Browse the repository at this point in the history
feat: apply concurrency to individual sharp instances
  • Loading branch information
zachleat authored Apr 19, 2024
2 parents d9723c5 + fd21db9 commit b46f0a4
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions img.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,18 +619,21 @@ class Image {
// Should never write when dryRun is true
outputFilePromises.push(
fsp.mkdir(path.dirname(stat.outputPath), { recursive: true })
.then(() => sharpInstance.toFile(stat.outputPath))
.then(() => processingQueue.add(async () => sharpInstance.toFile(stat.outputPath)))
.then(info => {
stat.size = info.size;
return stat;
})
);
} else {
outputFilePromises.push(sharpInstance.toBuffer({ resolveWithObject: true }).then(({ data, info }) => {
stat.buffer = data;
stat.size = info.size;
return stat;
}));
outputFilePromises.push(
processingQueue.add(async () => sharpInstance.toBuffer({ resolveWithObject: true }))
.then(({ data, info }) => {
stat.buffer = data;
stat.size = info.size;
return stat;
})
);
}
}

Expand Down Expand Up @@ -732,7 +735,7 @@ function queueImage(src, opts) {

debug("In-memory cache miss for %o, options: %o", src, opts);

let promise = processingQueue.add(async () => {
let promise = (async () => {
if(typeof src === "string" && opts && opts.statsOnly) {
if(Util.isRemoteUrl(src)) {
if(!opts.remoteImageMetadata || !opts.remoteImageMetadata.width || !opts.remoteImageMetadata.height) {
Expand All @@ -756,7 +759,7 @@ function queueImage(src, opts) {

let input = await img.getInput();
return img.resize(input);
});
})();

if(img.options.useCache) {
memCache.add(key, promise);
Expand Down

0 comments on commit b46f0a4

Please sign in to comment.