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(#2264): Expose Retry Handler #2281

Merged
merged 18 commits into from
Nov 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
175 changes: 175 additions & 0 deletions lib/retry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
const assert = require('node:assert')

const createError = require('http-errors')
metcoder95 marked this conversation as resolved.
Show resolved Hide resolved

const { isDisturbed, parseHeaders } = require('./core/util')

function parseRange (range) {
if (range == null || range === '') return { start: 0, end: null }

const m = range ? range.match(/^bytes=(\d+)-(\d+)?$/) : null
return m ? { start: parseInt(m[1]), end: m[2] ? parseInt(m[2]) : null } : null
}

class RetryAgent {
constructor (opts, { dispatch, handler }) {
const {
retry: {
retry: retryFn,
// Retry scoped
max,
maxTimeout,
minTimeout,
timeoutFactor,
// Response scoped
methods,
idempotent,
codes,
status: statusCode
}
} = opts.retry ?? {}

this.dispatch = dispatch
this.handler = handler
this.opts = opts
this.abort = null
this.aborted = false
this.opts.retry = {
retry: retryFn ?? this.retry,
maxTimeout: maxTimeout ?? 30 * 1000, // 30s,
timeout: minTimeout ?? 500, // 1s
timeoutFactor: timeoutFactor ?? 2,
max: max ?? 8,
// Indicates weather or not retry on methods
// Takes prevalence over idempotent
methods: methods ?? [
'GET',
'HEAD',
'OPTIONS',
'PUT',
'DELETE',
'TRACE',
'PATCH'
],
// States weather or not retry on idempotent methods
idempotent: idempotent ?? true,
// Works as block list if status is not provided
status: statusCode ?? [420, 429, 502, 503, 504],
// List of errors to not retry
codes: codes ?? [
'ECONNRESET',
'ECONNREFUSED',
'ENOTFOUND',
'ENETDOWN',
'ENETUNREACH',
'EHOSTDOWN',
'EHOSTUNREACH',
'EPIPE'
]
}

// TODO: hidde behind a symbol
this.count = 0
this.timeout = null
this.retryAfter = this.retryOpts.timeout
this.range = opts.method === 'GET' ? parseRange(opts.headers?.range) : null
}

onConnect (abort) {
this.abort = abort
return this.handler.onConnect(reason => {
this.aborted = true
this.abort(reason)
})
}

onBodySent (chunk) {
return this.handler.onBodySent(chunk)
}

// TODO: hidde behind a symbol
retry (_err, { counter, currentTimeout }, opts) {
const { max, maxTimeout, timeoutFactor } = opts.retry

if (counter > max) return null

const retryTimeout = Math.min(
currentTimeout * timeoutFactor ** counter,
maxTimeout
)

return retryTimeout
}

onHeaders (statusCode, rawHeaders, resume, statusMessage) {
if (statusCode < 400 || this.opts.retry.status.includes(statusCode)) {
return this.handler.onHeaders(
statusCode,
rawHeaders,
resume,
statusMessage
)
}

const err = createError(statusCode, { headers: parseHeaders(rawHeaders) })

// Meant to just indicate weather or not trigger a retry
const retryAfter = this.retryOpts.retry(
err,
{ counter: this.count++, currentTimeout: this.retryAfter },
this.opts
)

if (retryAfter == null) {
return this.handler.onHeaders(
statusCode,
rawHeaders,
resume,
statusMessage
)
}

assert(Number.isFinite(retryAfter), 'invalid retryAfter')

this.retryAfter = retryAfter

this.abort(err)

return false
}

onData (chunk) {
return this.handler.onData(chunk)
}

onComplete (rawTrailers) {
return this.handler.onComplete(rawTrailers)
}

onError (err) {
if (this.timeout) {
clearTimeout(this.timeout)
this.timeout = null
}

// TODO: improve the erro handler for retry
if (
this.retryAfter == null ||
this.aborted ||
isDisturbed(this.opts.body)
) {
return this.handler.onError(err)
}

this.timeout = setTimeout(() => {
metcoder95 marked this conversation as resolved.
Show resolved Hide resolved
this.timeout = null
try {
this.dispatch(this.opts, this)
} catch (err) {
this.handler.onError(err)
}
}, this.retryAfter)
}
}

module.exprots = RetryAgent