poorbarcode commented on code in PR #21684:
URL: https://github.com/apache/pulsar/pull/21684#discussion_r1419157675


##########
pulsar-common/src/main/java/org/apache/pulsar/common/protocol/OptionalProxyProtocolDecoder.java:
##########
@@ -19,36 +19,63 @@
 package org.apache.pulsar.common.protocol;
 
 import io.netty.buffer.ByteBuf;
+import io.netty.buffer.CompositeByteBuf;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.ChannelInboundHandlerAdapter;
 import io.netty.handler.codec.ProtocolDetectionResult;
 import io.netty.handler.codec.ProtocolDetectionState;
 import io.netty.handler.codec.haproxy.HAProxyMessageDecoder;
 import io.netty.handler.codec.haproxy.HAProxyProtocolVersion;
+import lombok.extern.slf4j.Slf4j;
 
 /**
  * Decoder that added whether a new connection is prefixed with the 
ProxyProtocol.
  * More about the ProxyProtocol see: 
http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt.
  */
+@Slf4j
 public class OptionalProxyProtocolDecoder extends ChannelInboundHandlerAdapter 
{
 
     public static final String NAME = "optional-proxy-protocol-decoder";
 
+    public static final int MIN_BYTES_SIZE_TO_DETECT_PROTOCOL = 12;
+
+    private CompositeByteBuf cumulatedByteBuf;
+
     @Override
     public void channelRead(ChannelHandlerContext ctx, Object msg) throws 
Exception {
         if (msg instanceof ByteBuf) {
-            ProtocolDetectionResult<HAProxyProtocolVersion> result =
-                    HAProxyMessageDecoder.detectProtocol((ByteBuf) msg);
-            // should accumulate data if need more data to detect the protocol
+            // Combine cumulated buffers.
+            ByteBuf buf = (ByteBuf) msg;
+            if (cumulatedByteBuf != null) {
+                buf = cumulatedByteBuf.addComponent(true, buf);
+            }
+
+            ProtocolDetectionResult<HAProxyProtocolVersion> result = 
HAProxyMessageDecoder.detectProtocol(buf);
             if (result.state() == ProtocolDetectionState.NEEDS_MORE_DATA) {
+                // Accumulate data if need more data to detect the protocol.
+                if (cumulatedByteBuf == null) {
+                    cumulatedByteBuf = new CompositeByteBuf(ctx.alloc(), 
false, MIN_BYTES_SIZE_TO_DETECT_PROTOCOL, buf);

Review Comment:
   > PulsarByteBufAllocator have defined memory policies and OOM listener.
   
   Ah, it is the same object. see: 
https://github.com/apache/pulsar/blob/master/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/BrokerService.java#L488
   
   >  and it's not recommended to use the constructor to build CompositeByteBuf 
in it's doc.
   
   Since these two recommended methods `ByteBufAllocator.compositeBuffer()` and 
`Unpooled.wrappedBuffer(ByteBuf...)` do not support the param 
`maxNumComponents`, we create `CompositeByteBuf` by constructor is better.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to