Question

I have a rather simple Node.JS application that uses subscribe to redis to receive messages which it then sends out to the connected clients. In addition I also use the subscribe method to pass Node.JS the command to send out emails to our users.

All works well but the memory usage grows daily until it reaches 1.1GB (using command top on linux) and then all further email sending fails with the error message:

{ [Error: spawn ENOMEM] code: 'ENOMEM', errno: 'ENOMEM', syscall: 'spawn' }

Where could that come from? The source code is rather simple:

Clients connecting:

io.sockets.on('connection', function (socket) {
    nClients++;
    console.log("Number of clients connected " + nClients);
    socket.on('disconnect', function () {
        nClients--;
        console.log("Number of clients remaining " + nClients);
    });
});

Receiving messages to send out to the clients

cli_sub.on("message",function(channel,message) {
    if(nUseDelay==1) {
        oo = JSON.parse(message);
        ablv_last_message[oo[0]["base"]+"_"+oo[0]["alt"]] = message;
    } else {
        io.sockets.emit('ablv', message);
    }
});

Sending out emails to clients

cli_email.on("message",function(channel,message) {
    var transport = nodemailer.createTransport("sendmail");

    oo = JSON.parse(message);

    var mailOptions = {
        from: email_sender,
        bcc: oo["bcc"],
        to: oo["recipient"],
        subject: oo["subject"],
        html: oo["text"]
    } 
    try {
        transport.sendMail(mailOptions);
    } catch(err) {
        console.log(err);
    }
});


setInterval(function() {
        Object.keys(ablv_last_message).forEach( function(key) {
        io.sockets.emit('ablv', ablv_last_message[key]);
    });
    ask_last_message = [];
}, 5000);
Was it helpful?

Solution

When you call nodemailer.createTransport you're creating a pool of SMTP connections that's intended to be reused for the life of your application.

So change your code to call that once at your app's startup and then reuse the transport object each time you need to send an email.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top