Skip to content

Commit 062e473

Browse files
committed
Debug edits
1 parent 7915991 commit 062e473

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -137,36 +137,48 @@ private void connect() throws IOException {
137137
if (VersionUtils.isJavaVersionAtLeast(16) && enableJdkSocket) {
138138
try {
139139
// Use reflection to avoid compiling Java 16+ classes in incompatible versions
140-
Class<?> protocolFamilyClass = Class.forName("java.net.StandardProtocolFamily");
141-
Object unixProtocol = Enum.valueOf((Class<Enum>) protocolFamilyClass, "UNIX");
142-
// Explicitly set StandardProtocolFamily.UNIX so that the socket uses the UDS protocol
140+
Class<?> protocolFamilyClass = Class.forName("java.net.ProtocolFamily");
141+
Class<?> standardProtocolFamilyClass = Class.forName("java.net.StandardProtocolFamily");
142+
Object unixProtocol = Enum.valueOf((Class<Enum>) standardProtocolFamilyClass, "UNIX");
143143
Method openMethod = SocketChannel.class.getMethod("open", protocolFamilyClass);
144-
// Open the socketchannel with the UDS protocol
144+
// Open a socketchannel with Unix Domain Socket protocol family
145145
SocketChannel channel = (SocketChannel) openMethod.invoke(null, unixProtocol);
146146

147-
if (connectionTimeout > 0) {
148-
channel.socket().setSoTimeout(connectionTimeout);
149-
}
147+
// if (connectionTimeout > 0) {
148+
// channel.socket().setSoTimeout(connectionTimeout);
149+
// }
150+
channel.configureBlocking(true);
151+
150152
try {
151153
System.out.println("========== Native UDS connect address: " + address);
152154
System.out.println("========== Native UDS connect address type: " + address.getClass().getName());
155+
156+
SocketAddress connectAddress = address;
157+
if (address instanceof UnixSocketAddressWithTransport) {
158+
connectAddress = ((UnixSocketAddressWithTransport) address).getAddress();
159+
System.out.println("========== Unwrapped address: " + connectAddress);
160+
System.out.println("========== Unwrapped address type: " + connectAddress.getClass().getName());
161+
}
162+
153163
Method connectMethod = SocketChannel.class.getMethod("connect", SocketAddress.class);
154-
boolean connected = (boolean) connectMethod.invoke(channel, address);
155-
// socketchannel is failing to connect here :(
164+
boolean connected = (boolean) connectMethod.invoke(channel, connectAddress);
156165
if (!connected) {
157-
if (connectionTimeout > 0 && System.nanoTime() > deadline) {
158-
throw new IOException("Connection timed out");
159-
}
160-
if (!channel.finishConnect()) {
161-
throw new IOException("Connection failed");
162-
}
166+
// if (connectionTimeout > 0 && System.nanoTime() > deadline) {
167+
// throw new IOException("Connection timed out");
168+
// }
169+
// if (!channel.finishConnect()) {
170+
// throw new IOException("Connection failed");
171+
// }
172+
throw new IOException("Connection failed");
163173
}
164174
System.out.println("========== Connection successful");
165-
channel.socket().setSoTimeout(Math.max(timeout, 0));
166-
if (bufferSize > 0) {
167-
channel.socket().setSendBufferSize(bufferSize);
168-
}
175+
// channel.socket().setSoTimeout(Math.max(timeout, 0));
176+
// if (bufferSize > 0) {
177+
// channel.socket().setSendBufferSize(bufferSize);
178+
// }
169179
} catch (Exception e) {
180+
System.out.println("========== Native UDS connection failed with exception: " + e.getClass().getName() + ": " + e.getMessage());
181+
e.printStackTrace();
170182
try {
171183
channel.close();
172184
} catch (IOException __) {
@@ -178,6 +190,8 @@ private void connect() throws IOException {
178190
this.delegate = channel;
179191
return;
180192
} catch (Exception e) {
193+
System.out.println("========== Native UDS implementation failed with outer exception: " + e.getClass().getName() + ": " + e.getMessage());
194+
e.printStackTrace();
181195
throw new IOException("Failed to create UnixStreamClientChannel for native UDS implementation", e);
182196
}
183197
}

0 commit comments

Comments
 (0)