Skip to content

Commit

Permalink
fix: onConnect retry
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Nov 12, 2024
1 parent 20b8026 commit 4f8dff1
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 17 deletions.
7 changes: 7 additions & 0 deletions lib/api/api-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ConnectHandler extends AsyncResource {
this.responseHeaders = responseHeaders || null
this.callback = callback
this.abort = null
this.upgraded = false

addSignal(this, signal)
}
Expand All @@ -49,6 +50,12 @@ class ConnectHandler extends AsyncResource {
}

onUpgrade (statusCode, rawHeaders, socket) {
if (this.upgraded) {
throw new Error('request already upgraded')
}

this.upgraded = true

const { callback, opaque, context } = this

removeSignal(this)
Expand Down
10 changes: 10 additions & 0 deletions lib/api/api-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class PipelineHandler extends AsyncResource {
this.abort = null
this.context = null
this.onInfo = onInfo || null
this.headersSent = false

this.req = new PipelineRequest().on('error', noop)

Expand Down Expand Up @@ -160,7 +161,16 @@ class PipelineHandler extends AsyncResource {
this.context = context
}

onUpgrade () {
throw new Error('bad upgrade')
}

onHeaders (statusCode, rawHeaders, resume) {
if (this.headersSent) {
throw new Error('headers already sent')
}
this.headersSent = true

const { opaque, handler, context } = this

if (statusCode < 200) {
Expand Down
10 changes: 10 additions & 0 deletions lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class RequestHandler extends AsyncResource {
this.highWaterMark = highWaterMark
this.reason = null
this.removeAbortListener = null
this.headersSent = false

if (signal?.aborted) {
this.reason = signal.reason ?? new RequestAbortedError()
Expand Down Expand Up @@ -85,7 +86,16 @@ class RequestHandler extends AsyncResource {
this.context = context
}

onUpgrade () {
throw new Error('bad upgrade')
}

onHeaders (statusCode, rawHeaders, resume, statusMessage) {
if (this.headersSent) {
throw new Error('headers already sent')
}
this.headersSent = true

const { callback, opaque, abort, context, responseHeaders, highWaterMark } = this

const headers = responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
Expand Down
10 changes: 10 additions & 0 deletions lib/api/api-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class StreamHandler extends AsyncResource {
this.trailers = null
this.body = body
this.onInfo = onInfo || null
this.headersSent = false

if (util.isStream(body)) {
body.on('error', (err) => {
Expand All @@ -78,7 +79,16 @@ class StreamHandler extends AsyncResource {
this.context = context
}

onUpgrade () {
throw new Error('bad upgrade')
}

onHeaders (statusCode, rawHeaders, resume, statusMessage) {
if (this.headersSent) {
throw new Error('headers already sent')
}
this.headersSent = true

const { factory, opaque, context, responseHeaders } = this

const headers = responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
Expand Down
6 changes: 6 additions & 0 deletions lib/api/api-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class UpgradeHandler extends AsyncResource {
this.callback = callback
this.abort = null
this.context = null
this.upgraded = false

addSignal(this, signal)
}
Expand All @@ -50,6 +51,11 @@ class UpgradeHandler extends AsyncResource {
}

onUpgrade (statusCode, rawHeaders, socket) {
if (this.upgraded) {
throw new Error('request already upgraded')
}
this.upgraded = true

assert(statusCode === 101)

const { callback, opaque, context } = this
Expand Down
32 changes: 15 additions & 17 deletions lib/handler/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class RetryHandler {
this.dispatch = handlers.dispatch
this.handler = handlers.handler
this.opts = { ...dispatchOpts, body: wrapRequestBody(opts.body) }
this.abort = null
this.aborted = false
this.retryOpts = {
retry: retryFn ?? RetryHandler[kRetryHandlerDefaultRetry],
retryAfter: retryAfter ?? true,
Expand All @@ -62,22 +60,14 @@ class RetryHandler {
]
}

this.abort = null
this.aborted = false
this.retryCount = 0
this.retryCountCheckpoint = 0
this.start = 0
this.end = null
this.etag = null
this.resume = null

// Handle possible onConnect duplication
this.handler.onConnect(reason => {
this.aborted = true
if (this.abort) {
this.abort(reason)
} else {
this.reason = reason
}
})
}

onRequestSent () {
Expand All @@ -93,11 +83,19 @@ class RetryHandler {
}

onConnect (abort) {
if (this.aborted) {
abort(this.reason)
} else {
this.abort = abort
}
this.abort = abort
this.aborted = false
this.retryCount = 0
this.retryCountCheckpoint = 0
this.start = 0
this.end = null
this.etag = null
this.resume = null

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

onBodySent (chunk) {
Expand Down

0 comments on commit 4f8dff1

Please sign in to comment.