Skip to content

Commit 6055e59

Browse files
authored
Merge pull request #117 from jcalcote/feature/m/specify-connect-timeout-in-client-builder
Specify client connect timeout in OncRpcClientBuilder; consume in OncRpcClient connect call. This change adds a withConnectTimeout method to the OncRpcClientBuilder, which populates two fields: long connectTimeout; TimeUnit connectTimeoutUnit; These fields are then passed into the OncRpcClient during build and stored there as final fields on the OncRpcClient. When connect() is called, these values are used. They default to the same values that connect() originally used - Long.MAX_VALUE, and TimeUnit.MILLISECONDS. This change allows generated interfaces to use a user-specified connect timeout, defaulting to the original default values if not specified. Note the rpcgen code did not need to be modified for this, as it continues to use rpcClient.connect(), but internally, this method uses the builder-specified default values, or the original default values if no builder was used. An additional constructor is added to facilitate passing the default connect timeout parameters, which makes it possible for non-builder-users to also specify the default connect timeout (though they may also pass those values to connect directly).
2 parents 4f5ae8a + b18a329 commit 6055e59

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

oncrpc4j-core/src/main/java/org/dcache/oncrpc4j/rpc/OncRpcClient.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class OncRpcClient implements AutoCloseable {
3434
private static final String DEFAULT_SERVICE_NAME = null;
3535

3636
private final InetSocketAddress _socketAddress;
37+
private final long _connectTimeout;
38+
private final TimeUnit _connectTimeoutUnit;
3739
private final OncRpcSvc _rpcsvc;
3840

3941
public OncRpcClient(InetAddress address, int protocol, int port) {
@@ -66,14 +68,19 @@ public OncRpcClient(InetSocketAddress socketAddress, int protocol, int localPort
6668
.build());
6769
}
6870

69-
7071
private OncRpcClient(InetSocketAddress socketAddress, OncRpcSvc clientSvc) {
72+
this(socketAddress, Long.MAX_VALUE, TimeUnit.MILLISECONDS, clientSvc);
73+
}
74+
75+
public OncRpcClient(InetSocketAddress socketAddress, long connectTimeout, TimeUnit connectTimeoutUnit, OncRpcSvc clientSvc) {
7176
_socketAddress = socketAddress;
77+
_connectTimeout = connectTimeout;
78+
_connectTimeoutUnit = connectTimeoutUnit;
7279
_rpcsvc = clientSvc;
7380
}
7481

7582
public RpcTransport connect() throws IOException {
76-
return connect(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
83+
return connect(_connectTimeout, _connectTimeoutUnit);
7784
}
7885

7986
public RpcTransport connect(long timeout, TimeUnit timeUnit) throws IOException {
@@ -105,6 +112,8 @@ public static class OncRpcClientBuilder {
105112
.withSelectorThreadPoolSize(1)
106113
.withWorkerThreadPoolSize(1)
107114
.withoutAutoPublish();
115+
private long connectTimeout = Long.MAX_VALUE;
116+
private TimeUnit connectTimeoutUnit = TimeUnit.MILLISECONDS;
108117

109118
private OncRpcClientBuilder() {
110119
// no direct instantiation
@@ -192,14 +201,20 @@ public OncRpcClientBuilder withoutStartTLS() {
192201
return this;
193202
}
194203

204+
public OncRpcClientBuilder withConnectTimeout(long timeout, TimeUnit unit) {
205+
this.connectTimeout = timeout;
206+
this.connectTimeoutUnit = unit;
207+
return this;
208+
}
209+
195210
/**
196211
* Build a new {@link OncRpcClient} instance.
197212
*
198213
* @param endpoint the socket address of the remote RPC server
199214
* @return a new {@link OncRpcClient} instance
200215
*/
201216
public OncRpcClient build(InetSocketAddress endpoint) {
202-
return new OncRpcClient(endpoint, svcBuilder.build());
217+
return new OncRpcClient(endpoint, connectTimeout, connectTimeoutUnit, svcBuilder.build());
203218
}
204219

205220
/**

0 commit comments

Comments
 (0)