nodemailer-smtp-pool
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -75,2 +75,10 @@ 'use strict'; | ||
this._connectionCounter = 0; | ||
this.idling = true; | ||
setImmediate(function () { | ||
if (this.idling) { | ||
this.emit('idle'); | ||
} | ||
}.bind(this)); | ||
} | ||
@@ -86,2 +94,6 @@ util.inherits(SMTPPool, EventEmitter); | ||
SMTPPool.prototype.send = function (mail, callback) { | ||
if (this._closed) { | ||
return false; | ||
} | ||
this._queue.push({ | ||
@@ -91,3 +103,10 @@ mail: mail, | ||
}); | ||
this._processMessages(); | ||
if (this.idling && this._queue.length >= this.options.maxConnections) { | ||
this.idling = false; | ||
} | ||
setImmediate(this._processMessages.bind(this)); | ||
return true; | ||
}; | ||
@@ -119,2 +138,20 @@ | ||
} | ||
// make sure that entire queue would be cleaned | ||
var invokeCallbacks = function () { | ||
if (!this._queue.length) { | ||
this.logger.debug('Pending queue elements cleared'); | ||
return; | ||
} | ||
var element = this._queue.shift(); | ||
if (element && typeof element.callback === 'function') { | ||
try { | ||
element.callback(new Error('Connection pool was closed')); | ||
} catch (E) { | ||
this.logger.error('Callback error for #%s: %s', connection.id, E.message); | ||
} | ||
} | ||
setImmediate(invokeCallbacks); | ||
}.bind(this); | ||
setImmediate(invokeCallbacks); | ||
}; | ||
@@ -128,9 +165,21 @@ | ||
var connection; | ||
var i, len; | ||
if (!this._queue.length || this._closed) { | ||
// do nothing if already closed | ||
if (this._closed) { | ||
return; | ||
} | ||
// do nothing if queue is empty | ||
if (!this._queue.length) { | ||
if (!this.idling) { | ||
// no pending jobs | ||
this.idling = true; | ||
this.emit('idle'); | ||
} | ||
return; | ||
} | ||
// find first available connection | ||
for (var i = 0, len = this._connections.length; i < len; i++) { | ||
for (i = 0, len = this._connections.length; i < len; i++) { | ||
if (this._connections[i].available) { | ||
@@ -147,5 +196,13 @@ connection = this._connections[i]; | ||
if (!connection) { | ||
// no more free connection slots available | ||
this.idling = false; | ||
return; | ||
} | ||
// check if there is free space in the processing queue | ||
if (!this.idling && this._queue.length < this.options.maxConnections) { | ||
this.idling = true; | ||
this.emit('idle'); | ||
} | ||
var element = connection.queueElement = this._queue.shift(); | ||
@@ -168,6 +225,10 @@ element.messageId = (connection.queueElement.mail.message.getHeader('message-id') || '').replace(/[<>\s]/g, ''); | ||
if (element === connection.queueElement) { | ||
element.callback(err, info); | ||
try { | ||
element.callback(err, info); | ||
} catch (E) { | ||
this.logger.error('Callback error for #%s: %s', connection.id, E.message); | ||
} | ||
connection.queueElement = false; | ||
} | ||
}); | ||
}.bind(this)); | ||
}; | ||
@@ -207,3 +268,7 @@ | ||
if (connection.queueElement) { | ||
connection.queueElement.callback(err); | ||
try { | ||
connection.queueElement.callback(err); | ||
} catch (E) { | ||
this.logger.error('Callback error for #%s: %s', connection.id, E.message); | ||
} | ||
connection.queueElement = false; | ||
@@ -310,1 +375,8 @@ } | ||
}; | ||
/** | ||
* Returns true if there are free slots in the queue | ||
*/ | ||
SMTPPool.prototype.isIdle = function () { | ||
return this.idling; | ||
}; |
{ | ||
"name": "nodemailer-smtp-pool", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "SMTP transport for Nodemailer", | ||
@@ -5,0 +5,0 @@ "main": "lib/smtp-pool.js", |
@@ -76,2 +76,22 @@ # SMTP transport module for Nodemailer | ||
## Events | ||
The following events are emitted by this transport | ||
### Event: 'idle' | ||
Emitted if there are free slots in the connection pool. | ||
Check with `.isIdle()` method if these free slots are still available. | ||
Using this method makes sense if you maintain your own queue (for example pull from some queue service). | ||
```javascript | ||
var messages = [...'list of messages']; | ||
transporter.on('idle', function(){ | ||
// send next messages from the pending queue | ||
while(transporter.isIdle() && messages.length){ | ||
transporter.send(messages.shift()); | ||
} | ||
}); | ||
``` | ||
## Authentication | ||
@@ -78,0 +98,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25127
523
169