Skip to content

Commit f9b0644

Browse files
authored
Merge pull request #243 from DataDog/corentin.chary/unix-stream
2 parents c18559c + c59bcdb commit f9b0644

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,26 +134,33 @@ private void connect() throws IOException {
134134
// We'd have better timeout support if we used Java 16's native Unix domain socket support (JEP 380)
135135
delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, connectionTimeout);
136136
}
137-
if (!delegate.connect(address)) {
138-
if (connectionTimeout > 0 && System.nanoTime() > deadline) {
139-
throw new IOException("Connection timed out");
137+
try {
138+
if (!delegate.connect(address)) {
139+
if (connectionTimeout > 0 && System.nanoTime() > deadline) {
140+
throw new IOException("Connection timed out");
141+
}
142+
if (!delegate.finishConnect()) {
143+
throw new IOException("Connection failed");
144+
}
140145
}
141-
if (!delegate.finishConnect()) {
142-
throw new IOException("Connection failed");
146+
147+
delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, Math.max(timeout, 0));
148+
if (bufferSize > 0) {
149+
delegate.setOption(UnixSocketOptions.SO_SNDBUF, bufferSize);
143150
}
151+
} catch (Exception e) {
152+
try {
153+
delegate.close();
154+
} catch (IOException __) {
155+
// ignore
156+
}
157+
throw e;
144158
}
145159

146-
if (timeout > 0) {
147-
delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, timeout);
148-
} else {
149-
delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, 0);
150-
}
151-
if (bufferSize > 0) {
152-
delegate.setOption(UnixSocketOptions.SO_SNDBUF, bufferSize);
153-
}
160+
154161
this.delegate = delegate;
155162
}
156-
163+
157164
@Override
158165
public void close() throws IOException {
159166
disconnect();

0 commit comments

Comments
 (0)