Fixed throttling and pausing #243
This commit is contained in:
parent
a7b2c33b30
commit
cff908887f
3 changed files with 117 additions and 83 deletions
15
lib/db.js
15
lib/db.js
|
@ -4,6 +4,7 @@ let config = require('config');
|
||||||
let mysql = require('mysql');
|
let mysql = require('mysql');
|
||||||
let redis = require('redis');
|
let redis = require('redis');
|
||||||
let Lock = require('redfour');
|
let Lock = require('redfour');
|
||||||
|
let tools = require('./tools');
|
||||||
|
|
||||||
module.exports = mysql.createPool(config.mysql);
|
module.exports = mysql.createPool(config.mysql);
|
||||||
if (config.redis && config.redis.enabled) {
|
if (config.redis && config.redis.enabled) {
|
||||||
|
@ -33,7 +34,7 @@ if (config.redis && config.redis.enabled) {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.clearCache = (key, callback) => {
|
module.exports.clearCache = (key, callback) => {
|
||||||
module.exports.redis.del(key, err => callback(err));
|
module.exports.redis.del('mailtrain:cache:' + key, err => callback(err));
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.addToCache = (key, value, callback) => {
|
module.exports.addToCache = (key, value, callback) => {
|
||||||
|
@ -76,9 +77,21 @@ if (config.redis && config.redis.enabled) {
|
||||||
|
|
||||||
module.exports.clearCache = (key, callback) => {
|
module.exports.clearCache = (key, callback) => {
|
||||||
caches.delete(key);
|
caches.delete(key);
|
||||||
|
tools.workers.forEach(child => {
|
||||||
|
child.send({
|
||||||
|
cmd: 'db.clearCache',
|
||||||
|
key
|
||||||
|
});
|
||||||
|
});
|
||||||
setImmediate(() => callback());
|
setImmediate(() => callback());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
process.on('message', m => {
|
||||||
|
if (m && m.cmd === 'db.clearCache' && m.key) {
|
||||||
|
caches.delete(m.key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
module.exports.addToCache = (key, value, callback) => {
|
module.exports.addToCache = (key, value, callback) => {
|
||||||
if (!caches.has(key)) {
|
if (!caches.has(key)) {
|
||||||
caches.set(key, []);
|
caches.set(key, []);
|
||||||
|
|
|
@ -182,10 +182,10 @@ function createMailer(callback) {
|
||||||
module.exports.transport.checkThrottling = null;
|
module.exports.transport.checkThrottling = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
let throttling = Number(configItems.smtpThrottling) || 0;
|
let sendingRate = Number(configItems.smtpThrottling) || 0;
|
||||||
if (throttling) {
|
if (sendingRate) {
|
||||||
// convert to messages/second
|
// convert to messages/second
|
||||||
throttling = 1 / (throttling / (3600 * 1000));
|
sendingRate = sendingRate / 3600;
|
||||||
}
|
}
|
||||||
|
|
||||||
let transportOptions;
|
let transportOptions;
|
||||||
|
@ -236,7 +236,7 @@ function createMailer(callback) {
|
||||||
error: logfunc.bind(null, 'error')
|
error: logfunc.bind(null, 'error')
|
||||||
},
|
},
|
||||||
maxConnections: Number(configItems.smtpMaxConnections),
|
maxConnections: Number(configItems.smtpMaxConnections),
|
||||||
sendingRate: throttling,
|
sendingRate,
|
||||||
tls: {
|
tls: {
|
||||||
rejectUnauthorized: !configItems.smtpSelfSigned
|
rejectUnauthorized: !configItems.smtpSelfSigned
|
||||||
}
|
}
|
||||||
|
@ -257,19 +257,30 @@ function createMailer(callback) {
|
||||||
oldListeners.forEach(listener => module.exports.transport.on('idle', listener));
|
oldListeners.forEach(listener => module.exports.transport.on('idle', listener));
|
||||||
}
|
}
|
||||||
|
|
||||||
let lastCheck = Date.now();
|
|
||||||
if (configItems.mailTransport === 'smtp' || !configItems.mailTransport) {
|
if (configItems.mailTransport === 'smtp' || !configItems.mailTransport) {
|
||||||
|
|
||||||
|
let throttling = Number(configItems.smtpThrottling) || 0;
|
||||||
|
if (throttling) {
|
||||||
|
throttling = 1 / (throttling / (3600 * 1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
let lastCheck = Date.now();
|
||||||
|
|
||||||
module.exports.transport.checkThrottling = function (next) {
|
module.exports.transport.checkThrottling = function (next) {
|
||||||
if (!throttling) {
|
if (!throttling) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
let nextCheck = Date.now();
|
let nextCheck = Date.now();
|
||||||
let checkDiff = (nextCheck - lastCheck);
|
let checkDiff = (nextCheck - lastCheck);
|
||||||
lastCheck = nextCheck;
|
|
||||||
if (checkDiff < throttling) {
|
if (checkDiff < throttling) {
|
||||||
log.verbose('Mail', 'Throttling next message in %s sec.', (throttling - checkDiff) / 1000);
|
log.verbose('Mail', 'Throttling next message in %s sec.', (throttling - checkDiff) / 1000);
|
||||||
setTimeout(next, throttling - checkDiff);
|
setTimeout(() => {
|
||||||
|
lastCheck = Date.now();
|
||||||
|
next();
|
||||||
|
}, throttling - checkDiff);
|
||||||
} else {
|
} else {
|
||||||
|
lastCheck = nextCheck;
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -466,12 +466,20 @@ let sendLoop = () => {
|
||||||
return setTimeout(sendLoop, 10 * 1000);
|
return setTimeout(sendLoop, 10 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let isThrottled = false;
|
||||||
|
|
||||||
let getNext = () => {
|
let getNext = () => {
|
||||||
if (!mailer.transport.isIdle()) {
|
if (!mailer.transport.isIdle() || isThrottled) {
|
||||||
// only retrieve new messages if there are free slots in the mailer queue
|
// only retrieve new messages if there are free slots in the mailer queue
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isThrottled = true;
|
||||||
|
|
||||||
|
mailer.transport.checkThrottling(() => {
|
||||||
|
|
||||||
|
isThrottled = false;
|
||||||
|
|
||||||
// find an unsent message
|
// find an unsent message
|
||||||
findUnsent((err, message) => {
|
findUnsent((err, message) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -484,7 +492,7 @@ let sendLoop = () => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//log.verbose('Mail', 'Found new message to be delivered: %s', message.subscription.cid);
|
// log.verbose('Mail', 'Found new message to be delivered: %s', message.subscription.cid);
|
||||||
// format message to nodemailer message format
|
// format message to nodemailer message format
|
||||||
formatMessage(message, (err, mail) => {
|
formatMessage(message, (err, mail) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -499,6 +507,7 @@ let sendLoop = () => {
|
||||||
setTimeout(getNext, mailing_timeout);
|
setTimeout(getNext, mailing_timeout);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!blacklisted) {
|
if (!blacklisted) {
|
||||||
let tryCount = 0;
|
let tryCount = 0;
|
||||||
let trySend = () => {
|
let trySend = () => {
|
||||||
|
@ -571,14 +580,15 @@ let sendLoop = () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
setImmediate(() => mailer.transport.checkThrottling(getNext));
|
setImmediate(getNext);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
mailer.transport.on('idle', () => mailer.transport.checkThrottling(getNext));
|
mailer.transport.on('idle', getNext);
|
||||||
setImmediate(() => mailer.transport.checkThrottling(getNext));
|
setImmediate(getNext);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue