Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout feature #21

Closed
birdlavv opened this issue Oct 23, 2021 · 10 comments
Closed

Timeout feature #21

birdlavv opened this issue Oct 23, 2021 · 10 comments
Labels
enhancement New feature or request

Comments

@birdlavv
Copy link

Timeout support (limiting the request execution time) would allow for snappy client-facing network timeout error messages if network connectivity drops.

Copy link
Member

atinux commented Oct 25, 2021

Copy link
Member

atinux commented Oct 25, 2021

@atinux atinux added the enhancement New feature or request label Oct 25, 2021 — with Volta.net
@nathanchase
Copy link

nathanchase commented Jul 27, 2022

Would very much like to see timeouts implemented - preferably with an exponential increase in timeout duration on subsequent retries. This is a much needed and essential feature coming from relying on it with axios.

Can we hook into the signal: AbortSignal.timeout(8000) mentioned here: whatwg/fetch#951 (comment)?

Something like:

$fetch(event.req.url, {
    baseURL: config.public.baseUrl,
    params,
    method,
    body,
    retry: 10,
    timeout: 2000, // signal: AbortSignal.timeout(2000),
    ...

@oleynikd
Copy link

retryTimeout would be great too

@rmcmk
Copy link

rmcmk commented Apr 26, 2023

+1, would be a very useful feature to have. Currently have to hack around to get this to work in nuxt

@gustavopch
Copy link

gustavopch commented Apr 30, 2023

It could be implemented using https://npm.im/async-retry or adapted directly from the source code of https://github.com/tim-kos/node-retry to avoid adding a dependency.

EDIT: or perhaps just:

-  function onError(context: FetchContext): Promise<FetchResponse<any>> {
+  async function onError(context: FetchContext): Promise<FetchResponse<any>> {
     // Is Abort
     // If it is an active abort, it will not retry automatically.
     // https://developer.mozilla.org/en-US/docs/Web/API/DOMException#error_names
     const isAbort =
       (context.error && context.error.name === "AbortError") || false;
     // Retry
     if (context.options.retry !== false && !isAbort) {
       let retries;
       if (typeof context.options.retry === "number") {
         retries = context.options.retry;
       } else {
         retries = isPayloadMethod(context.options.method) ? 0 : 1;
       }
 
       const responseCode = (context.response && context.response.status) || 500;
       if (retries > 0 && retryStatusCodes.has(responseCode)) {
+        context.options.timeout ??= 1000;
+        await new Promise(resolve => setTimeout(resolve, context.options.timeout));
+        context.options.timeout *= 2;
         return $fetchRaw(context.request, {
           ...context.options,
           retry: retries - 1,
         });
       }
     }

@Siddharth24Khera
Copy link

+1, would be a very useful feature to have. Currently have to hack around to get this to work in nuxt

@rmcmk Can you share the workaround you are using in Nuxt.

@danielcmm
Copy link

Very much needed!

@seth100
Copy link

seth100 commented Jul 15, 2023

Looking forward to having this implemented !

@pi0
Copy link
Member

pi0 commented Aug 22, 2023

implemented in #268 (finally!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

10 participants