From 17c110934fd491ce0ad5016dda6806160ed9b58a Mon Sep 17 00:00:00 2001
From: ryan <ryannganga13325@gmail.com>
Date: Thu, 17 Apr 2025 02:12:56 +0300
Subject: [PATCH] fix(network): set up retries to fix network failure issues

---
 package.json       |  2 +-
 src/formHandler.js | 23 +++++++++++++++++++----
 testDns.js         |  5 +++++
 3 files changed, 25 insertions(+), 5 deletions(-)
 create mode 100644 testDns.js

diff --git a/package.json b/package.json
index f8936e0..acfec99 100644
--- a/package.json
+++ b/package.json
@@ -6,7 +6,7 @@
   "type": "module",
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
-    "start": "node src/server.js"
+    "start": "NODE_OPTIONS='--dns-result-order=ipv4first' node src/server.js"
   },
   "author": "ryan",
   "license": "ISC",
diff --git a/src/formHandler.js b/src/formHandler.js
index 1481118..ea5c110 100644
--- a/src/formHandler.js
+++ b/src/formHandler.js
@@ -25,16 +25,31 @@ export async function submitToListmonk(req, res) {
 
     console.log('Sending request to Listmonk with payload:', payload);
 
+    async function fetchWithRetry(url, options, retries = 3) {
+        for (let i = 0; i < retries; i++) {
+          console.log(`Attempt ${i + 1} sending request to ${url}`);
+          try {
+            const res = await fetch(url, options);
+            if (!res.ok) throw new Error(`HTTP ${res.status}`);
+            return res;
+          } catch (err) {
+            console.log(`Attempt ${i + 1} failed: ${err}`);
+            if (i === retries - 1) throw err;
+            await new Promise(resolve => setTimeout(resolve, 500));
+          }
+        }
+      }
+      
     try {
         // Add subscriber
-        const response = await fetch(listmonkUrl, {
+        const response = await fetchWithRetry(listmonkUrl, {
             method: 'POST',
             headers: {
                 'Content-Type': 'application/json',
                 'Authorization': `Basic ${auth}`
             },
             body: JSON.stringify(payload),
-        });
+        },3);
 
         if (!response.ok) {
             const errorBody = await response.text();
@@ -64,13 +79,13 @@ export async function submitToListmonk(req, res) {
                 },
                 body: JSON.stringify(statusPayload),
             });
-        
+
             if (!statusResponse.ok) {
                 const errorBody = await statusResponse.text();
                 console.error('Response from Listmonk:', errorBody);
                 throw new Error(`Listmonk API error (Update Campaign Status): ${statusResponse.statusText}. Response body: ${errorBody}`);
             }
-        
+
             const statusResult = await statusResponse.json();
             console.log('Campaign status updated successfully:', statusResult);
         } catch (error) {
diff --git a/testDns.js b/testDns.js
new file mode 100644
index 0000000..0da14b0
--- /dev/null
+++ b/testDns.js
@@ -0,0 +1,5 @@
+import dns from 'dns';
+
+dns.lookup('mailer.melonion.me', { all: true }, (err, addresses) => {
+  console.log(addresses);
+});