Skip to content

Commit e158c41

Browse files
authored
Merge pull request #1242 from ably/fix/check-fallback-impl
Improve fallback host resolution in connection logic
2 parents 243a08c + cad7635 commit e158c41

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

‎lib/src/main/java/io/ably/lib/transport/ConnectionManager.java‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,21 +1471,26 @@ private synchronized void setSuspendTime() {
14711471
suspendTime = (clock.currentTimeMillis() + connectionStateTtl);
14721472
}
14731473

1474+
private boolean shouldTryFallback(TransportParams params, ErrorInfo reason) {
1475+
return params != null && (reason == null || reason.statusCode >= 500);
1476+
}
1477+
14741478
/**
14751479
* After a connection attempt failed, check to
14761480
* see whether we should attempt to use a fallback.
14771481
* @param reason
14781482
* @return StateIndication if a fallback connection attempt is required, otherwise null
14791483
*/
14801484
private StateIndication checkFallback(ErrorInfo reason) {
1481-
if(pendingConnect != null && (reason == null || reason.statusCode >= 500)) {
1482-
if (checkConnectivity()) {
1483-
/* we will try a fallback host */
1484-
String hostFallback = hosts.getFallback(pendingConnect.host);
1485-
if (hostFallback != null) {
1486-
Log.v(TAG, "checkFallback: fallback to " + hostFallback);
1487-
return new StateIndication(ConnectionState.connecting, null, hostFallback, pendingConnect.host);
1488-
}
1485+
// checkConnectivity is blocking http call, try to avoid it if we don't need to try fallback
1486+
TransportParams pendingConnectionParams = pendingConnect;
1487+
boolean internetIsUp = shouldTryFallback(pendingConnectionParams, reason) && checkConnectivity();
1488+
if (internetIsUp) {
1489+
/* we will try a fallback host */
1490+
String hostFallback = hosts.getFallback(pendingConnectionParams.host);
1491+
if (hostFallback != null && pendingConnectionParams == pendingConnect) {
1492+
Log.v(TAG, "checkFallback: fallback to " + hostFallback);
1493+
return new StateIndication(ConnectionState.connecting, null, hostFallback, pendingConnectionParams.host);
14891494
}
14901495
}
14911496
pendingConnect = null;
@@ -2033,7 +2038,7 @@ private boolean isFatalError(ErrorInfo err) {
20332038
private final Map<ConnectionState, State> states = new HashMap<>();
20342039
private State currentState;
20352040
private ErrorInfo stateError;
2036-
private ConnectParams pendingConnect;
2041+
private volatile ConnectParams pendingConnect;
20372042
private boolean suppressRetry; /* for tests only; modified via reflection */
20382043
private volatile ITransport transport;
20392044
private long suspendTime;

0 commit comments

Comments
 (0)