-
Notifications
You must be signed in to change notification settings - Fork 4k
netty: Support never-indexed metadata keys #12976
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
| import io.grpc.HttpConnectProxiedSocketAddress; | ||
| import io.grpc.Internal; | ||
| import io.grpc.ManagedChannelBuilder; | ||
| import io.grpc.Metadata; | ||
| import io.grpc.NameResolverProvider; | ||
| import io.grpc.NameResolverRegistry; | ||
| import io.grpc.internal.AtomicBackoff; | ||
|
|
@@ -60,12 +61,15 @@ | |
| import io.netty.channel.ReflectiveChannelFactory; | ||
| import io.netty.channel.socket.nio.NioSocketChannel; | ||
| import io.netty.handler.ssl.SslContext; | ||
| import io.netty.util.AsciiString; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.SocketAddress; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
@@ -105,6 +109,7 @@ public final class NettyChannelBuilder extends ForwardingChannelBuilder2<NettyCh | |
| private ObjectPool<? extends EventLoopGroup> eventLoopGroupPool = DEFAULT_EVENT_LOOP_GROUP_POOL; | ||
| private boolean autoFlowControl = DEFAULT_AUTO_FLOW_CONTROL; | ||
| private int flowControlWindow = DEFAULT_FLOW_CONTROL_WINDOW; | ||
| private final Set<AsciiString> neverIndexedMetadataKeys = new HashSet<>(); | ||
| private int maxHeaderListSize = GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE; | ||
| private int softLimitHeaderListSize = GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE; | ||
| private int maxInboundMessageSize = GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE; | ||
|
|
@@ -434,6 +439,35 @@ public NettyChannelBuilder flowControlWindow(int flowControlWindow) { | |
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Configures an outbound metadata key to use HPACK's never-indexed literal representation. | ||
| * | ||
| * <p>All values associated with the key's normalized name will be sent as literals and will not | ||
| * be added to the peer's HPACK dynamic table. This method is additive and may be called multiple | ||
| * times. Configuring the same normalized key name more than once has no additional effect. By | ||
| * default, no metadata keys are configured as never indexed. | ||
| */ | ||
| @CanIgnoreReturnValue | ||
| public NettyChannelBuilder neverIndexMetadataKey(Metadata.Key<?> key) { | ||
| neverIndexedMetadataKeys.add(AsciiString.of(checkNotNull(key, "key").name())); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add these methods to |
||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Configures outbound metadata keys to use HPACK's never-indexed literal representation. | ||
| * | ||
| * <p>This method is equivalent to calling {@link #neverIndexMetadataKey} for each key in {@code | ||
| * keys}. Duplicate normalized key names are ignored. | ||
| */ | ||
| @CanIgnoreReturnValue | ||
| public NettyChannelBuilder neverIndexMetadataKeys( | ||
| Collection<? extends Metadata.Key<?>> keys) { | ||
| for (Metadata.Key<?> key : checkNotNull(keys, "keys")) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the input collection contains a null element after several valid keys, a NPE is thrown after the preceding keys have already been added to the builder's internal |
||
| neverIndexMetadataKey(key); | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the maximum size of header list allowed to be received. This is cumulative size of the | ||
| * headers with some overhead, as defined for | ||
|
|
@@ -626,6 +660,7 @@ ClientTransportFactory buildTransportFactory() { | |
| eventLoopGroupPool, | ||
| autoFlowControl, | ||
| flowControlWindow, | ||
| neverIndexedMetadataKeys, | ||
| maxInboundMessageSize, | ||
| maxHeaderListSize, | ||
| softLimitHeaderListSize, | ||
|
|
@@ -769,6 +804,7 @@ private static final class NettyTransportFactory implements ClientTransportFacto | |
| private final EventLoopGroup group; | ||
| private final boolean autoFlowControl; | ||
| private final int flowControlWindow; | ||
| private final Set<AsciiString> neverIndexedMetadataKeys; | ||
| private final int maxMessageSize; | ||
| private final int maxHeaderListSize; | ||
| private final int softLimitHeaderListSize; | ||
|
|
@@ -790,6 +826,7 @@ private static final class NettyTransportFactory implements ClientTransportFacto | |
| ObjectPool<? extends EventLoopGroup> groupPool, | ||
| boolean autoFlowControl, | ||
| int flowControlWindow, | ||
| Set<AsciiString> neverIndexedMetadataKeys, | ||
| int maxMessageSize, | ||
| int maxHeaderListSize, | ||
| int softLimitHeaderListSize, | ||
|
|
@@ -807,6 +844,8 @@ private static final class NettyTransportFactory implements ClientTransportFacto | |
| this.group = groupPool.getObject(); | ||
| this.autoFlowControl = autoFlowControl; | ||
| this.flowControlWindow = flowControlWindow; | ||
| this.neverIndexedMetadataKeys = Collections.unmodifiableSet( | ||
| new HashSet<>(checkNotNull(neverIndexedMetadataKeys, "neverIndexedMetadataKeys"))); | ||
| this.maxMessageSize = maxMessageSize; | ||
| this.maxHeaderListSize = maxHeaderListSize; | ||
| this.softLimitHeaderListSize = softLimitHeaderListSize; | ||
|
|
@@ -856,6 +895,7 @@ public void run() { | |
| localNegotiator, | ||
| autoFlowControl, | ||
| flowControlWindow, | ||
| neverIndexedMetadataKeys, | ||
| maxMessageSize, | ||
| maxHeaderListSize, | ||
| softLimitHeaderListSize, | ||
|
|
@@ -895,6 +935,7 @@ public SwapChannelCredentialsResult swapChannelCredentials(ChannelCredentials ch | |
| groupPool, | ||
| autoFlowControl, | ||
| flowControlWindow, | ||
| neverIndexedMetadataKeys, | ||
| maxMessageSize, | ||
| maxHeaderListSize, | ||
| softLimitHeaderListSize, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Copyright 2026 The gRPC Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.grpc.netty; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
|
|
||
| import io.netty.buffer.ByteBuf; | ||
| import io.netty.buffer.Unpooled; | ||
| import io.netty.handler.codec.http2.DefaultHttp2Headers; | ||
| import io.netty.handler.codec.http2.DefaultHttp2HeadersDecoder; | ||
| import io.netty.handler.codec.http2.DefaultHttp2HeadersEncoder; | ||
| import io.netty.handler.codec.http2.Http2Headers; | ||
| import io.netty.handler.codec.http2.Http2HeadersEncoder; | ||
| import io.netty.util.AsciiString; | ||
| import java.util.Collections; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class NettyClientHandlerSensitivityDetectorTest { | ||
| private static final AsciiString CUSTOM_NAME = AsciiString.cached("custom-key"); | ||
| private static final AsciiString CUSTOM_VALUE = AsciiString.cached("custom-value"); | ||
|
|
||
| @Test | ||
| public void emptyConfigurationUsesDefaultPolicy() { | ||
| assertThat(NettyClientHandler.sensitivityDetector(Collections.<AsciiString>emptySet())) | ||
| .isSameInstanceAs(Http2HeadersEncoder.NEVER_SENSITIVE); | ||
| } | ||
|
|
||
| @Test | ||
| public void configuredHeaderIsNeverIndexed() throws Exception { | ||
| DefaultHttp2HeadersEncoder encoder = new DefaultHttp2HeadersEncoder( | ||
| NettyClientHandler.sensitivityDetector(Collections.singleton(CUSTOM_NAME)), | ||
| false, | ||
| 16, | ||
| Integer.MAX_VALUE); | ||
| DefaultHttp2HeadersDecoder decoder = new DefaultHttp2HeadersDecoder(); | ||
| ByteBuf first = Unpooled.buffer(); | ||
| ByteBuf second = Unpooled.buffer(); | ||
| try { | ||
| Http2Headers headers = new DefaultHttp2Headers().add(CUSTOM_NAME, CUSTOM_VALUE); | ||
|
|
||
| encoder.encodeHeaders(1, headers, first); | ||
| encoder.encodeHeaders(3, headers, second); | ||
|
|
||
| assertThat(first.getUnsignedByte(first.readerIndex()) & 0xF0).isEqualTo(0x10); | ||
| assertThat(second.getUnsignedByte(second.readerIndex()) & 0xF0).isEqualTo(0x10); | ||
| assertThat(decoder.decodeHeaders(1, first).get(CUSTOM_NAME).toString()) | ||
| .isEqualTo(CUSTOM_VALUE.toString()); | ||
| assertThat(decoder.decodeHeaders(3, second).get(CUSTOM_NAME).toString()) | ||
| .isEqualTo(CUSTOM_VALUE.toString()); | ||
| } finally { | ||
| first.release(); | ||
| second.release(); | ||
| encoder.close(); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
@since 1.84.0annotation to both methods. Also add@ExperimentalApiannotation.