Skip to content

Commit

Permalink
Copy connection attributes to streams (#1883)
Browse files Browse the repository at this point in the history
* Use generics on attribute copy

* Copy connection dimensions as well
  • Loading branch information
jguerra authored Jan 28, 2025
1 parent b5857dd commit e54f879
Showing 1 changed file with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.netflix.netty.common.metrics.Http2MetricsChannelHandlers;
import com.netflix.netty.common.proxyprotocol.HAProxyMessageChannelHandler;
import com.netflix.zuul.netty.server.BaseZuulChannelInitializer;
import com.netflix.zuul.netty.server.Server;
import com.netflix.zuul.netty.server.ssl.SslHandshakeInfoHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
Expand All @@ -30,6 +31,8 @@
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http2.Http2StreamFrameToHttpObjectCodec;
import io.netty.util.AttributeKey;

import java.util.Set;
import java.util.function.Consumer;

/**
Expand All @@ -38,6 +41,23 @@
@ChannelHandler.Sharable
public class Http2StreamInitializer extends ChannelInboundHandlerAdapter {

private static final Set<AttributeKey<?>> ATTRIBUTES_TO_COPY = Set.of(
SourceAddressChannelHandler.ATTR_LOCAL_ADDRESS,
SourceAddressChannelHandler.ATTR_LOCAL_INET_ADDR,
SourceAddressChannelHandler.ATTR_SOURCE_ADDRESS,
SourceAddressChannelHandler.ATTR_REMOTE_ADDR,
SourceAddressChannelHandler.ATTR_SOURCE_INET_ADDR,
SourceAddressChannelHandler.ATTR_SERVER_LOCAL_ADDRESS,
SourceAddressChannelHandler.ATTR_SERVER_LOCAL_PORT,
SourceAddressChannelHandler.ATTR_PROXY_PROTOCOL_DESTINATION_ADDRESS,
Http2OrHttpHandler.PROTOCOL_NAME,
SslHandshakeInfoHandler.ATTR_SSL_INFO,
HAProxyMessageChannelHandler.ATTR_HAPROXY_MESSAGE,
HAProxyMessageChannelHandler.ATTR_HAPROXY_VERSION,
HAProxyMessageChannelHandler.ATTR_HAPROXY_CUSTOM_TLVS,
BaseZuulChannelInitializer.ATTR_CHANNEL_CONFIG,
Server.CONN_DIMENSIONS);

private static final Http2StreamHeaderCleaner http2StreamHeaderCleaner = new Http2StreamHeaderCleaner();
private static final Http2ResetFrameHandler http2ResetFrameHandler = new Http2ResetFrameHandler();
private static final Http2StreamErrorHandler http2StreamErrorHandler = new Http2StreamErrorHandler();
Expand Down Expand Up @@ -90,29 +110,12 @@ protected void addHttp2MetricsHandlers(ChannelPipeline pipeline) {
}

protected void copyAttrsFromParentChannel(Channel parent, Channel child) {
AttributeKey[] attributesToCopy = {
SourceAddressChannelHandler.ATTR_LOCAL_ADDRESS,
SourceAddressChannelHandler.ATTR_LOCAL_INET_ADDR,
SourceAddressChannelHandler.ATTR_SOURCE_ADDRESS,
SourceAddressChannelHandler.ATTR_REMOTE_ADDR,
SourceAddressChannelHandler.ATTR_SOURCE_INET_ADDR,
SourceAddressChannelHandler.ATTR_SERVER_LOCAL_ADDRESS,
SourceAddressChannelHandler.ATTR_SERVER_LOCAL_PORT,
SourceAddressChannelHandler.ATTR_PROXY_PROTOCOL_DESTINATION_ADDRESS,
Http2OrHttpHandler.PROTOCOL_NAME,
SslHandshakeInfoHandler.ATTR_SSL_INFO,
HAProxyMessageChannelHandler.ATTR_HAPROXY_MESSAGE,
HAProxyMessageChannelHandler.ATTR_HAPROXY_VERSION,
HAProxyMessageChannelHandler.ATTR_HAPROXY_CUSTOM_TLVS,
BaseZuulChannelInitializer.ATTR_CHANNEL_CONFIG
};

for (AttributeKey key : attributesToCopy) {
copyAttrFromParentChannel(parent, child, key);
for (AttributeKey<?> key : ATTRIBUTES_TO_COPY) {
copyAttributesFromParentChannel(parent, child, key);
}
}

protected void copyAttrFromParentChannel(Channel parent, Channel child, AttributeKey key) {
protected <T> void copyAttributesFromParentChannel(Channel parent, Channel child, AttributeKey<T> key) {
child.attr(key).set(parent.attr(key).get());
}
}

0 comments on commit e54f879

Please sign in to comment.