This is an automated email from the ASF dual-hosted git repository.

zrlw pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.3 by this push:
     new e396bde74a Add connection preface process to TripleHttp2Protocol 
(#15368)
e396bde74a is described below

commit e396bde74aa16961137f955f3c9fac35d6bc0410
Author: zrlw <[email protected]>
AuthorDate: Sat Jun 7 13:05:42 2025 +0800

    Add connection preface process to TripleHttp2Protocol (#15368)
---
 .../apache/dubbo/remoting/api/WireProtocol.java    |  4 ++
 .../remoting/api/connection/ConnectionHandler.java |  7 +++
 .../netty4/AbstractNettyConnectionClient.java      | 57 ++++++++++++++++++++++
 .../transport/netty4/NettyConnectionHandler.java   |  8 +++
 .../rpc/protocol/tri/TripleHttp2Protocol.java      |  7 +++
 .../tri/transport/TripleHttp2SettingsHandler.java  | 45 +++++++++++++++++
 6 files changed, 128 insertions(+)

diff --git 
a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/WireProtocol.java
 
b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/WireProtocol.java
index 89f6d1766f..952884d98f 100644
--- 
a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/WireProtocol.java
+++ 
b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/WireProtocol.java
@@ -32,4 +32,8 @@ public interface WireProtocol {
     void configClientPipeline(URL url, ChannelOperator operator, 
ContextOperator contextOperator);
 
     void close();
+
+    default boolean hasConnectionPreface() {
+        return false;
+    }
 }
diff --git 
a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/connection/ConnectionHandler.java
 
b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/connection/ConnectionHandler.java
index 4bafd19266..871a754c01 100644
--- 
a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/connection/ConnectionHandler.java
+++ 
b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/api/connection/ConnectionHandler.java
@@ -31,4 +31,11 @@ public interface ConnectionHandler {
      * @param channel Channel
      */
     void reconnect(Object channel);
+
+    /**
+     * when connection preface is received.
+     *
+     * @param channel Channel
+     */
+    void onConnectionPrefaceReceived(Object channel);
 }
diff --git 
a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/AbstractNettyConnectionClient.java
 
b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/AbstractNettyConnectionClient.java
index f26ca4d819..f0530d85d5 100644
--- 
a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/AbstractNettyConnectionClient.java
+++ 
b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/AbstractNettyConnectionClient.java
@@ -57,6 +57,8 @@ public abstract class AbstractNettyConnectionClient extends 
AbstractConnectionCl
 
     private ConnectionListener connectionListener;
 
+    private AtomicReference<Promise<Void>> connectionPrefaceReceivedPromiseRef;
+
     public static final AttributeKey<AbstractConnectionClient> CONNECTION = 
AttributeKey.valueOf("connection");
 
     public AbstractNettyConnectionClient(URL url, ChannelHandler handler) 
throws RemotingException {
@@ -81,6 +83,12 @@ public abstract class AbstractNettyConnectionClient extends 
AbstractConnectionCl
         isReconnecting = new AtomicBoolean(false);
         connectionListener = new ConnectionListener();
         increase();
+
+        // Create connection preface received promise for Http2 client since 
it should wait server http2 settings frame
+        // before sending client Headers frame, see details at 
https://github.com/apache/dubbo/issues/15233
+        if (protocol != null && protocol.hasConnectionPreface()) {
+            connectionPrefaceReceivedPromiseRef = new AtomicReference<>();
+        }
     }
 
     protected abstract void initBootstrap() throws Exception;
@@ -129,6 +137,11 @@ public abstract class AbstractNettyConnectionClient 
extends AbstractConnectionCl
         Future<Void> connectPromise = performConnect();
         connectPromise.addListener(connectionListener);
 
+        Promise<Void> connectionPrefaceReceivedPromise = null;
+        if (connectionPrefaceReceivedPromiseRef != null) {
+            connectionPrefaceReceivedPromise = 
getOrCreateConnectionPrefaceReceivedPromise();
+        }
+
         boolean ret = 
connectingPromise.awaitUninterruptibly(getConnectTimeout(), 
TimeUnit.MILLISECONDS);
         // destroy connectingPromise after used
         synchronized (this) {
@@ -168,6 +181,35 @@ public abstract class AbstractNettyConnectionClient 
extends AbstractConnectionCl
 
             throw remotingException;
         }
+
+        if (connectionPrefaceReceivedPromise != null) {
+            long retainedTimeout = getConnectTimeout() - 
System.currentTimeMillis() + start;
+            ret = 
connectionPrefaceReceivedPromise.awaitUninterruptibly(retainedTimeout, 
TimeUnit.MILLISECONDS);
+            // destroy connectionPrefaceReceivedPromise after used
+            synchronized (this) {
+                connectionPrefaceReceivedPromiseRef.set(null);
+            }
+            if (!ret || !connectionPrefaceReceivedPromise.isSuccess()) {
+                // 6-2 Client-side connection preface timeout
+                RemotingException remotingException = new RemotingException(
+                        this,
+                        "client(url: " + getUrl() + ") failed to connect to 
server " + getConnectAddress()
+                                + " client-side connection preface timeout " + 
getConnectTimeout() + "ms (elapsed: "
+                                + (System.currentTimeMillis() - start) + "ms) 
from netty client "
+                                + NetUtils.getLocalHost()
+                                + " using dubbo version "
+                                + Version.getVersion());
+
+                logger.error(
+                        TRANSPORT_CLIENT_CONNECT_TIMEOUT,
+                        "provider crash",
+                        "",
+                        "Client-side connection preface timeout",
+                        remotingException);
+
+                throw remotingException;
+            }
+        }
     }
 
     protected abstract ChannelFuture performConnect();
@@ -246,6 +288,16 @@ public abstract class AbstractNettyConnectionClient 
extends AbstractConnectionCl
         }
     }
 
