got: Requests with timeout always takes longer than 6 seconds

EXPECTED

  • Set timeout: 100 in option, the request should failed no too much longer than 100ms 300ms (with 2 retries by default)

CURRENT BEHAVIOR

  • It takes longer than 6s to failed on ‘ETIMEOUT’

REPRODUCE

https://runkit.com/amio/5b5873387b9d1e0011ef8f77

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 23 (6 by maintainers)

Most upvoted comments

@amio Sorry for late response, but I just recalled this:

The retry function delays the next request by ((2 ** (iteration - 1)) * 1000).

@sindresorhus What about making a retry.delay option?

{
	retry: {
		retries: 2,
		delay: iteration => Math.random() * 100
	}
}

@sindresorhus Maybe we should expose the retry delay?

If I’ve understand the code correctly this happens because of how the default retry delay is calculated: https://github.com/sindresorhus/got/blob/master/source/normalize-arguments.js#L259.

So your request times out after 300ms, then it waits ((1 << 1) * 1000) + Math.random() * 100; so ≃ 2000ms, then it times out after 300ms and then waits ((1 << 2) * 1000) + Math.random() * 100; which is ≃ 4000ms. In total around 6600ms.

If you want a lower wait before it retries you would have to send in a function to the retries argument.

@szmarczak I guess it could be useful, but then we need to clearly document that the response may be empty depending on when the timeout occurred.

@szmarczak Sure, I’ll take on this later 😄