Skip to content

Commit

Permalink
Added withServerTimeout and withKeepAliveInterval to HubConnectionBui…
Browse files Browse the repository at this point in the history
…lder for java client (dotnet#46172)

* Added withServerTimeout and withKeepAliveInterval to HubConnectionBuilder for java client
  • Loading branch information
surayya-MS authored and atossell91 committed Jan 23, 2023
1 parent 5145bd6 commit 55634bb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class HttpHubConnectionBuilder {
private Map<String, String> headers;
private TransportEnum transportEnum;
private Action1<OkHttpClient.Builder> configureBuilder;
private long serverTimeout = HubConnection.DEFAULT_SERVER_TIMEOUT;
private long keepAliveInterval = HubConnection.DEFAULT_KEEP_ALIVE_INTERVAL;

HttpHubConnectionBuilder(String url) {
this.url = url;
Expand Down Expand Up @@ -140,13 +142,35 @@ public HttpHubConnectionBuilder setHttpClientBuilderCallback(Action1<OkHttpClien
return this;
}

/**
* Sets serverTimeout for the {@link HubConnection}.
*
* @param timeoutInMilliseconds The serverTimeout to be set.
* @return This instance of the HttpHubConnectionBuilder.
*/
public HttpHubConnectionBuilder withServerTimeout(long timeoutInMilliseconds) {
this.serverTimeout = timeoutInMilliseconds;
return this;
}

/**
* Sets keepAliveInterval for the {@link HubConnection}.
*
* @param intervalInMilliseconds The keepAliveInterval to be set.
* @return This instance of the HttpHubConnectionBuilder.
*/
public HttpHubConnectionBuilder withKeepAliveInterval(long intervalInMilliseconds) {
this.keepAliveInterval = intervalInMilliseconds;
return this;
}

/**
* Builds a new instance of {@link HubConnection}.
*
* @return A new instance of {@link HubConnection}.
*/
public HubConnection build() {
return new HubConnection(url, transport, skipNegotiate, httpClient, protocol, accessTokenProvider,
handshakeResponseTimeout, headers, transportEnum, configureBuilder);
handshakeResponseTimeout, headers, transportEnum, configureBuilder, serverTimeout, keepAliveInterval);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
* A connection used to invoke hub methods on a SignalR Server.
*/
public class HubConnection implements AutoCloseable {
static final long DEFAULT_SERVER_TIMEOUT = 30 * 1000;
static final long DEFAULT_KEEP_ALIVE_INTERVAL = 15 * 1000;

private static final byte RECORD_SEPARATOR = 0x1e;
private static final List<Type> emptyArray = new ArrayList<>();
private static final int MAX_NEGOTIATE_ATTEMPTS = 100;
Expand All @@ -49,8 +52,8 @@ public class HubConnection implements AutoCloseable {
// These are all user-settable properties
private String baseUrl;
private List<OnClosedCallback> onClosedCallbackList;
private long keepAliveInterval = 15 * 1000;
private long serverTimeout = 30 * 1000;
private long keepAliveInterval = DEFAULT_KEEP_ALIVE_INTERVAL;
private long serverTimeout = DEFAULT_SERVER_TIMEOUT;
private long handshakeResponseTimeout = 15 * 1000;

// Private property, modified for testing
Expand Down Expand Up @@ -120,7 +123,7 @@ Transport getTransport() {

HubConnection(String url, Transport transport, boolean skipNegotiate, HttpClient httpClient, HubProtocol protocol,
Single<String> accessTokenProvider, long handshakeResponseTimeout, Map<String, String> headers, TransportEnum transportEnum,
Action1<OkHttpClient.Builder> configureBuilder) {
Action1<OkHttpClient.Builder> configureBuilder, long serverTimeout, long keepAliveInterval) {
if (url == null || url.isEmpty()) {
throw new IllegalArgumentException("A valid url is required.");
}
Expand Down Expand Up @@ -159,6 +162,9 @@ Transport getTransport() {
this.headers = headers;
this.skipNegotiate = skipNegotiate;

this.serverTimeout = serverTimeout;
this.keepAliveInterval = keepAliveInterval;

this.callback = (payload) -> ReceiveLoop(payload);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public void removingMultipleHandlersWithOneCallToRemove() {
Action action = () -> value.getAndUpdate((val) -> val + 1);
Action secondAction = () -> {
value.getAndUpdate((val) -> val + 2);

complete.onComplete();
};

Expand Down Expand Up @@ -3962,4 +3962,28 @@ public void hubConnectionStopDuringConnecting() {

assertTrue(close.blockingAwait(30, TimeUnit.SECONDS));
}

@Test
public void serverTimeoutIsSetThroughBuilder()
{
long timeout = 60 * 1000;
HubConnection hubConnection = HubConnectionBuilder
.create("http://example.com")
.withServerTimeout(timeout)
.build();

assertEquals(timeout, hubConnection.getServerTimeout());
}

@Test
public void keepAliveIntervalIsSetThroughBuilder()
{
long interval = 60 * 1000;
HubConnection hubConnection = HubConnectionBuilder
.create("http://example.com")
.withKeepAliveInterval(interval)
.build();

assertEquals(interval, hubConnection.getKeepAliveInterval());
}
}

0 comments on commit 55634bb

Please sign in to comment.