+    public void onConnectionPrefaceReceived(Object channel) {
+        if (!(channel instanceof io.netty.channel.Channel) || 
connectionPrefaceReceivedPromiseRef == null) {
+            return;
+        }
+        Promise<Void> connectionPrefaceReceivedPromise = 
connectionPrefaceReceivedPromiseRef.get();
+        if (connectionPrefaceReceivedPromise != null) {
+            connectionPrefaceReceivedPromise.trySuccess(null);
+        }
+    }
+
     @Override
     protected Channel getChannel() {
         io.netty.channel.Channel c = getNettyChannel();
@@ -302,6 +354,11 @@ public abstract class AbstractNettyConnectionClient 
extends AbstractConnectionCl
         return connectingPromiseRef.get();
     }
 
+    private Promise<Void> getOrCreateConnectionPrefaceReceivedPromise() {
+        connectionPrefaceReceivedPromiseRef.compareAndSet(null, new 
DefaultPromise<>(GlobalEventExecutor.INSTANCE));
+        return connectionPrefaceReceivedPromiseRef.get();
+    }
+
     public Promise<Void> getClosePromise() {
         return closePromise;
     }
diff --git 
a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConnectionHandler.java
 
b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConnectionHandler.java
index 9c4d08c0ce..00116d3d2a 100644
--- 
a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConnectionHandler.java
+++ 
b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyConnectionHandler.java
@@ -83,6 +83,14 @@ public class NettyConnectionHandler extends 
ChannelInboundHandlerAdapter impleme
         eventLoop.schedule(connectionClient::doReconnect, 1, TimeUnit.SECONDS);
     }
 
