Skip to content

Commit

Permalink
Fix implementation fo connection freshness
Browse files Browse the repository at this point in the history
and extract it into a method
  • Loading branch information
funkyboy committed May 10, 2018
1 parent 02f0d0f commit 11d348f
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/src/main/java/io/ably/lib/transport/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void onConnectionStateChanged(ConnectionStateListener.ConnectionStateChan
put(ConnectionState.failed, new StateInfo(ConnectionState.failed, false, false, true, false, 0, REASON_FAILED));
}};

long maxIdleInterval;
long maxIdleInterval = Defaults.maxIdleInterval;
long connectionStateTtl = Defaults.connectionStateTtl;
private long failedAuthAttempts = 0;

Expand Down Expand Up @@ -207,19 +207,6 @@ public String getHost() {

public void connect() {

if (transport != null) {
long now = System.currentTimeMillis();
long intervalSinceLastActivity = now - lastActivity;
if (intervalSinceLastActivity > (maxIdleInterval + connectionStateTtl)) {
connection.id = null;
connection.key = null;
connection.recoveryKey = null;
startThread();
requestState(ConnectionState.connecting);
return;
}
}

boolean connectionExist = state.state == ConnectionState.connected;
boolean connectionAttemptInProgress = (requestedState != null && requestedState.state == ConnectionState.connecting) ||
state.state == ConnectionState.connecting;
Expand Down Expand Up @@ -688,12 +675,29 @@ private void handleStateRequest() {
requestedState = null;
}

private boolean isConnectionStale() {
if (lastActivity == 0) {
return false;
}
long now = System.currentTimeMillis();
long intervalSinceLastActivity = now - lastActivity;
return intervalSinceLastActivity > (maxIdleInterval + connectionStateTtl);
}

private void handleStateChange(StateIndication stateChange) {
/* if we have had a disconnected state indication
* from the transport then we have to decide whether
* to transition to closed, disconnected to suspended depending
* on when we last had a successful connection */
if(stateChange.state == ConnectionState.disconnected) {
if (isConnectionStale()) {
/* RTN15g1, RTN15g2 Force a new connection if the previous one is stale */
connection.id = null;
connection.key = null;
connection.recoveryKey = null;
requestState(ConnectionState.suspended);
return;
}
switch(state.state) {
case connecting:
stateChange = checkSuspend(stateChange);
Expand Down

0 comments on commit 11d348f

Please sign in to comment.