Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uds-stream: close unused sockets #243

Merged
merged 5 commits into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,33 @@ private void connect() throws IOException {
// We'd have better timeout support if we used Java 16's native Unix domain socket support (JEP 380)
delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, connectionTimeout);
}
if (!delegate.connect(address)) {
if (connectionTimeout > 0 && System.nanoTime() > deadline) {
throw new IOException("Connection timed out");
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense for this block to cover setOption for connectionTimeout above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

if (!delegate.connect(address)) {
if (connectionTimeout > 0 && System.nanoTime() > deadline) {
throw new IOException("Connection timed out");
}
if (!delegate.finishConnect()) {
throw new IOException("Connection failed");
}
}
if (!delegate.finishConnect()) {
throw new IOException("Connection failed");

delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, Math.max(timeout, 0));
if (bufferSize > 0) {
delegate.setOption(UnixSocketOptions.SO_SNDBUF, bufferSize);
}
} catch (Exception e) {
try {
delegate.close();
} catch (IOException __) {
// ignore
}
throw e;
}

if (timeout > 0) {
delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, timeout);
} else {
delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, 0);
}
if (bufferSize > 0) {
delegate.setOption(UnixSocketOptions.SO_SNDBUF, bufferSize);
}

this.delegate = delegate;
}

@Override
public void close() throws IOException {
disconnect();
Expand Down
Loading