+    @Override
+    public void onConnectionPrefaceReceived(Object channel) {
+        if (!(channel instanceof Channel)) {
+            return;
+        }
+        connectionClient.onConnectionPrefaceReceived(channel);
+    }
+
     @Override
     public void channelActive(ChannelHandlerContext ctx) {
         ctx.fireChannelActive();
diff --git 
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/TripleHttp2Protocol.java
 
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/TripleHttp2Protocol.java
index bbd53f4ab1..1d68251d4e 100644
--- 
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/TripleHttp2Protocol.java
+++ 
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/TripleHttp2Protocol.java
@@ -41,6 +41,7 @@ import 
org.apache.dubbo.rpc.protocol.tri.h12.TripleProtocolDetector;
 import 
org.apache.dubbo.rpc.protocol.tri.h12.http1.DefaultHttp11ServerTransportListenerFactory;
 import 
org.apache.dubbo.rpc.protocol.tri.h12.http2.GenericHttp2ServerTransportListenerFactory;
 import org.apache.dubbo.rpc.protocol.tri.transport.TripleGoAwayHandler;
+import org.apache.dubbo.rpc.protocol.tri.transport.TripleHttp2SettingsHandler;
 import 
org.apache.dubbo.rpc.protocol.tri.transport.TripleServerConnectionHandler;
 import org.apache.dubbo.rpc.protocol.tri.transport.TripleTailHandler;
 import 
org.apache.dubbo.rpc.protocol.tri.websocket.DefaultWebSocketServerTransportListenerFactory;
@@ -95,6 +96,11 @@ public class TripleHttp2Protocol extends 
AbstractWireProtocol implements ScopeMo
         super.close();
     }
 
+    @Override
+    public boolean hasConnectionPreface() {
+        return true;
+    }
+
     @Override
     public void configClientPipeline(URL url, ChannelOperator operator, 
ContextOperator contextOperator) {
         TripleConfig tripleConfig = 
ConfigManager.getProtocolOrDefault(url).getTripleOrDefault();
@@ -116,6 +122,7 @@ public class TripleHttp2Protocol extends 
AbstractWireProtocol implements ScopeMo
         handlers.add(new ChannelHandlerPretender(new Http2MultiplexHandler(new 
ChannelDuplexHandler())));
         handlers.add(new ChannelHandlerPretender(new 
TriplePingPongHandler(UrlUtils.getCloseTimeout(url))));
         handlers.add(new ChannelHandlerPretender(new TripleGoAwayHandler()));
+        handlers.add(new ChannelHandlerPretender(new 
TripleHttp2SettingsHandler()));
         handlers.add(new ChannelHandlerPretender(new TripleTailHandler()));
         operator.configChannelHandler(handlers);
     }
diff --git 
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/transport/TripleHttp2SettingsHandler.java
 
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/transport/TripleHttp2SettingsHandler.java
new file mode 100644
index 0000000000..541c2fb915
--- /dev/null
+++ 
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/transport/TripleHttp2SettingsHandler.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.dubbo.rpc.protocol.tri.transport;
+
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.remoting.Constants;
+import org.apache.dubbo.remoting.api.connection.ConnectionHandler;
+
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.SimpleChannelInboundHandler;
+import io.netty.handler.codec.http2.Http2SettingsFrame;
+
+public class TripleHttp2SettingsHandler extends 
SimpleChannelInboundHandler<Http2SettingsFrame> {
+
+    private static final Logger logger = 
LoggerFactory.getLogger(TripleHttp2SettingsHandler.class);
+
+    public TripleHttp2SettingsHandler() {}
+
+    @Override
+    protected void channelRead0(ChannelHandlerContext ctx, Http2SettingsFrame 
msg) throws Exception {
+        final ConnectionHandler connectionHandler =
+                (ConnectionHandler) 
ctx.pipeline().get(Constants.CONNECTION_HANDLER_NAME);
+        if (logger.isDebugEnabled()) {
+            logger.debug("Receive Http2 Settings frame of " + 
ctx.channel().localAddress() + " -> "
+                    + ctx.channel().remoteAddress());
+        }
+        // Notify the connection preface is received.
+        connectionHandler.onConnectionPrefaceReceived(ctx.channel());
+    }
+}

Reply via email to