Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions lib/src/main/java/io/ably/lib/transport/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1471,21 +1471,26 @@ private synchronized void setSuspendTime() {
suspendTime = (clock.currentTimeMillis() + connectionStateTtl);
}

private boolean shouldTryFallback(TransportParams params, ErrorInfo reason) {
return params != null && (reason == null || reason.statusCode >= 500);
}

/**
* After a connection attempt failed, check to
* see whether we should attempt to use a fallback.
* @param reason
* @return StateIndication if a fallback connection attempt is required, otherwise null
*/
private StateIndication checkFallback(ErrorInfo reason) {
if(pendingConnect != null && (reason == null || reason.statusCode >= 500)) {
if (checkConnectivity()) {
/* we will try a fallback host */
String hostFallback = hosts.getFallback(pendingConnect.host);
if (hostFallback != null) {
Log.v(TAG, "checkFallback: fallback to " + hostFallback);
return new StateIndication(ConnectionState.connecting, null, hostFallback, pendingConnect.host);
}
// checkConnectivity is blocking http call, try to avoid it if we don't need to try fallback
TransportParams pendingConnectionParams = pendingConnect;
boolean internetIsUp = shouldTryFallback(pendingConnectionParams, reason) && checkConnectivity();
if (internetIsUp) {
/* we will try a fallback host */
String hostFallback = hosts.getFallback(pendingConnectionParams.host);
if (hostFallback != null && pendingConnectionParams == pendingConnect) {
Log.v(TAG, "checkFallback: fallback to " + hostFallback);
return new StateIndication(ConnectionState.connecting, null, hostFallback, pendingConnectionParams.host);
}
}
pendingConnect = null;
Expand Down Expand Up @@ -2033,7 +2038,7 @@ private boolean isFatalError(ErrorInfo err) {
private final Map<ConnectionState, State> states = new HashMap<>();
private State currentState;
private ErrorInfo stateError;
private ConnectParams pendingConnect;
private volatile ConnectParams pendingConnect;
private boolean suppressRetry; /* for tests only; modified via reflection */
private volatile ITransport transport;
private long suspendTime;
Expand Down
Loading