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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new b8b98d81a4f5 CAMEL-27172: camel-netty - requestTimeout applies to 
every request on a channel used without a borrow from the producer pool (#30355)
b8b98d81a4f5 is described below

commit b8b98d81a4f57f0d4149f92bb654bb83aca9dc41
Author: allthingssecurity <[email protected]>
AuthorDate: Thu Nov 19 15:09:49 2026 +0530

    CAMEL-27172: camel-netty - requestTimeout applies to every request on a 
channel used without a borrow from the producer pool (#30355)
    
    The reply removes the ReadTimeoutHandler from the channel, and only a 
borrow from
    the producer pool added it again, so with producerPoolEnabled=false or
    reuseChannel=true the requests after the first one on a channel had no 
timeout
    and waited for ever for a server that did not answer. The producer now adds 
the
    configured requestTimeout when it sends a sync request on a channel without 
it.
    
    CAMEL-27172: camel-netty - address review: drop a redundant assertNotNull 
in the test
    
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/component/netty/NettyProducer.java       | 13 ++++
 .../NettyRequestTimeoutReusedChannelTest.java      | 88 ++++++++++++++++++++++
 2 files changed, 101 insertions(+)

diff --git 
a/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
 
b/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
index d1ed0aaf0ab4..861f62b80568 100644
--- 
a/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
+++ 
b/components/camel-netty/src/main/java/org/apache/camel/component/netty/NettyProducer.java
@@ -337,6 +337,19 @@ public class NettyProducer extends DefaultAsyncProducer {
             } else {
                 channel.pipeline().replace(oldHandler, "timeout", newHandler);
             }
+        } else if (configuration.isSync() && configuration.getRequestTimeout() 
> 0
+                && channel.pipeline().get("timeout") == null) {
+            // the reply to the previous request on this channel removed the 
timeout (ClientChannelHandler), and only a
+            // borrow from the producer pool adds it again (activateObject): a 
channel that is used again without a
+            // borrow (producerPoolEnabled=false, or reuseChannel=true) needs 
it added here, or the request never times out
+            LOG.trace("Adding the request timeout of {} millis to channel: 
{}", configuration.getRequestTimeout(), channel);
+            try {
+                channel.pipeline().addBefore("handler", "timeout",
+                        new 
ReadTimeoutHandler(configuration.getRequestTimeout(), TimeUnit.MILLISECONDS));
+            } catch (IllegalArgumentException e) {
+                // another request on this shared channel 
(producerPoolEnabled=false) added it meanwhile
+                LOG.trace("The request timeout was added to channel: {} 
meanwhile", channel);
+            }
         }
 
         //This will refer to original callback since netty will release body 
by itself
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyRequestTimeoutReusedChannelTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyRequestTimeoutReusedChannelTest.java
new file mode 100644
index 000000000000..7d3475e77c02
--- /dev/null
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyRequestTimeoutReusedChannelTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.camel.component.netty;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import io.netty.handler.timeout.ReadTimeoutException;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.Endpoint;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.camel.test.junit6.TestSupport.assertIsInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * The requestTimeout applies to every request on a channel that is used again 
without a borrow from the producer pool:
+ * with producerPoolEnabled=false, and with reuseChannel=true for the second 
call in the same exchange.
+ */
+class NettyRequestTimeoutReusedChannelTest extends BaseNettyTest {
+
+    // the server answers a slow request when the test ends, or after 5 seconds
+    private final CountDownLatch slowReply = new CountDownLatch(1);
+
+    @AfterEach
+    void releaseServer() {
+        slowReply.countDown();
+    }
+
+    @Test
+    void testRequestTimeoutWithoutProducerPool() {
+        Endpoint endpoint = resolveMandatoryEndpoint(
+                
"netty:tcp://localhost:{{port}}?textline=true&sync=true&requestTimeout=200&producerPoolEnabled=false");
+
+        assertEquals("Bye World", template.requestBody(endpoint, "Hello", 
String.class));
+        // the same channel is used again: the request must time out
+        assertTimeout(() -> template.requestBody(endpoint, "Hello Camel", 
String.class));
+    }
+
+    @Test
+    void testRequestTimeoutWithReuseChannel() {
+        assertTimeout(() -> template.requestBody("direct:reuse", "Hello", 
String.class));
+    }
+
+    private static void assertTimeout(Runnable request) {
+        CamelExecutionException e = 
assertThrows(CamelExecutionException.class, request::run);
+        assertIsInstanceOf(ReadTimeoutException.class, e.getCause());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("netty:tcp://localhost:{{port}}?textline=true&sync=true")
+                        .process(exchange -> {
+                            if 
(exchange.getIn().getBody(String.class).contains("Camel")) {
+                                slowReply.await(5, TimeUnit.SECONDS);
+                            }
+                        })
+                        .transform().constant("Bye World");
+
+                // two calls in the same exchange on the same channel
+                from("direct:reuse")
+                        
.to("netty:tcp://localhost:{{port}}?textline=true&sync=true&requestTimeout=200&reuseChannel=true")
+                        .setBody(constant("Hello Camel"))
+                        
.to("netty:tcp://localhost:{{port}}?textline=true&sync=true&requestTimeout=200&reuseChannel=true");
+            }
+        };
+    }
+}

Reply via email to