Skip to content

[ISSUE #8435] Copy upstream response headers natively in NettyHttpClientPlugin - #8508

Closed
konlue wants to merge 2 commits into
apache:masterfrom
konlue:perf/8435-native-header-copy
Closed

konlue wants to merge 2 commits into
apache:masterfrom
konlue:perf/8435-native-header-copy

Conversation

@konlue

@konlue konlue commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Closing in favor of the original author's PR (#8436) — apologies for the duplicate: my claim checklist missed the linked PRs, which the issue search surface does not show. The work here added nothing beyond what that PR already covers.

…ttpClientPlugin

For every proxied response, NettyHttpClientPlugin built an intermediate
Spring HttpHeaders (a LinkedCaseInsensitiveMap with a list per name) from
the upstream Netty headers and wrote it through Spring's adapter back
into the Reactor Netty response headers. Since Spring 6.1, the server
response's getHeaders() is a live view of the Netty headers, so the
intermediate map is unnecessary in the default case.

The plugin now takes a fast path when the response is Spring's own
package-private ReactorServerHttpResponse (matched by class name), not
yet committed, no RESP_UNIQUE_HEADER handling is configured (the dedup
works on the intermediate map) and the plugin is not subclassed
(identity comparison — subclasses may override doRequest): it sets each
upstream name directly on the native Netty headers, which the live view
exposes unchanged. In every other case the existing path is unchanged,
including the UnsupportedOperationException guard for read-only
responses behind decorators.
…t path

set(name, value) per upstream entry made duplicate names last-wins,
dropping values for headers that legitimately repeat (Set-Cookie). The
fast path now follows the issue wording exactly: remove the names the
upstream sends, then add every upstream entry — multi-value headers keep
all their values, the same putAll semantics as the slow path. A test
pins a two-value Set-Cookie.

@wy471x wy471x left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: request changes (one correctness gap, otherwise clean)

Thanks — the allocation-only framing, the four guards and the byte-identical fallback are all in good shape. There is one blocking issue.

Blocking: the native header copy bypasses HttpRouteResponseHeaders.apply

copyUpstreamHeadersFast(...) runs at the top of the response callback, i.e. before HttpRouteResponseHeaders.apply(exchange, () -> ...). That wrapper is not just a status guard: it snapshots the gateway's uncommitted headers before the first upstream write, and when a timed route has already been restored it suppresses all late upstream writes (state.timedOut). With this patch:

  1. If the route already timed out, the upstream headers still land on the native Netty headers while the wrapper only suppresses the status write — a timeout response can advertise the upstream's content-type, set-cookie or content-length.
  2. The snapshot taken inside apply now already contains the upstream headers, so restore() restores a contaminated header set. The PR description ("the wrapper's snapshot reads the fast path's writes through the live view") describes the opposite order of what the code does.

Fix: move the native write inside the runnable passed to apply, so both paths share the same seam and the timed-out suppression applies to the fast path too. Please add a regression test that installs the HttpRouteResponseHeaders state, makes it time out, and asserts that no upstream header reaches the native headers.

Non-blocking

  • final HttpHeaders headers = nativeCopy ? null : slowPathHeaders(...) introduces a null-typed flow through a method call; two explicit branches read better and remove the null.
  • copyUpstreamHeadersFast is package-private only so the test can call it, and the tests never exercise doRequest. Prefer keeping it private and asserting through doRequest with a real ReactorServerHttpResponse (like #8436's socket-based tests do).
  • The committed-response test reflects into AbstractServerHttpResponse.state and the package-private State.valueOf; that breaks silently on Spring upgrades. Guard it with an assumption so it skips instead of erroring.
  • The description says the fast path uses "set = replace values per name"; the code does remove+add. The semantics are equivalent, but please align the wording.

Duplicate PR

#8436 targets the same issue #8435. Please consolidate on one PR. This one is the better shape (it keeps the apply seam and is a smaller divergence) — after the fix above I'd support merging this PR and closing #8436.

Note: the class-name match and the getClass() != NettyHttpClientPlugin.class guard are exactly what the issue prescribes, so I'm not asking you to change that design.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants