fix(network): set up retries to fix network failure issues
This commit is contained in:
parent
b01a40a1fd
commit
17c110934f
3 changed files with 25 additions and 5 deletions
|
@ -6,7 +6,7 @@
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"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",
|
"author": "ryan",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
|
|
|
@ -25,16 +25,31 @@ export async function submitToListmonk(req, res) {
|
||||||
|
|
||||||
console.log('Sending request to Listmonk with payload:', payload);
|
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 {
|
try {
|
||||||
// Add subscriber
|
// Add subscriber
|
||||||
const response = await fetch(listmonkUrl, {
|
const response = await fetchWithRetry(listmonkUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': `Basic ${auth}`
|
'Authorization': `Basic ${auth}`
|
||||||
},
|
},
|
||||||
body: JSON.stringify(payload),
|
body: JSON.stringify(payload),
|
||||||
});
|
},3);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorBody = await response.text();
|
const errorBody = await response.text();
|
||||||
|
|
5
testDns.js
Normal file
5
testDns.js
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import dns from 'dns';
|
||||||
|
|
||||||
|
dns.lookup('mailer.melonion.me', { all: true }, (err, addresses) => {
|
||||||
|
console.log(addresses);
|
||||||
|
});
|
Loading…
Add table
Reference in a new issue