Skip to content

Commit d50bb85

Browse files
committed
Add builder method
1 parent 95843b6 commit d50bb85

3 files changed

Lines changed: 26 additions & 17 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.timgroup.statsd;
22

3+
import jnr.constants.platform.Sock;
34
import jnr.unixsocket.UnixSocketAddress;
45

56
import java.net.InetAddress;
@@ -132,6 +133,25 @@ public NonBlockingStatsDClientBuilder namedPipe(String val) {
132133
return this;
133134
}
134135

136+
private Callable<SocketAddress> socketLookup(final String path, final UnixSocketAddressWithTransport.TransportType transport) {
137+
return new Callable<SocketAddress>() {
138+
@Override
139+
public SocketAddress call() throws Exception {
140+
return new UnixSocketAddressWithTransport(new UnixSocketAddress(path), transport);
141+
}
142+
};
143+
}
144+
145+
public NonBlockingStatsDClientBuilder socket(final String path, final UnixSocketAddressWithTransport.TransportType transport) {
146+
addressLookup = socketLookup(path, transport);
147+
return this;
148+
}
149+
150+
public NonBlockingStatsDClientBuilder telemetrySocket(final String path, final UnixSocketAddressWithTransport.TransportType transport) {
151+
telemetryAddressLookup = socketLookup(path, transport);
152+
return this;
153+
}
154+
135155
public NonBlockingStatsDClientBuilder prefix(String val) {
136156
prefix = val;
137157
return this;

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,13 @@ 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-
delegate.connect(address);
138-
while (!delegate.finishConnect()) {
139-
// wait for connection to be established
140-
try {
141-
Thread.sleep(10);
142-
} catch (InterruptedException e) {
143-
throw new IOException("Interrupted while waiting for connection", e);
144-
}
137+
if (!delegate.connect(address)) {
145138
if (connectionTimeout > 0 && System.nanoTime() > deadline) {
146139
throw new IOException("Connection timed out");
147140
}
141+
if (!delegate.finishConnect()) {
142+
throw new IOException("Connection failed");
143+
}
148144
}
149145

150146
if (timeout > 0) {

src/test/java/com/timgroup/statsd/UnixStreamSocketTest.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,8 @@ public void start() throws IOException {
5151

5252
server = new UnixStreamSocketDummyStatsDServer(socketFile.toString());
5353

54-
Callable<SocketAddress> addressLookup = new Callable<SocketAddress>() {
55-
@Override
56-
public SocketAddress call() throws Exception {
57-
return new UnixSocketAddressWithTransport(new UnixSocketAddress(socketFile.getPath()), UnixSocketAddressWithTransport.TransportType.UDS_STREAM);
58-
}
59-
};
60-
6154
client = new NonBlockingStatsDClientBuilder().prefix("my.prefix")
62-
.addressLookup(addressLookup)
55+
.socket(socketFile.getPath(), UnixSocketAddressWithTransport.TransportType.UDS_STREAM)
6356
.port(0)
6457
.queueSize(1)
6558
.timeout(500) // non-zero timeout to ensure exception triggered if socket buffer full.
@@ -71,7 +64,7 @@ public SocketAddress call() throws Exception {
7164
.build();
7265

7366
clientAggregate = new NonBlockingStatsDClientBuilder().prefix("my.prefix")
74-
.addressLookup(addressLookup)
67+
.socket(socketFile.getPath(), UnixSocketAddressWithTransport.TransportType.UDS_STREAM)
7568
.port(0)
7669
.queueSize(1)
7770
.timeout(500) // non-zero timeout to ensure exception triggered if socket buffer full.

0 commit comments

Comments
 (0)