Skip to content

Commit

Permalink
fix: fixed retry client (for sitemap), fixed bounces on timeout and r…
Browse files Browse the repository at this point in the history
…etries
  • Loading branch information
titanism committed Sep 22, 2023
1 parent facf8e8 commit c2603d2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
9 changes: 8 additions & 1 deletion helpers/process-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const isCodeBug = require('./is-code-bug');
const logger = require('./logger');
const parseRootDomain = require('./parse-root-domain');
const { decrypt } = require('./encrypt-decrypt');
const isRetryableError = require('./is-retryable-error');
const isSSLError = require('./is-ssl-error');
const isTLSError = require('./is-tls-error');

const config = require('#config');
const env = require('#config/env');
Expand Down Expand Up @@ -962,7 +965,11 @@ async function processEmail({
isEmail(error.recipient, { ignore_max_length: true }) &&
!error.is_recently_blocked &&
!email.soft_bounces.includes(error.recipient) &&
!email.hard_bounces.includes(error.recipient)
!email.hard_bounces.includes(error.recipient) &&
!isRetryableError(error) &&
!isSSLError(error) &&
!isTLSError(error) &&
!isCodeBug(error)
);

if (!email.is_bounce && filteredErrors.length > 0) {
Expand Down
16 changes: 10 additions & 6 deletions helpers/retry-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ class RetryClient extends undici.Client {
constructor(opts) {
super(opts);

opts.timeout = opts.timeout || ms('30s');
opts.retries = opts.retries || 3;
const timeout =
typeof opts?.timeout === 'number' ? opts.timeout : ms('30s');
const retries = typeof opts?.retries === 'number' ? opts.retries : 3;

// exponential retry backoff (2, 4, 8)
opts.calculateDelay = (count) => Math.round(1000 * 2 ** count);
const calculateDelay =
typeof opts?.calculateDelay === 'function'
? opts.calculateDelay
: (count) => Math.round(1000 * 2 ** count);

this._request = this.request;

this.request = async (options, count = 1) => {
try {
options.signal = AbortSignal.timeout(opts.timeout);
options.signal = AbortSignal.timeout(timeout);
options.throwOnError = true;
const response = await this._request(options);
// the error code is between 200-400 (e.g. 302 redirect)
Expand All @@ -37,8 +41,8 @@ class RetryClient extends undici.Client {

return response;
} catch (err) {
if (count >= opts.retries || !isRetryableError(err)) throw err;
const ms = opts.calculateDelay(count);
if (count >= retries || !isRetryableError(err)) throw err;
const ms = calculateDelay(count);
if (ms) await delay(ms);
return this.request(options, count + 1);
}
Expand Down

0 comments on commit c2603d2

Please sign in to comment.