From 29a97377fdc5a76d75ef5c7955dc7ded94c21240 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Tue, 5 Nov 2019 12:47:59 +0000 Subject: [PATCH 1/4] Make VertxHttpRequest a final class --- .../runtime/standalone/VertxHttpRequest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java index 7267f11313189..839a311771031 100644 --- a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java +++ b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java @@ -37,14 +37,14 @@ * @author Kristoffer Sjogren * @version $Revision: 1 $ */ -public class VertxHttpRequest extends BaseHttpRequest { - protected ResteasyHttpHeaders httpHeaders; - protected SynchronousDispatcher dispatcher; - protected String httpMethod; - protected String remoteHost; - protected InputStream inputStream; - protected Map attributes = new HashMap(); - protected VertxHttpResponse response; +public final class VertxHttpRequest extends BaseHttpRequest { + private ResteasyHttpHeaders httpHeaders; + private SynchronousDispatcher dispatcher; + private String httpMethod; + private String remoteHost; + private InputStream inputStream; + private Map attributes = new HashMap(); + private VertxHttpResponse response; private final boolean is100ContinueExpected; private VertxExecutionContext executionContext; private final Context context; From c69e180390c1616c4bdef0bf81c4b07262ce0cf2 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Tue, 5 Nov 2019 12:52:48 +0000 Subject: [PATCH 2/4] Performance: VertxHttpRequest should allocate attributes map lazily --- .../runtime/standalone/VertxHttpRequest.java | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java index 839a311771031..682fbcf34c86e 100644 --- a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java +++ b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.io.InputStream; +import java.util.Collections; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; @@ -43,7 +44,7 @@ public final class VertxHttpRequest extends BaseHttpRequest { private String httpMethod; private String remoteHost; private InputStream inputStream; - private Map attributes = new HashMap(); + private Map attributes; private VertxHttpResponse response; private final boolean is100ContinueExpected; private VertxExecutionContext executionContext; @@ -81,20 +82,25 @@ public void setHttpMethod(String method) { @Override public Enumeration getAttributeNames() { - Enumeration en = new Enumeration() { - private Iterator it = attributes.keySet().iterator(); - - @Override - public boolean hasMoreElements() { - return it.hasNext(); - } + final Map attributes = this.attributes; + if (attributes == null) { + return Collections.emptyEnumeration(); + } else { + Enumeration en = new Enumeration() { + private Iterator it = attributes.keySet().iterator(); + + @Override + public boolean hasMoreElements() { + return it.hasNext(); + } - @Override - public String nextElement() { - return it.next(); - } - }; - return en; + @Override + public String nextElement() { + return it.next(); + } + }; + return en; + } } @Override @@ -108,17 +114,22 @@ public boolean isFlushed() { @Override public Object getAttribute(String attribute) { - return attributes.get(attribute); + return attributes != null ? attributes.get(attribute) : null; } @Override public void setAttribute(String name, Object value) { + if (attributes == null) { + attributes = new HashMap(); + } attributes.put(name, value); } @Override public void removeAttribute(String name) { - attributes.remove(name); + if (attributes != null) { + attributes.remove(name); + } } @Override From d8c446723a8c0ea3a7700eaa42cdf665071b33e8 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Tue, 5 Nov 2019 13:31:38 +0000 Subject: [PATCH 3/4] Performance: try resolve the remote Host address lazily --- .../runtime/standalone/VertxHttpRequest.java | 9 +++++---- .../runtime/standalone/VertxRequestHandler.java | 14 +++++++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java index 682fbcf34c86e..5a8c3199fad94 100644 --- a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java +++ b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java @@ -9,6 +9,7 @@ import java.util.Iterator; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; import javax.ws.rs.ServiceUnavailableException; import javax.ws.rs.container.AsyncResponse; @@ -42,7 +43,7 @@ public final class VertxHttpRequest extends BaseHttpRequest { private ResteasyHttpHeaders httpHeaders; private SynchronousDispatcher dispatcher; private String httpMethod; - private String remoteHost; + private Supplier remoteHost; private InputStream inputStream; private Map attributes; private VertxHttpResponse response; @@ -55,7 +56,7 @@ public VertxHttpRequest(Context context, ResteasyHttpHeaders httpHeaders, ResteasyUriInfo uri, String httpMethod, - String remoteHost, + Supplier remoteHost, SynchronousDispatcher dispatcher, VertxHttpResponse response, boolean is100ContinueExpected) { @@ -139,12 +140,12 @@ public HttpHeaders getHttpHeaders() { @Override public String getRemoteHost() { - return remoteHost; + return remoteHost.get(); } @Override public String getRemoteAddress() { - return remoteHost; + return remoteHost.get(); } @Override diff --git a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxRequestHandler.java b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxRequestHandler.java index a73732dd94a6c..73731a156b5f1 100644 --- a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxRequestHandler.java +++ b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxRequestHandler.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.io.InputStream; +import java.util.function.Supplier; import javax.enterprise.inject.Instance; import javax.enterprise.inject.spi.CDI; @@ -96,12 +97,15 @@ private void dispatch(RoutingContext routingContext, InputStream is, VertxOutput HttpServerResponse response = request.response(); VertxHttpResponse vertxResponse = new VertxHttpResponse(request, dispatcher.getProviderFactory(), request.method(), allocator, output); - // client address may not be available with VirtualHttp - SocketAddress socketAddress = request.remoteAddress(); - String host = socketAddress != null ? socketAddress.host() : null; + // client address may not be available with VirtualHttp; + // using a supplier to make the remote Address resolution lazy: often it's not needed. + Supplier hostNameProvider = () -> { + SocketAddress socketAddress = request.remoteAddress(); + String host = socketAddress != null ? socketAddress.host() : null; + return host; + }; - VertxHttpRequest vertxRequest = new VertxHttpRequest(ctx, headers, uriInfo, request.rawMethod(), - host, dispatcher.getDispatcher(), vertxResponse, false); + VertxHttpRequest vertxRequest = new VertxHttpRequest(ctx, headers, uriInfo, request.rawMethod(), hostNameProvider, dispatcher.getDispatcher(), vertxResponse, false); vertxRequest.setInputStream(is); try { ResteasyContext.pushContext(SecurityContext.class, new QuarkusResteasySecurityContext(request)); From f1242b6c45f5f0cc37f8edef9d1c43543ca8a7e6 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Tue, 5 Nov 2019 13:35:52 +0000 Subject: [PATCH 4/4] Remove some dead code --- .../runtime/standalone/VertxHttpRequest.java | 17 +---------------- .../runtime/standalone/VertxRequestHandler.java | 3 ++- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java index 5a8c3199fad94..957fac534e2d7 100644 --- a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java +++ b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxHttpRequest.java @@ -41,16 +41,13 @@ */ public final class VertxHttpRequest extends BaseHttpRequest { private ResteasyHttpHeaders httpHeaders; - private SynchronousDispatcher dispatcher; private String httpMethod; private Supplier remoteHost; private InputStream inputStream; private Map attributes; private VertxHttpResponse response; - private final boolean is100ContinueExpected; private VertxExecutionContext executionContext; private final Context context; - private volatile boolean flushed; public VertxHttpRequest(Context context, ResteasyHttpHeaders httpHeaders, @@ -58,13 +55,10 @@ public VertxHttpRequest(Context context, String httpMethod, Supplier remoteHost, SynchronousDispatcher dispatcher, - VertxHttpResponse response, - boolean is100ContinueExpected) { + VertxHttpResponse response) { super(uri); this.context = context; - this.is100ContinueExpected = is100ContinueExpected; this.response = response; - this.dispatcher = dispatcher; this.httpHeaders = httpHeaders; this.httpMethod = httpMethod; this.remoteHost = remoteHost; @@ -109,10 +103,6 @@ public ResteasyAsynchronousContext getAsyncContext() { return executionContext; } - public boolean isFlushed() { - return flushed; - } - @Override public Object getAttribute(String attribute) { return attributes != null ? attributes.get(attribute) : null; @@ -167,10 +157,6 @@ public VertxHttpResponse getResponse() { return response; } - public boolean is100ContinueExpected() { - return is100ContinueExpected; - } - @Override public void forward(String path) { throw new NotImplementedYetException(); @@ -321,7 +307,6 @@ public boolean cancel(int retryAfter) { } protected synchronized void vertxFlush() { - flushed = true; try { vertxResponse.finish(); } catch (IOException e) { diff --git a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxRequestHandler.java b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxRequestHandler.java index 73731a156b5f1..38a6df6c13c8a 100644 --- a/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxRequestHandler.java +++ b/extensions/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/standalone/VertxRequestHandler.java @@ -105,7 +105,8 @@ private void dispatch(RoutingContext routingContext, InputStream is, VertxOutput return host; }; - VertxHttpRequest vertxRequest = new VertxHttpRequest(ctx, headers, uriInfo, request.rawMethod(), hostNameProvider, dispatcher.getDispatcher(), vertxResponse, false); + VertxHttpRequest vertxRequest = new VertxHttpRequest(ctx, headers, uriInfo, request.rawMethod(), hostNameProvider, + dispatcher.getDispatcher(), vertxResponse); vertxRequest.setInputStream(is); try { ResteasyContext.pushContext(SecurityContext.class, new QuarkusResteasySecurityContext(request));