sindresorhus / ky

🌳 Tiny & elegant JavaScript HTTP client based on the Fetch API
MIT License
11.91k stars 342 forks source link

retry is wrongly counting #528

Closed Kikobeats closed 6 months ago

Kikobeats commented 10 months ago

I wrote a library that can use ky or got as an HTTP client.

I wanted to verify retries are consistent. To be sure about hat, I wrote a little server that counts retries:

let retryCount = -1
let last 

const server = require('http').createServer((req, res) => {
  if (Date.now() - last > 5000) retryCount = -1
  ++retryCount
  res.statusCode = 500
  console.log({ retryCount })
  last = Date.now()
  res.end(String(retryCount))
})

server.listen(4000, () => {
  console.log('> Listening at http://localhost:4000')
})

Then, I setup my code with { retry: 3 }

Using got:

{ retryCount: 0 } // first request
{ retryCount: 1 }
{ retryCount: 2 }
{ retryCount: 3 }

Using ky:

{ retryCount: 0 } // first request
{ retryCount: 1 }
{ retryCount: 2 }

As you can see, ky is counting the first request as retry while retry should be counter after the first request is performed.

I'm opening this issue since it's unsure if that is intentional; My fix was to count +1 in the case of ky: https://github.com/microlinkhq/mql/pull/130/

sindresorhus commented 9 months ago

Thanks for reporting. This is indeed a bug.

Failing test: https://github.com/sindresorhus/ky/commit/577dd4c5e36f214930a22247523a73d37847c335