Skip to content

Commit ff47a6b

Browse files
committed
Improve logging. Improve connection closing. Exit upon failure to bind.
1 parent 37d32dd commit ff47a6b

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

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

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public static Runnable channelToChannelBiDirectionalHandler(final int byteBuffer
3838
return () -> {
3939
currentThread().setName("channelToChannelHandler");
4040
LOG.info("Connections opened");
41+
final String leftChannelName = getChannelName(leftChannel);
42+
final String rightChannelName = getChannelName(rightChannel);
4143
int bytesReadLeft = 0;
4244
int bytesReadRight = 0;
4345
int bytesWrittenLeft = 0;
@@ -46,8 +48,8 @@ public static Runnable channelToChannelBiDirectionalHandler(final int byteBuffer
4648
try (final Selector selector = Selector.open()) {
4749
leftChannel.configureBlocking(false);
4850
rightChannel.configureBlocking(false);
49-
leftChannel.register(selector, SelectionKey.OP_READ);
50-
rightChannel.register(selector, SelectionKey.OP_WRITE);
51+
leftChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
52+
rightChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
5153
long bytesTransferredTime = System.currentTimeMillis();
5254
while (leftChannel.isOpen() && rightChannel.isOpen()) {
5355
if (selector.selectNow() > 0) {
@@ -89,20 +91,14 @@ public static Runnable channelToChannelBiDirectionalHandler(final int byteBuffer
8991
LOG.errorf(e, "Error in bi-directional channel handling");
9092
} finally {
9193
closeConnections(leftChannel, rightChannel);
92-
try {
93-
final String leftChannelName = leftChannel.getRemoteAddress().getClass().getSimpleName();
94-
final String rightChannelName = rightChannel.getRemoteAddress().getClass().getSimpleName();
95-
LOG.infof("Read %d total bytes from % channel to %s channel", leftChannelName, rightChannelName,
96-
bytesReadLeft);
97-
LOG.infof("Read %d total bytes from % channel to %s channel", rightChannelName, leftChannelName,
98-
bytesReadRight);
99-
LOG.infof("Wrote %d total bytes from % channel to %s channel", leftChannelName, rightChannelName,
100-
bytesWrittenLeft);
101-
LOG.infof("Wrote %d total bytes from % channel to %s channel", rightChannelName, leftChannelName,
102-
bytesWrittenRight);
103-
} catch (IOException e) {
104-
throw new RuntimeException(e);
105-
}
94+
LOG.infof("Read %d total bytes from %s channel to %s channel", bytesReadLeft, leftChannelName,
95+
rightChannelName);
96+
LOG.infof("Read %d total bytes from %s channel to %s channel", bytesReadRight, rightChannelName,
97+
leftChannelName);
98+
LOG.infof("Wrote %d total bytes from %s channel to %s channel", bytesWrittenLeft, leftChannelName,
99+
rightChannelName);
100+
LOG.infof("Wrote %d total bytes from %s channel to %s channel", bytesWrittenRight, rightChannelName,
101+
leftChannelName);
106102
LOG.infof("Read %d total bytes between channels overall", bytesReadLeft + bytesReadRight);
107103
LOG.infof("Wrote %d total bytes between channels overall", bytesWrittenLeft + bytesWrittenRight);
108104
}
@@ -125,31 +121,43 @@ private static int transferData(final SocketChannel sourceChannel, final SocketC
125121
return bytesTransferred;
126122
}
127123

128-
private static void closeConnections(final SocketChannel sourceChannel, final SocketChannel destinationChannel) {
129-
try {
130-
if (sourceChannel != null && sourceChannel.isOpen()) {
131-
sourceChannel.close();
132-
}
133-
} catch (final IOException e) {
134-
LOG.errorf(e, "Error closing source channel");
124+
private static void closeConnections(final SocketChannel leftChannel, final SocketChannel rightChannel) {
125+
if (leftChannel != null) {
126+
closeConnection(leftChannel);
135127
}
128+
if (rightChannel != null) {
129+
closeConnection(rightChannel);
130+
}
131+
LOG.info("Connections closed");
132+
}
133+
134+
private static void closeConnection(final SocketChannel channel) {
136135
try {
137-
if (destinationChannel != null && destinationChannel.isOpen()) {
138-
destinationChannel.close();
136+
if (channel.isOpen()) {
137+
channel.close();
139138
}
140139
} catch (final IOException e) {
141-
LOG.errorf(e, "Error closing destination channel");
140+
LOG.errorf(e, "Error closing %s channel", getChannelName(channel));
142141
}
143-
LOG.info("Connections closed");
144142
}
145143

146144
private static void logBytes(final SocketChannel sourceChannel, final SocketChannel destinationChannel,
147145
final Operation operation,
148-
final int bytesTransferred) throws IOException {
146+
final int bytesTransferred) {
149147
if (bytesTransferred > 0) {
150148
LOG.infof("%s %d bytes from %s channel to %s channel", operation.descriptor, bytesTransferred,
151-
sourceChannel.getRemoteAddress().getClass().getSimpleName(),
152-
destinationChannel.getRemoteAddress().getClass().getSimpleName());
149+
getChannelName(sourceChannel),
150+
getChannelName(destinationChannel));
151+
}
152+
}
153+
154+
private static String getChannelName(final SocketChannel channel) {
155+
String channelName = "unknown";
156+
try {
157+
channelName = channel.getRemoteAddress().getClass().getSimpleName();
158+
} catch (final IOException e) {
159+
LOG.errorf(e, "Error getting channel name");
153160
}
161+
return channelName;
154162
}
155163
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import io.netty.handler.codec.http.HttpResponseStatus;
1111
import io.quarkus.logging.Log;
12+
import io.quarkus.runtime.Quarkus;
1213
import io.vertx.core.AbstractVerticle;
1314
import io.vertx.core.Vertx;
1415
import io.vertx.core.buffer.Buffer;
@@ -65,6 +66,7 @@ public void start() {
6566
Log.infof("Server is now listening on port %d", httpServerPort);
6667
} else {
6768
Log.errorf(result.cause(), "Failed to bind server");
69+
Quarkus.asyncExit();
6870
}
6971
});
7072
}

0 commit comments

Comments
 (0)