I'm using imagemin-mozjpeg which uses mozjpeg binary to compress images.
The problem is that I'm using it in a nodejs webserver.
This is how it works now:
I'm uploading a JPEG image using "request" module (fs.createReadStream).
Multer process the stream the stream and keep it in buffer (memory storage).
Then pass the buffer to the imagemin to compress.
The compressed buffer is then written to a file. (example.jpg)
Everything works.
The problem here is that for each request, a new child process of mozjpeg binary is spawned, cjpeg.
1 child process is consuming 12.5 MB of memory (for a .5 MB file).
If I've 50 requests at the same time, that goes to ~700 MB because for 50 images there are 50 child processes.
Is there a way that I can limit the the number of child processes? (the library is using "execa" module) or just spawn 4-5 child process and they do the compression for all the requests.
Thanks
if (req.files.myimage[0].buffer) {
let fileNumber = filesUploaded++;
imagemin.buffer(req.files.myimage[0].buffer, {
plugins: [
imageminMozjpeg({ quality: 60, progressive: true })
]
})
.then(file => {
fs.writeFile(__dirname + '/uploads/' + uuidv4() + '.jpg', file, 'hex' , function () {
res.end("SUCCESS" + fileNumber.toString());
});
})
.catch(err => {
console.log(err);
res.end("FAILED");
});
}