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

Added withServerTimeout and withKeepAliveInterval to HubConnectionBuilder for java client #46172

Merged
merged 7 commits into from
Jan 23, 2023
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 timeout The serverTimeout to be set.
* @return This instance of the HttpHubConnectionBuilder.
*/
public HttpHubConnectionBuilder withServerTimeout(long timeout) {
this.serverTimeout = timeout;
surayya-MS marked this conversation as resolved.
Show resolved Hide resolved
return this;
}

/**
* Sets keepAliveInterval for the {@link HubConnection}.
*
* @param interval The keepAliveInterval to be set.
* @return This instance of the HttpHubConnectionBuilder.
*/
public HttpHubConnectionBuilder withKeepAliveInterval(long interval) {
this.keepAliveInterval = interval;
surayya-MS marked this conversation as resolved.
Show resolved Hide resolved
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());
}
}