Skip to content

Commit 8713073

Browse files
committed
Improve ExternalProxyVerticle. Get rid of thread dumping because unhelpful.
1 parent 1b98a17 commit 8713073

File tree

2 files changed

+44
-35
lines changed

2 files changed

+44
-35
lines changed

java-components/domain-proxy/common/src/main/java/com/redhat/hacbs/domainproxy/common/CommonIOUtil.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.nio.channels.Selector;
99
import java.nio.channels.SocketChannel;
1010
import java.util.Iterator;
11-
import java.util.Map;
1211

1312
import org.jboss.logging.Logger;
1413

@@ -38,7 +37,6 @@ public static Runnable channelToChannelBiDirectionalHandler(final int byteBuffer
3837
final SocketChannel rightChannel) {
3938
return () -> {
4039
currentThread().setName("channelToChannelHandler");
41-
CommonIOUtil.threadDump();
4240
LOG.info("Connections opened");
4341
final String leftChannelName = getChannelName(leftChannel);
4442
final String rightChannelName = getChannelName(rightChannel);
@@ -162,22 +160,4 @@ private static String getChannelName(final SocketChannel channel) {
162160
}
163161
return channelName;
164162
}
165-
166-
public static void threadDump() {
167-
LOG.info("Before thread dump");
168-
String threadDumpStr = "";
169-
Map<Thread, StackTraceElement[]> threadMap = Thread.getAllStackTraces();
170-
LOG.info("Thread dump count: " + threadMap.size());
171-
for (Map.Entry<Thread, StackTraceElement[]> entry : threadMap.entrySet()) {
172-
Thread thread = entry.getKey();
173-
StackTraceElement[] stackTrace = entry.getValue();
174-
threadDumpStr += "Thread: " + thread.getName() + " [ID: " + thread.getId() + "]\n";
175-
threadDumpStr += "Thread Type: " + (thread.isVirtual() ? "Virtual" : "Platform") + "\n";
176-
for (StackTraceElement ste : stackTrace) {
177-
threadDumpStr += "\t" + ste + "\n";
178-
}
179-
}
180-
LOG.info(threadDumpStr);
181-
LOG.info("After thread dump");
182-
}
183163
}

java-components/domain-proxy/server/src/main/java/com/redhat/hacbs/domainproxy/server/ExternalProxyVerticle.java

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.redhat.hacbs.domainproxy.server;
22

3+
import static com.redhat.hacbs.domainproxy.common.CommonIOUtil.TIMEOUT_MS;
4+
35
import java.util.Set;
46
import java.util.concurrent.atomic.AtomicInteger;
57

