Skip to content

Commit 369058e

Browse files
authored
fix: honor backpressure in decompression interceptor (#5829) (#5837)
* fix: honor decompression backpressure * fix(decompress): disable size limit by default --------- (cherry picked from commit 19901d8) Signed-off-by: Matteo Collina <[email protected]>
1 parent a7914e5 commit 369058e

7 files changed

Lines changed: 1337 additions & 91 deletions

File tree

‎docs/docs/api/Dispatcher.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,7 @@ The `decompress` interceptor automatically decompresses response bodies that are
11531153

11541154
- `skipErrorResponses` - Whether to skip decompression for error responses (status codes >= 400). Default: `true`.
11551155
- `skipStatusCodes` - Array of status codes to skip decompression for. Default: `[204, 304]`.
1156+
- `maxSize` - Maximum decompressed response size in bytes for each decompression stage. Set to `0` to disable the limit. Default: `0`.
11561157

11571158
**Example - Basic Decompress Interceptor**
11581159

‎lib/handler/retry-handler.js‎

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,55 @@ function validatePartialResponseContentLength (headers, range, statusCode, retry
3535
}
3636
}
3737

38+
// A stable controller handed to the downstream handler for the lifetime of the
39+
// request. Each transparent retry/resume is a separate dispatch with its own
40+
// connection controller. The proxy always forwards to the active connection
41+
// while preserving a downstream pause across controller replacement.
42+
class RetryController {
43+
#paused = false
44+
#target = null
45+
46+
set target (target) {
47+
this.#target = target
48+
if (this.#paused) {
49+
target?.pause()
50+
}
51+
}
52+
53+
get target () { return this.#target }
54+
55+
pause () {
56+
this.#paused = true
57+
this.#target?.pause()
58+
}
59+
60+
resume () {
61+
this.#paused = false
62+
this.#target?.resume()
63+
}
64+
65+
abort (reason) {
66+
this.#target?.abort(reason)
67+
}
68+
69+
get paused () { return this.#paused || (this.#target?.paused ?? false) }
70+
get aborted () { return this.#target?.aborted ?? false }
71+
get reason () { return this.#target?.reason ?? null }
72+
get rawHeaders () { return this.#target?.rawHeaders ?? null }
73+
set rawHeaders (value) {
74+
if (this.#target) {
75+
this.#target.rawHeaders = value
76+
}
77+
}
78+
79+
get rawTrailers () { return this.#target?.rawTrailers ?? null }
80+
set rawTrailers (value) {
81+
if (this.#target) {
82+
this.#target.rawTrailers = value
83+
}
84+
}
85+
}
86+
3887
class RetryHandler {
3988
constructor (opts, { dispatch, handler }) {
4089
const { retryOptions, ...dispatchOpts } = opts
@@ -89,6 +138,7 @@ class RetryHandler {
89138
this.start = 0
90139
this.end = null
91140
this.etag = null
141+
this.controllerProxy = new RetryController()
92142
}
93143

94144
onResponseStartWithRetry (controller, statusCode, headers, statusMessage, err) {
@@ -99,11 +149,11 @@ class RetryHandler {
99149
// The downstream handler already received the response from an
100150
// earlier attempt. Forwarding this response would replace the
101151
// downstream body and leave the original body pending forever.
102-
this.handler.onResponseError?.(controller, err)
152+
this.handler.onResponseError?.(this.controllerProxy, err)
103153
} else {
104154
this.headersSent = true
105155
this.checkpointResponseEnd(headers)
106-
this.handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
156+
this.handler.onResponseStart?.(this.controllerProxy, statusCode, headers, statusMessage)
107157
}
108158
} else {
109159
this.error = err
@@ -115,7 +165,7 @@ class RetryHandler {
115165
if (isDisturbed(this.opts.body)) {
116166
this.headersSent = true
117167
this.checkpointResponseEnd(headers)
118-
this.handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
168+
this.handler.onResponseStart?.(this.controllerProxy, statusCode, headers, statusMessage)
119169
return
120170
}
121171

@@ -125,11 +175,11 @@ class RetryHandler {
125175
// The downstream handler already received the response from an
126176
// earlier attempt. Forwarding this response would replace the
127177
// downstream body and leave the original body pending forever.
128-
this.handler.onResponseError?.(controller, passedErr)
178+
this.handler.onResponseError?.(this.controllerProxy, passedErr)
129179
} else {
130180
this.headersSent = true
131181
this.checkpointResponseEnd(headers)
132-
this.handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
182+
this.handler.onResponseStart?.(this.controllerProxy, statusCode, headers, statusMessage)
133183
}
134184
controller.resume()
135185
return
@@ -165,13 +215,14 @@ class RetryHandler {
165215
}
166216

167217
onRequestStart (controller, context) {
218+
this.controllerProxy.target = controller
168219
if (!this.headersSent) {
169-
this.handler.onRequestStart?.(controller, context)
220+
this.handler.onRequestStart?.(this.controllerProxy, context)
170221
}
171222
}
172223

173-
onRequestUpgrade (controller, statusCode, headers, socket) {
174-
this.handler.onRequestUpgrade?.(controller, statusCode, headers, socket)
224+
onRequestUpgrade (_controller, statusCode, headers, socket) {
225+
this.handler.onRequestUpgrade?.(this.controllerProxy, statusCode, headers, socket)
175226
}
176227

177228
static [kRetryHandlerDefaultRetry] (err, { state, opts }, cb) {
@@ -302,7 +353,7 @@ class RetryHandler {
302353
if (range == null) {
303354
this.headersSent = true
304355
this.handler.onResponseStart?.(
305-
controller,
356+
this.controllerProxy,
306357
statusCode,
307358
headers,
308359
statusMessage
@@ -351,7 +402,7 @@ class RetryHandler {
351402

352403
this.headersSent = true
353404
this.handler.onResponseStart?.(
354-
controller,
405+
this.controllerProxy,
355406
statusCode,
356407
headers,
357408
statusMessage
@@ -364,30 +415,30 @@ class RetryHandler {
364415
}
365416
}
366417

367-
onResponseData (controller, chunk) {
418+
onResponseData (_controller, chunk) {
368419
if (this.error) {
369420
return
370421
}
371422

372423
this.start += chunk.length
373424

374-
this.handler.onResponseData?.(controller, chunk)
425+
this.handler.onResponseData?.(this.controllerProxy, chunk)
375426
}
376427

377-
onResponseEnd (controller, trailers) {
428+
onResponseEnd (_controller, trailers) {
378429
if (this.error && this.retryOpts.throwOnError) {
379430
throw this.error
380431
}
381432

382433
if (!this.error) {
383434
this.retryCount = 0
384-
return this.handler.onResponseEnd?.(controller, trailers)
435+
return this.handler.onResponseEnd?.(this.controllerProxy, trailers)
385436
}
386437

387-
this.retry(controller)
438+
this.retry()
388439
}
389440

390-
retry (controller) {
441+
retry () {
391442
if (this.start !== 0) {
392443
const headers = { range: `bytes=${this.start}-${this.end ?? ''}` }
393444

@@ -409,23 +460,23 @@ class RetryHandler {
409460
this.retryCountCheckpoint = this.retryCount
410461
this.dispatch(this.opts, this)
411462
} catch (err) {
412-
this.handler.onResponseError?.(controller, err)
463+
this.handler.onResponseError?.(this.controllerProxy, err)
413464
}
414465
}
415466

416467
onResponseError (controller, err) {
417468
if (controller?.aborted || isDisturbed(this.opts.body) || (this.headersSent && !this.resume)) {
418-
this.handler.onResponseError?.(controller, err)
469+
this.handler.onResponseError?.(this.controllerProxy, err)
419470
return
420471
}
421472

422473
function shouldRetry (returnedErr) {
423474
if (!returnedErr) {
424-
this.retry(controller)
475+
this.retry()
425476
return
426477
}
427478

428-
this.handler?.onResponseError?.(controller, returnedErr)
479+
this.handler?.onResponseError?.(this.controllerProxy, returnedErr)
429480
}
430481

431482
// We reconcile in case of a mix between network errors

0 commit comments

Comments
 (0)