Skip to content

Commit

Permalink
fix(MS365): Wait until responding with a throttling response (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 authored Sep 13, 2023
1 parent 62878e3 commit 09bfb3e
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 130 deletions.
21 changes: 21 additions & 0 deletions lib/imap-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,28 @@ class ImapFlow extends EventEmitter {
err.responseStatus = parsed.command.toUpperCase();
if (txt) {
err.responseText = txt;

// MS365 throttling
// tag BAD Request is throttled. Suggested Backoff Time: 92415 milliseconds
if (/Request is throttled/i.test(txt) && /Backoff Time/i.test(txt)) {
let throttlingMatch = txt.match(/Backoff Time[:=\s]+(\d+)/i);
if (throttlingMatch && throttlingMatch[1] && !isNaN(throttlingMatch[1])) {
err.code = 'ETHROTTLE';
err.throttleReset = Number(throttlingMatch[1]);

let delayResponse = err.throttleReset;
if (delayResponse > 5 * 60 * 1000) {
// max delay cap
delayResponse = 5 * 60 * 1000;
}
if (delayResponse) {
this.log.warn({ msg: 'Throttling detected', err, cid: this.id, delayResponse });
await new Promise(r => setTimeout(r, delayResponse));
}
}
}
}

request.reject(err);
break;
}
Expand Down
Loading

0 comments on commit 09bfb3e

Please sign in to comment.