@@ -48,8 +50,15 @@ public class ExternalProxyVerticle extends AbstractVerticle {
4850
private AtomicInteger counter = new AtomicInteger(0);
4951

5052
public ExternalProxyVerticle(final Vertx vertx) {
51-
webClient = WebClient.create(vertx, new WebClientOptions());
52-
netClient = vertx.createNetClient(new NetClientOptions());
53+
webClient = WebClient.create(vertx, new WebClientOptions()
54+
.setMaxPoolSize(50)
55+
.setKeepAlive(true)
56+
.setIdleTimeout(60)
57+
.setConnectTimeout(TIMEOUT_MS));
58+
netClient = vertx.createNetClient(new NetClientOptions()
59+
.setReuseAddress(true)
60+
.setIdleTimeout(60)
61+
.setConnectTimeout(TIMEOUT_MS));
5362
httpServer = vertx.createHttpServer();
5463
}
5564

@@ -76,11 +85,13 @@ public void start() {
7685

7786
private void handleGetRequest(final HttpServerRequest request) {
7887
Log.info("Handling HTTP GET Request");
79-
counter.incrementAndGet();
80-
Log.infof("Request no: %d", counter.get());
88+
final int requestNo = counter.incrementAndGet();
89+
Log.infof("Request no: %d", requestNo);
8190
if (isTargetWhitelisted(request.authority().host(), request)) {
8291
Log.infof("Target URI %s", request.uri());
92+
final long startTime = System.currentTimeMillis();
8393
webClient.getAbs(request.uri()).send(asyncResult -> {
94+
Log.infof("Request %d took %d ms", requestNo, System.currentTimeMillis() - startTime);
8495
if (asyncResult.succeeded()) {
8596
final HttpResponse<Buffer> response = asyncResult.result();
8697
if (response.statusCode() != HttpResponseStatus.OK.code()) {
@@ -92,26 +103,26 @@ private void handleGetRequest(final HttpServerRequest request) {
92103
.headers().addAll(response.headers());
93104
request.response().end(response.body());
94105
} else {
95-
Log.errorf(asyncResult.cause(), "Failed to get response");
96-
request.response()
97-
.setStatusCode(HttpResponseStatus.BAD_GATEWAY.code())
98-
.setStatusMessage(HttpResponseStatus.BAD_GATEWAY.reasonPhrase())
99-
.end("The server received an invalid response from the upstream server.");
106+
handleErrorResponse(request, asyncResult.cause(), "Failed to get response");
100107
}
101108
});
102109
}
103110
}
104111

105112
private void handleConnectRequest(final HttpServerRequest request) {
106113
Log.info("Handling HTTPS CONNECT request");
114+
final int requestNo = counter.incrementAndGet();
115+
Log.infof("Request no: %d", requestNo);
107116
final String targetHost = request.authority().host();
108117
if (isTargetWhitelisted(targetHost, request)) {
109118
Log.infof("Target URI %s", request.uri());
110119
int targetPort = request.authority().port();
111120
if (targetPort == -1) {
112121
targetPort = HTTPS_PORT;
113122
}
123+
final long startTime = System.currentTimeMillis();
114124
netClient.connect(targetPort, targetHost, targetConnect -> {
125+
Log.infof("Request %d took %d ms", requestNo, System.currentTimeMillis() - startTime);
115126
if (targetConnect.succeeded()) {
116127
final NetSocket targetSocket = targetConnect.result();
117128
request.toNetSocket().onComplete(sourceConnect -> {
@@ -122,15 +133,11 @@ private void handleConnectRequest(final HttpServerRequest request) {
122133
sourceSocket.closeHandler(v -> targetSocket.close());
123134
targetSocket.closeHandler(v -> sourceSocket.close());
124135
} else {
125-
Log.errorf(sourceConnect.cause(), "Failed to connect to source");
136+
handleErrorResponse(request, sourceConnect.cause(), "Failed to connect to source");
126137
}
127138
});
128139
} else {
129-
Log.errorf(targetConnect.cause(), "Failed to connect to target");
130-
request.response()
131-
.setStatusCode(HttpResponseStatus.BAD_GATEWAY.code())
132-
.setStatusMessage(HttpResponseStatus.BAD_GATEWAY.reasonPhrase())
133-
.end("The server received an invalid response from the upstream server.");
140+
handleErrorResponse(request, targetConnect.cause(), "Failed to connect to target");
134141
}
135142
});
136143
}
@@ -148,4 +155,26 @@ private boolean isTargetWhitelisted(final String targetHost, final HttpServerReq
148155
}
149156
return true;
150157
}
158+
159+
private void handleErrorResponse(final HttpServerRequest request, final Throwable cause, final String message) {
160+
Log.errorf(cause, message);
161+
request.response()
162+
.setStatusCode(HttpResponseStatus.BAD_GATEWAY.code())
163+
.setStatusMessage(HttpResponseStatus.BAD_GATEWAY.reasonPhrase())
164+
.end(message + ": " + cause.getMessage());
165+
}
166+
167+
@Override
168+
public void stop() {
169+
Log.info("Shutting down domain proxy server...");
170+
webClient.close();
171+
netClient.close();
172+
httpServer.close(ar -> {
173+
if (ar.succeeded()) {
174+
Log.info("Server shut down successfully.");
175+
} else {
176+
Log.errorf(ar.cause(), "Server shutdown failed");
177+
}
178+
});
179+
}
151180
}

0 commit comments

Comments
 (0)