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

feat: add support for if-match on retry handler #3144

Merged
merged 7 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 22 additions & 15 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,
metcoder95 marked this conversation as resolved.
Show resolved Hide resolved
retryAfter: retryAfter ?? true,
maxTimeout: maxTimeout ?? 30 * 1000, // 30s,
minTimeout: minTimeout ?? 500, // .5s
Expand Down Expand Up @@ -64,6 +64,7 @@ class RetryHandler {
this.start = 0
this.end = null
this.etag = null
this.isWeakEtag = false
this.resume = null

// Handle possible onConnect duplication
Expand Down Expand Up @@ -116,11 +117,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 @@ -208,7 +205,7 @@ class RetryHandler {
}

// Let's start with a weak etag check
if (this.etag != null && this.etag !== headers.etag) {
if (this.etag != null && !this.isWeakEtag && this.etag !== headers.etag) {
this.abort(
new RequestRetryError('ETag mismatch', statusCode, {
headers,
Expand Down Expand Up @@ -246,10 +243,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 +264,10 @@ class RetryHandler {
this.resume = resume
this.etag = headers.etag != null ? headers.etag : null

if (this.etag != null) {
this.isWeakEtag = this.etag.startsWith('W/')
}

return this.handler.onHeaders(
statusCode,
rawHeaders,
Expand Down Expand Up @@ -308,7 +306,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 +328,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 && !this.isWeakEtag) {
headers['if-match'] = this.etag
}

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