Having issues with retry interceptor #2963
-
Hello I'm trying to use the new First i created a basic http server to test requests against: import http from "http";
const server = http.createServer((req, res) => {
if (req.url === "/api" && req.method === "GET") {
if (Math.random() > 0.5) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("500 Internal Server Error");
} else {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Hello from the API endpoint!" }));
}
} else {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("404 Not Found");
}
});
const port = 3000;
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
}); Then I tried doing a basic retry like this: import { Client, interceptors } from "undici";
const { retry } = interceptors;
const client = new Client("http://localhost:3000").compose(
retry({
retry: function (err, context, callback) {
console.log("retrying", {
err: err.code,
state: context.state,
opts: context.opts.retryOptions,
});
if (context.state.counter >= context.opts.retryOptions.maxRetries) {
callback(err);
return;
}
setTimeout(() => callback(null), context.opts.retryOptions.timeout);
},
maxRetries: 3,
minTimeout: 400,
maxTimeout: 10000,
})
);
let res;
const start = performance.now();
try {
res = await client.request({
path: "/api",
method: "GET",
throwOnError: true,
});
} catch (error) {
console.log(error.name);
}
const end = performance.now();
res ? console.log(await res.body.json()) : console.log("No response");
console.log(end - start); Then when i try to log out the results i get this weird output
as we can see here it only retried twice and the what am i doing wrong here 🤔 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
👋 Outocme after exhausting a retry counter of 4 Server is running on port 3000
retrying {
err: 'UND_ERR_REQ_RETRY',
state: { counter: 1 },
opts: {
retry: [Function: retry],
retryAfter: true,
maxTimeout: 10000,
timeout: 400,
timeoutFactor: 2,
maxRetries: 4,
methods: [ 'GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE' ],
statusCodes: [ 500, 502, 503, 504, 429 ],
errorCodes: [
'ECONNRESET',
'ECONNREFUSED',
'ENOTFOUND',
'ENETDOWN',
'ENETUNREACH',
'EHOSTDOWN',
'EHOSTUNREACH',
'EPIPE',
'UND_ERR_SOCKET'
]
}
}
retrying {
err: 'UND_ERR_REQ_RETRY',
state: { counter: 2 },
opts: {
retry: [Function: retry],
retryAfter: true,
maxTimeout: 10000,
timeout: 400,
timeoutFactor: 2,
maxRetries: 4,
methods: [ 'GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE' ],
statusCodes: [ 500, 502, 503, 504, 429 ],
errorCodes: [
'ECONNRESET',
'ECONNREFUSED',
'ENOTFOUND',
'ENETDOWN',
'ENETUNREACH',
'EHOSTDOWN',
'EHOSTUNREACH',
'EPIPE',
'UND_ERR_SOCKET'
]
}
}
retrying {
err: 'UND_ERR_REQ_RETRY',
state: { counter: 3 },
opts: {
retry: [Function: retry],
retryAfter: true,
maxTimeout: 10000,
timeout: 400,
timeoutFactor: 2,
maxRetries: 4,
methods: [ 'GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE' ],
statusCodes: [ 500, 502, 503, 504, 429 ],
errorCodes: [
'ECONNRESET',
'ECONNREFUSED',
'ENOTFOUND',
'ENETDOWN',
'ENETUNREACH',
'EHOSTDOWN',
'EHOSTUNREACH',
'EPIPE',
'UND_ERR_SOCKET'
]
}
}
retrying {
err: 'UND_ERR_REQ_RETRY',
state: { counter: 4 },
opts: {
retry: [Function: retry],
retryAfter: true,
maxTimeout: 10000,
timeout: 400,
timeoutFactor: 2,
maxRetries: 4,
methods: [ 'GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE' ],
statusCodes: [ 500, 502, 503, 504, 429 ],
errorCodes: [
'ECONNRESET',
'ECONNREFUSED',
'ENOTFOUND',
'ENETDOWN',
'ENETUNREACH',
'EHOSTDOWN',
'EHOSTUNREACH',
'EPIPE',
'UND_ERR_SOCKET'
]
}
}
RequestRetryError
No response
1230.755166 |
Beta Was this translation helpful? Give feedback.
👋
I tried to reproduce your issue and so far, behavioural speaking is working as expected, though I found the bug around the counter and I'm opening a PR for it.
Outocme after exhausting a retry counter of 4