Skip to content

Commit

Permalink
fix(retryagent): On request fail retry ZMS-191 (#48)
Browse files Browse the repository at this point in the history
* retry zilter request on errors. Update config

* fix keepAliveMaxTimeout default value

* refactor, make code cleaner
  • Loading branch information
NickOvt authored Jan 3, 2025
1 parent da54b4e commit 001c19a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
22 changes: 15 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const undici = require('undici');
const libmime = require('libmime');
const { request, RetryAgent, Agent } = require('undici');
const { decodeWords } = require('libmime');
const { toUnicode } = require('punycode');
const { randomBytes } = require('node:crypto');

Expand All @@ -16,7 +16,7 @@ function decodeHeaderLineIntoKeyValuePair(headerLine) {
let headerValue = headerLine.substring(headerSeparatorPos + 1);

try {
decodedHeaderStr = libmime.decodeWords(headerValue);
decodedHeaderStr = decodeWords(headerValue);
} catch (err) {
// keep the value as is
decodedHeaderStr = headerValue;
Expand Down Expand Up @@ -232,17 +232,25 @@ module.exports.init = async app => {

// Call Zilter with required params
try {
const res = await undici.request(zilterUrl, {
dispatcher: undici.getGlobalDispatcher(),
// Create Undici RetryAgent to retry requests on common errors
const { keepAliveTimeout, keepAliveMaxTimeout, maxRetries, minRetryTimeout, maxRetryTimeout, timeoutFactor } = app.config;
const agent = new RetryAgent(new Agent({ keepAliveTimeout: keepAliveTimeout || 5000, keepAliveMaxTimeout: keepAliveMaxTimeout || 600e3 }), {
maxRetries: maxRetries || 3,
minTimeout: minRetryTimeout || 100,
maxTimeout: maxRetryTimeout || 300,
timeoutFactor: timeoutFactor || 1.5
});
const res = await request(zilterUrl, {
dispatcher: agent, // use RetryAgent so in case of request fail - retry
method: 'POST',
body: JSON.stringify({
host: originhost, // Originhost is a string that includes [] (array as a string literal)
'zilter-id': zilterId, // Random ID
sender, // Sender User ID (uid) in the system
helo: transhost, // Transhost is a string that includes [] (array as a string literal)
'authenticated-sender': authenticatedUserAddress || authenticatedUser, // Sender user email
'queue-id': envelope.id,
'rfc822-size': messageSize,
'queue-id': envelope.id, // Queue ID of the envelope of the message
'rfc822-size': messageSize, // Size of the raw RFC822-compatible e-mail
from: envelope.from,
rcpt: envelope.to,
headers: messageHeadersList
Expand Down
8 changes: 7 additions & 1 deletion wildduck-zonemta-zilter.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ enabled = "receiver"
userName = ""
apiKey = ""
serverHost = ""
zilterUrl = "" # full zilter url with https
zilterUrl = "" # full zilter url with https
logIncomingData = false
subjectMaxLength = 16000
keepAliveTimeout = 5000
keepAliveMaxTimeout = 600e3
maxRetries = 3
minRetryTimeout = 100
maxRetryTimeout = 300
timeoutFactor = 1.5

0 comments on commit 001c19a

Please sign in to comment.