[client-sync] Refresh SMTP client when auth credentials change

Summary:
Previously, we would create a nodemailer SMTP transport object when the
sync worker booted up. The transport object would be passed the account
SMTP credentials at the time of object creation. If the Google auth
token later expired, we would continue to try to send mail using the
expired token, resulting in "Invalid login" failures.

This patch makes it so we refresh the transport object if the auth token
changes, and also turns on SMTP connection pooling to limit simultaneous
SMTP connections (& maybe make sending multiple messages faster).

Fixes T7891

Test Plan: manual

Reviewers: juan, halla

Reviewed By: juan, halla

Subscribers: mark

Maniphest Tasks: T7891

Differential Revision: https://phab.nylas.com/D3997
This commit is contained in:
Christine Spang 2017-02-21 16:15:07 -08:00
parent ee5abf22eb
commit 84a3b20839
2 changed files with 10 additions and 5 deletions

View file

@ -29,13 +29,13 @@ class SyncWorker {
this._db = db;
this._manager = parentManager;
this._conn = null;
this._smtp = null;
this._account = account;
this._currentTask = null
this._mailListenerConn = null
this._interruptible = new Interruptible()
this._localDeltas = new LocalSyncDeltaEmitter(db, account.id)
this._logger = global.Logger.forAccount(account)
this._smtp = new SendmailClient(this._account, this._logger)
this._startTime = Date.now()
this._lastSyncTime = null
@ -219,7 +219,7 @@ class SyncWorker {
async _ensureConnection() {
const newCredentials = await this._ensureAccessToken()
if (!newCredentials && this._conn) {
if (!newCredentials && this._conn && this._smtp) {
// We already have a connection and we don't need to update the
// credentials
return this._conn.connect();
@ -235,9 +235,14 @@ class SyncWorker {
if (!settings || !settings.imap_host) {
throw new Error("_ensureConnection: There are no IMAP connection settings for this account.");
}
if (!credentials) {
throw new Error("_ensureConnection: There are no IMAP connection credentials for this account.");
if (!settings.smtp_host && !settings.smtp_custom_config) {
throw new Error("_ensureConnection: There are no SMTP connection settings for this account.");
}
if (!credentials) {
throw new Error("_ensureConnection: There are no IMAP/SMTP connection credentials for this account.");
}
this._smtp = new SendmailClient(this._account, this._logger)
this._socketTimeout = this._getIMAPSocketTimeout()
const conn = new IMAPConnection({

View file

@ -13,7 +13,7 @@ const formatParticipants = (participants) => {
class SendmailClient {
constructor(account, logger) {
this._transporter = nodemailer.createTransport(account.smtpConfig());
this._transporter = nodemailer.createTransport(Object.assign(account.smtpConfig(), {pool: true}));
this._logger = logger;
}