Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly throttle message sending with nodemailer SES transport?

The nodemailer documentation says:

If you use rate or connection limiting then you can also use helper methods to detect if the sending queue is full or not. This would help to avoid buffering up too many messages.

It also provides an example:

let transporter = nodemailer.createTransport({
    SES: new aws.SES({
        apiVersion: '2010-12-01'
    }),
    sendingRate: 1 // max 1 messages/second
});

// Push next messages to Nodemailer
transporter.on('idle', () => {
    while (transporter.isIdle()) {
        transporter.sendMail(...);
    }
});

Unfortunately, this is rather cryptic to me. Does sendingRate: 1 only provides a helper, or does it handle throttling ?

Also this piece of code looks to me like it would loop infinitely as soon as sendMail(...) is executed. Am I missing something here ?

Is there any example or recommendation on how to use this feature ?

Thanks a lot !

like image 749
aherve Avatar asked Oct 17 '22 00:10

aherve


2 Answers

From the documentation:

SES can tolerate short spikes but you can’t really flush all your emails at once and expect these to be delivered. To overcome this you can set a rate limiting value and let Nodemailer handle everything – if too many messages are being delivered then Nodemailer buffers these until there is an opportunity to do the actual delivery.

I don't think listening for the idle event is mandatory, it's only needed if you want to avoid Nodemailer buffering messages. I have an SES send rate of 15 messages per second and regularly throw 250 emails at once at Nodemailer and don't hit any throttling issues.

like image 176
Dougc Avatar answered Nov 01 '22 12:11

Dougc


You are right, the while loop only appears to be there for testing sending rate. Once you remove the while loop the code in documentation should work fine.

transporter.on('idle', () => {
    transporter.sendMail(...);
});
like image 35
Sunny Avatar answered Nov 01 '22 14:11

Sunny