-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Java client connection state part 1 #24166
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ public class HubConnection implements AutoCloseable { | |
private final int negotiateVersion = 1; | ||
private final Logger logger = LoggerFactory.getLogger(HubConnection.class); | ||
private ScheduledExecutorService handshakeTimeout = null; | ||
private Completable start; | ||
|
||
/** | ||
* Sets the server timeout interval for the connection. | ||
|
@@ -341,83 +342,99 @@ public void setBaseUrl(String url) { | |
* @return A Completable that completes when the connection has been established. | ||
*/ | ||
public Completable start() { | ||
if (hubConnectionState != HubConnectionState.DISCONNECTED) { | ||
return Completable.complete(); | ||
} | ||
|
||
handshakeResponseSubject = CompletableSubject.create(); | ||
handshakeReceived = false; | ||
CompletableSubject tokenCompletable = CompletableSubject.create(); | ||
localHeaders.put(UserAgentHelper.getUserAgentName(), UserAgentHelper.createUserAgentString()); | ||
if (headers != null) { | ||
this.localHeaders.putAll(headers); | ||
} | ||
CompletableSubject localStart = CompletableSubject.create(); | ||
|
||
accessTokenProvider.subscribe(token -> { | ||
if (token != null && !token.isEmpty()) { | ||
this.localHeaders.put("Authorization", "Bearer " + token); | ||
hubConnectionStateLock.lock(); | ||
try { | ||
if (hubConnectionState != HubConnectionState.DISCONNECTED) { | ||
logger.debug("The connection is in the '{}' state. Waiting for in-progress start to complete or completing this start immediately.", hubConnectionState); | ||
return start; | ||
} | ||
tokenCompletable.onComplete(); | ||
}, error -> { | ||
tokenCompletable.onError(error); | ||
}); | ||
|
||
stopError = null; | ||
Single<NegotiateResponse> negotiate = null; | ||
if (!skipNegotiate) { | ||
negotiate = tokenCompletable.andThen(Single.defer(() -> startNegotiate(baseUrl, 0))); | ||
} else { | ||
negotiate = tokenCompletable.andThen(Single.defer(() -> Single.just(new NegotiateResponse(baseUrl)))); | ||
} | ||
hubConnectionState = HubConnectionState.CONNECTING; | ||
start = localStart; | ||
|
||
CompletableSubject start = CompletableSubject.create(); | ||
handshakeResponseSubject = CompletableSubject.create(); | ||
handshakeReceived = false; | ||
CompletableSubject tokenCompletable = CompletableSubject.create(); | ||
localHeaders.put(UserAgentHelper.getUserAgentName(), UserAgentHelper.createUserAgentString()); | ||
if (headers != null) { | ||
this.localHeaders.putAll(headers); | ||
} | ||
|
||
negotiate.flatMapCompletable(negotiateResponse -> { | ||
logger.debug("Starting HubConnection."); | ||
if (transport == null) { | ||
Single<String> tokenProvider = negotiateResponse.getAccessToken() != null ? Single.just(negotiateResponse.getAccessToken()) : accessTokenProvider; | ||
switch (transportEnum) { | ||
case LONG_POLLING: | ||
transport = new LongPollingTransport(localHeaders, httpClient, tokenProvider); | ||
break; | ||
default: | ||
transport = new WebSocketTransport(localHeaders, httpClient); | ||
accessTokenProvider.subscribe(token -> { | ||
if (token != null && !token.isEmpty()) { | ||
this.localHeaders.put("Authorization", "Bearer " + token); | ||
} | ||
tokenCompletable.onComplete(); | ||
}, error -> { | ||
tokenCompletable.onError(error); | ||
}); | ||
|
||
stopError = null; | ||
Single<NegotiateResponse> negotiate = null; | ||
if (!skipNegotiate) { | ||
negotiate = tokenCompletable.andThen(Single.defer(() -> startNegotiate(baseUrl, 0))); | ||
} else { | ||
negotiate = tokenCompletable.andThen(Single.defer(() -> Single.just(new NegotiateResponse(baseUrl)))); | ||
} | ||
|
||
transport.setOnReceive(this.callback); | ||
transport.setOnClose((message) -> stopConnection(message)); | ||
|
||
return transport.start(negotiateResponse.getFinalUrl()).andThen(Completable.defer(() -> { | ||
ByteBuffer handshake = HandshakeProtocol.createHandshakeRequestMessage( | ||
new HandshakeRequestMessage(protocol.getName(), protocol.getVersion())); | ||
|
||
connectionState = new ConnectionState(this); | ||
|
||
return transport.send(handshake).andThen(Completable.defer(() -> { | ||
timeoutHandshakeResponse(handshakeResponseTimeout, TimeUnit.MILLISECONDS); | ||
return handshakeResponseSubject.andThen(Completable.defer(() -> { | ||
hubConnectionStateLock.lock(); | ||
try { | ||
hubConnectionState = HubConnectionState.CONNECTED; | ||
logger.info("HubConnection started."); | ||
resetServerTimeout(); | ||
//Don't send pings if we're using long polling. | ||
if (transportEnum != TransportEnum.LONG_POLLING) { | ||
activatePingTimer(); | ||
negotiate.flatMapCompletable(negotiateResponse -> { | ||
logger.debug("Starting HubConnection."); | ||
if (transport == null) { | ||
Single<String> tokenProvider = negotiateResponse.getAccessToken() != null ? Single.just(negotiateResponse.getAccessToken()) : accessTokenProvider; | ||
switch (transportEnum) { | ||
case LONG_POLLING: | ||
transport = new LongPollingTransport(localHeaders, httpClient, tokenProvider); | ||
break; | ||
default: | ||
transport = new WebSocketTransport(localHeaders, httpClient); | ||
} | ||
} | ||
|
||
transport.setOnReceive(this.callback); | ||
transport.setOnClose((message) -> stopConnection(message)); | ||
|
||
return transport.start(negotiateResponse.getFinalUrl()).andThen(Completable.defer(() -> { | ||
ByteBuffer handshake = HandshakeProtocol.createHandshakeRequestMessage( | ||
new HandshakeRequestMessage(protocol.getName(), protocol.getVersion())); | ||
|
||
connectionState = new ConnectionState(this); | ||
|
||
return transport.send(handshake).andThen(Completable.defer(() -> { | ||
timeoutHandshakeResponse(handshakeResponseTimeout, TimeUnit.MILLISECONDS); | ||
return handshakeResponseSubject.andThen(Completable.defer(() -> { | ||
hubConnectionStateLock.lock(); | ||
try { | ||
hubConnectionState = HubConnectionState.CONNECTED; | ||
logger.info("HubConnection started."); | ||
resetServerTimeout(); | ||
//Don't send pings if we're using long polling. | ||
if (transportEnum != TransportEnum.LONG_POLLING) { | ||
activatePingTimer(); | ||
} | ||
} finally { | ||
hubConnectionStateLock.unlock(); | ||
} | ||
} finally { | ||
hubConnectionStateLock.unlock(); | ||
} | ||
|
||
return Completable.complete(); | ||
return Completable.complete(); | ||
})); | ||
})); | ||
})); | ||
})); | ||
// subscribe makes this a "hot" completable so this runs immediately | ||
}).subscribeWith(start); | ||
// subscribe makes this a "hot" completable so this runs immediately | ||
}).subscribe(() -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The CONNECTING->CONNECTED state transition is hidden in this diff, but we should assert the previous state where we can. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a PR that extracts this into a method, I'll add the logging though |
||
localStart.onComplete(); | ||
}, error -> { | ||
hubConnectionStateLock.lock(); | ||
hubConnectionState = HubConnectionState.DISCONNECTED; | ||
hubConnectionStateLock.unlock(); | ||
localStart.onError(error); | ||
}); | ||
} finally { | ||
hubConnectionStateLock.unlock(); | ||
} | ||
|
||
return start; | ||
return localStart; | ||
} | ||
|
||
private void activatePingTimer() { | ||
|
@@ -445,8 +462,8 @@ public void run() { | |
} | ||
|
||
private Single<NegotiateResponse> startNegotiate(String url, int negotiateAttempts) { | ||
if (hubConnectionState != HubConnectionState.DISCONNECTED) { | ||
return Single.just(null); | ||
if (hubConnectionState != HubConnectionState.CONNECTING) { | ||
throw new RuntimeException("HubConnection trying to negotiate when not in the CONNECTING state."); | ||
} | ||
|
||
return handleNegotiate(url).flatMap(response -> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ | |
public enum HubConnectionState { | ||
CONNECTED, | ||
DISCONNECTED, | ||
CONNECTING, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should do verbose logging for each state transition.