Repository navigation
Commit c6750e2
Mark an HTTP/2 connection instead of looking it up (#2320)
## Problem
`ChannelManager.isHttp2(channel)` asks the pipeline for the multiplex
handler by name:
```java
return channel.pipeline().get(HTTP2_MULTIPLEX) != null;
```
The write path asks it of every request, twice: once in
`sendRequestWithOpenChannel` to decide
whether to store the per-request future on the channel, once in
`writeRequest` to route the
write. `DefaultChannelPipeline.get(String)` walks the handler chain
comparing names, and an
HTTP/1.1 connection - which has no such handler - is walked to the end
to answer no. That is the
common case for anyone who has not turned HTTP/2 on.
In a profile of a client running with `setHttp2Enabled(false)`,
`DefaultChannelPipeline.context0` accounted for 83 CPU samples on this
alone.
## Change
The multiplex handler is installed in exactly one place,
`upgradePipelineToHttp2`, so a channel
attribute is set beside it and `isHttp2` reads that instead:
```java
public static boolean isHttp2(Channel channel) {
return channel.hasAttr(HTTP2_CONNECTION_ATTRIBUTE);
}
```
A binary search over integer keys in a small array, rather than a walk
with a string compare per
handler. `hasAttr` rather than `attr(...).get()`: the latter would add
an entry to the attribute
map of every HTTP/1.1 channel just to find nothing in it.
Behaviour is unchanged in every configuration, including the ones a
config check would get wrong
- see below.
## Why not check the config instead
Reading `config.isHttp2Enabled()` first would be cheaper still, and it
was the first thing tried.
It is not safe: the two can disagree.
`NettyConnectListener` upgrades the pipeline on the ALPN result alone,
not on the config:
```java
boolean http2Negotiated = ApplicationProtocolNames.HTTP_2.equals(alpnProtocol);
if (http2Negotiated && !uri.isWebSocket()) {
channelManager.upgradePipelineToHttp2(channel.pipeline());
```
And ALPN can select `h2` with the flag off. `DefaultSslEngineFactory`
advertises `h2` only when
`isHttp2Enabled()`, but it leaves a caller-supplied `SslContext` alone
(`config.getSslContext() != null || !config.isHttp2Enabled()`), and a
caller-supplied
`SslEngineFactory` is free to advertise whatever it likes - which the
WebSocket guard beside the
upgrade already accounts for in as many words: *"this guard is the
backstop for a custom
SslEngineFactory that still advertises h2"*.
`upgradePipelineToHttp2AfterProxyConnect` is gated on
ALPN the same way.
With `http2Enabled(false)` and such a context, a config check would
route a genuine HTTP/2
connection down the HTTP/1.1 branch and write an HTTP/1.1 request onto
an HTTP/2 pipeline. The
attribute costs nothing more than the config read would have saved, and
cannot disagree with the
handler, being set where the handler is.
If HTTP/2 negotiated against `http2Enabled(false)` is considered
unsupported, a config
short-circuit could be layered on top of this - but that is a behaviour
decision rather than a
micro-optimisation, so it is not made here.
## Can the attribute go stale
No. Nothing removes `HTTP2_MULTIPLEX` from a pipeline - the only
reference to it besides the
lookup is the `addLast` in the upgrade - so there is no downgrade for
the two to diverge across.
Stream child channels carry neither the handler nor the attribute, so
`isHttp2` answers no for
them exactly as it did before.
## Tests
`ChannelManagerHttp2MarkerTest` pins the attribute to the handler:
either both say HTTP/2 or
neither does, so a later change to the upgrade cannot set one and forget
the other. Three cases -
a connection never upgraded, one upgraded, and a stream channel.
Checked by mutation rather than assumption: dropping the attribute
assignment fails that test and
46 of the 52 in `BasicHttp2Test`, the write path having routed HTTP/2
connections down the
HTTP/1.1 branch.
## Verification
`mvnw clean verify` - BUILD SUCCESS, 1488 tests, 0 failures, 0 errors,
26 skipped. Error Prone,
NullAway and Revapi all clean, with no revapi entries: `isHttp2` keeps
its signature and the
attribute key is private.
Caveat on the testing gate: `AGENTS.md` requires the build to run on JDK
11 and no JDK 11 is
installed on this machine, so it was run on **JDK 17** (also in the CI
matrix). The JDK 11 legs of
CI on this PR are the real gate.
Claude Code on behalf of @pavel-ptashyts
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>1 parent d1bdd7e commit c6750e2
4 files changed
Lines changed: 162 additions & 33 deletions
File tree
- client/src
- main/java/org/asynchttpclient/netty
- channel
- handler/intercept
- request
- test/java/org/asynchttpclient/netty/channel
Lines changed: 28 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1029 | 1029 | | |
1030 | 1030 | | |
1031 | 1031 | | |
1032 | | - | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
1033 | 1044 | | |
1034 | 1045 | | |
1035 | | - | |
| 1046 | + | |
1036 | 1047 | | |
1037 | 1048 | | |
1038 | 1049 | | |
| |||
1070 | 1081 | | |
1071 | 1082 | | |
1072 | 1083 | | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
1073 | 1099 | | |
1074 | 1100 | | |
1075 | 1101 | | |
| |||
1100 | 1126 | | |
1101 | 1127 | | |
1102 | 1128 | | |
1103 | | - | |
1104 | | - | |
1105 | | - | |
1106 | | - | |
1107 | | - | |
1108 | | - | |
1109 | | - | |
1110 | | - | |
1111 | | - | |
1112 | | - | |
1113 | 1129 | | |
1114 | 1130 | | |
1115 | 1131 | | |
| |||
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
45 | | - | |
46 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| |||
Lines changed: 18 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
704 | 704 | | |
705 | 705 | | |
706 | 706 | | |
707 | | - | |
708 | | - | |
709 | | - | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
710 | 713 | | |
711 | 714 | | |
712 | 715 | | |
| |||
772 | 775 | | |
773 | 776 | | |
774 | 777 | | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
775 | 781 | | |
776 | | - | |
777 | | - | |
778 | | - | |
779 | | - | |
| 782 | + | |
| 783 | + | |
780 | 784 | | |
781 | 785 | | |
782 | 786 | | |
| |||
851 | 855 | | |
852 | 856 | | |
853 | 857 | | |
854 | | - | |
855 | | - | |
856 | | - | |
857 | | - | |
858 | | - | |
859 | | - | |
860 | | - | |
861 | | - | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
862 | 864 | | |
863 | 865 | | |
864 | 866 | | |
| |||
897 | 899 | | |
898 | 900 | | |
899 | 901 | | |
900 | | - | |
901 | | - | |
902 | | - | |
| 902 | + | |
903 | 903 | | |
904 | 904 | | |
905 | 905 | | |
| |||
Lines changed: 113 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
0 commit comments