You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by ff...@apache.org on 2021/01/07 19:17:35 UTC

[cxf] branch 3.4.x-fixes updated: [CXF-8405]add RequestLimitingHandler for http-undertow transport

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

ffang pushed a commit to branch 3.4.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.4.x-fixes by this push:
     new 22e5008  [CXF-8405]add RequestLimitingHandler for http-undertow transport
22e5008 is described below

commit 22e5008312bf9024089c3148747940e434d641a0
Author: Freeman Fang <fr...@gmail.com>
AuthorDate: Thu Jan 7 14:13:00 2021 -0500

    [CXF-8405]add RequestLimitingHandler for http-undertow transport
    
    (cherry picked from commit d19a801b7673d2b0b7f2e657e63b8450ea04c8d4)
---
 .../handlers/CxfRequestLimitingHandler.java        | 83 ++++++++++++++++++++++
 .../http_undertow/UndertowBasicAuthTest.java       | 36 ++++++++++
 .../http_undertow/undertowBasicAuthServer.xml      |  4 ++
 3 files changed, 123 insertions(+)

diff --git a/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfRequestLimitingHandler.java b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfRequestLimitingHandler.java
new file mode 100644
index 0000000..af1dbfb
--- /dev/null
+++ b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/handlers/CxfRequestLimitingHandler.java
@@ -0,0 +1,83 @@
+/**
+ * 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.cxf.transport.http_undertow.handlers;
+
+import org.apache.cxf.transport.http_undertow.CXFUndertowHttpHandler;
+
+import io.undertow.server.HttpHandler;
+import io.undertow.server.HttpServerExchange;
+import io.undertow.server.handlers.RequestLimitingHandler;
+
+public class CxfRequestLimitingHandler implements CXFUndertowHttpHandler {
+    
+    private HttpHandler next;
+    private RequestLimitingHandler requestLimitingHandler;
+    private int maximumConcurrentRequests; 
+    private int queueSize;
+    
+    public CxfRequestLimitingHandler(int maximumConcurrentRequests, int queueSize) {
+        this.setMaximumConcurrentRequests(maximumConcurrentRequests);
+        this.setQueueSize(queueSize);
+    }
+    
+    public CxfRequestLimitingHandler() {
+        this.setMaximumConcurrentRequests(1);
+        this.setQueueSize(1);
+    }
+
+    @Override
+    public void handleRequest(HttpServerExchange exchange) throws Exception {
+        
+        if (requestLimitingHandler == null) {
+            buildLogHandler();
+        }
+        this.requestLimitingHandler.handleRequest(exchange);
+    }
+
+    @Override
+    public void setNext(HttpHandler nextHandler) {
+        this.next = nextHandler;
+        
+    }
+    
+    private void buildLogHandler() {
+        HttpHandler handler = this.next;
+        this.requestLimitingHandler = 
+            new RequestLimitingHandler(getMaximumConcurrentRequests(), getQueueSize(), handler);
+
+    }
+
+    public int getMaximumConcurrentRequests() {
+        return maximumConcurrentRequests;
+    }
+
+    public void setMaximumConcurrentRequests(int maximumConcurrentRequests) {
+        this.maximumConcurrentRequests = maximumConcurrentRequests;
+    }
+
+    public int getQueueSize() {
+        return queueSize;
+    }
+
+    public void setQueueSize(int queueSize) {
+        this.queueSize = queueSize;
+    }
+    
+}
+
diff --git a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java b/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java
index 27e9c40..5919ce1 100644
--- a/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java
+++ b/systests/transport-undertow/src/test/java/org/apache/cxf/systest/http_undertow/UndertowBasicAuthTest.java
@@ -21,6 +21,9 @@ package org.apache.cxf.systest.http_undertow;
 
 import java.io.File;
 import java.net.URL;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 import javax.xml.namespace.QName;
 import javax.xml.ws.BindingProvider;
@@ -87,6 +90,19 @@ public class UndertowBasicAuthTest extends AbstractClientServerTestBase {
     }
 
     @org.junit.Test
+    public void testRequestLimitHandler() throws Exception {
+        CountDownLatch latch = new CountDownLatch(50); 
+
+        ExecutorService executor = Executors.newFixedThreadPool(200); 
+
+        for (int i = 0; i < 50; i++) {
+            executor.execute(new SendRequest(latch)); 
+        }
+        latch.await();
+        
+    }
+    
+    @org.junit.Test
     public void testGetWSDL() throws Exception {
         BusFactory bf = BusFactory.newInstance();
         Bus bus = bf.createBus();
@@ -110,4 +126,24 @@ public class UndertowBasicAuthTest extends AbstractClientServerTestBase {
             c.setAuthorization(authorizationPolicy);
         }
     }
+    
+    class SendRequest implements Runnable {
+        private CountDownLatch latch;
+
+        SendRequest(CountDownLatch latch) {
+            this.latch = latch;
+        }
+
+        public void run() {
+            try {
+                assertEquals("Hello Request Limit", greeter.greetMe("Request Limit"));
+            } catch (Throwable ex) {
+                //some requests are expected to fail and receive 503 error
+                //cause Server side limit the concurrent request
+                assertTrue(ex.getCause().getMessage().contains("503: Service Unavailable"));
+            } finally {
+                latch.countDown();
+            }
+        }
+    }
 }
diff --git a/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml b/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml
index 154020f..d3be533 100644
--- a/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml
+++ b/systests/transport-undertow/src/test/resources/org/apache/cxf/systest/http_undertow/undertowBasicAuthServer.xml
@@ -26,6 +26,10 @@
         <httpu:engine port="${testutil.ports.UndertowBasicAuthServer}">
             <httpu:handlers>
                 <bean class="org.apache.cxf.systest.http_undertow.UndertowBasicAuthHandler"/>
+                <bean class="org.apache.cxf.transport.http_undertow.handlers.CxfRequestLimitingHandler">
+                    <property name="maximumConcurrentRequests" value="1" />
+                    <property name="queueSize" value="1"/>
+                </bean>
                 <bean class="org.apache.cxf.systest.http_undertow.LogHandler"/>
             </httpu:handlers>
         </httpu:engine>