Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/api/RetryHandler.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Extends: [`Dispatch.DispatchOptions`](Dispatcher.md#parameter-dispatchoptions).
- **methods** `string[]` (optional) - Array of HTTP methods to retry. Default: `['GET', 'PUT', 'HEAD', 'OPTIONS', 'DELETE']`
- **statusCodes** `number[]` (optional) - Array of HTTP status codes to retry. Default: `[429, 500, 502, 503, 504]`
- **errorCodes** `string[]` (optional) - Array of Error codes to retry. Default: `['ECONNRESET', 'ECONNREFUSED', 'ENOTFOUND', 'ENETDOWN','ENETUNREACH', 'EHOSTDOWN', 'UND_ERR_SOCKET']`
- **ifMatch** `boolean` (optional) - It enables the send of `if-match` pre-condition header if `etag` is present in the first response. Default: `true`

**`RetryContext`**

Expand Down
37 changes: 23 additions & 14 deletions lib/handler/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const { isDisturbed, parseHeaders, parseRangeHeader } = require('../core/util')

function calculateRetryAfterHeader (retryAfter) {
const current = Date.now()
const diff = new Date(retryAfter).getTime() - current

return diff
return new Date(retryAfter).getTime() - current
}

class RetryHandler {
Expand All @@ -22,6 +20,7 @@ class RetryHandler {
maxTimeout,
minTimeout,
timeoutFactor,
ifMatch,
// Response scoped
methods,
errorCodes,
Expand All @@ -36,6 +35,7 @@ class RetryHandler {
this.aborted = false
this.retryOpts = {
retry: retryFn ?? RetryHandler[kRetryHandlerDefaultRetry],
ifMatch: ifMatch ?? true,
retryAfter: retryAfter ?? true,
maxTimeout: maxTimeout ?? 30 * 1000, // 30s,
minTimeout: minTimeout ?? 500, // .5s
Expand Down Expand Up @@ -116,11 +116,7 @@ class RetryHandler {
const { counter } = state

// Any code that is not a Undici's originated and allowed to retry
if (
code &&
code !== 'UND_ERR_REQ_RETRY' &&
!errorCodes.includes(code)
) {
if (code && code !== 'UND_ERR_REQ_RETRY' && !errorCodes.includes(code)) {
cb(err)
return
}
Expand Down Expand Up @@ -246,10 +242,7 @@ class RetryHandler {
start != null && Number.isFinite(start),
'content-range mismatch'
)
assert(
end != null && Number.isFinite(end),
'invalid content-length'
)
assert(end != null && Number.isFinite(end), 'invalid content-length')

this.start = start
this.end = end
Expand All @@ -270,6 +263,13 @@ class RetryHandler {
this.resume = resume
this.etag = headers.etag != null ? headers.etag : null

// Weak etags are not useful for comparison nor cache
// for instance not safe to assume if the response is byte-per-byte
// equal
if (this.etag != null && this.etag.startsWith('W/')) {
this.etag = null
}

return this.handler.onHeaders(
statusCode,
rawHeaders,
Expand Down Expand Up @@ -308,7 +308,9 @@ class RetryHandler {
// and server error response
if (this.retryCount - this.retryCountCheckpoint > 0) {
// We count the difference between the last checkpoint and the current retry count
this.retryCount = this.retryCountCheckpoint + (this.retryCount - this.retryCountCheckpoint)
this.retryCount =
this.retryCountCheckpoint +
(this.retryCount - this.retryCountCheckpoint)
} else {
this.retryCount += 1
}
Expand All @@ -328,11 +330,18 @@ class RetryHandler {
}

if (this.start !== 0) {
const headers = { range: `bytes=${this.start}-${this.end ?? ''}` }

// Weak etag check - weak etags will make comparison algorithms never match
if (this.retryOpts.ifMatch && this.etag != null) {
headers['if-match'] = this.etag
}

this.opts = {
...this.opts,
headers: {
...this.opts.headers,
range: `bytes=${this.start}-${this.end ?? ''}`
...headers
}
}
}
Expand Down
Loading