Skip to content

Commit

Permalink
Fix reconnection issue
Browse files Browse the repository at this point in the history
  • Loading branch information
erasmospunk committed Apr 14, 2016
1 parent f75e71a commit cd9ccb7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
47 changes: 34 additions & 13 deletions core/src/main/java/com/coinomi/core/network/ServerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class ServerClient implements BitBlockchainConnection {
private static final Random RANDOM = new Random();

private static final long MAX_WAIT = 16;
private static final long CONNECTION_STABILIZATION = 30;
private final ConnectivityHelper connectivityHelper;

private CoinType type;
Expand All @@ -74,6 +75,7 @@ public class ServerClient implements BitBlockchainConnection {
private ServerAddress lastServerAddress;
private StratumClient stratumClient;
private long retrySeconds = 0;
private long reconnectAt = 0;
private boolean stopped = false;

private File cacheDir;
Expand All @@ -82,24 +84,39 @@ public class ServerClient implements BitBlockchainConnection {
// TODO, only one is supported at the moment. Change when accounts are supported.
private transient CopyOnWriteArrayList<ListenerRegistration<ConnectionEventListener>> eventListeners;

private void reschedule(Runnable r, long delay, TimeUnit unit) {
connectionExec.remove(r);
connectionExec.schedule(r, delay, unit);
}

private Runnable reconnectTask = new Runnable() {
public boolean isPolling = false;
@Override
public void run() {
if (!stopped) {
if (connectivityHelper.isConnected()) {
createStratumClient().startAsync();
isPolling = false;
long reconnectIn = Math.max(reconnectAt - System.currentTimeMillis(), 0);
// Check if we must reconnect in the next second
if (reconnectIn < 1000) {
if (connectivityHelper.isConnected()) {
createStratumClient().startAsync();
} else {
// Start polling for connection to become available
reschedule(reconnectTask, 1, TimeUnit.SECONDS);
}
} else {
// Start polling for connection to become available
if (!isPolling) log.info("No connectivity, starting polling.");
connectionExec.remove(reconnectTask);
connectionExec.schedule(reconnectTask, 1, TimeUnit.SECONDS);
isPolling = true;
reschedule(reconnectTask, reconnectIn, TimeUnit.MILLISECONDS);
}
} else {
log.info("{} client stopped, aborting reconnect.", type.getName());
isPolling = false;
}
}
};

private Runnable connectionCheckTask = new Runnable() {
@Override
public void run() {
if (isActivelyConnected()) {
reconnectAt = 0;
retrySeconds = 0;
}
}
};
Expand All @@ -111,7 +128,9 @@ public void running() {
if (isActivelyConnected()) {
log.info("{} client connected to {}", type.getName(), lastServerAddress);
broadcastOnConnection();
retrySeconds = 0;

// Test that the connection is stable
reschedule(connectionCheckTask, CONNECTION_STABILIZATION, TimeUnit.SECONDS);
}
}

Expand All @@ -125,8 +144,10 @@ public void terminated(Service.State from) {
// Try to restart
if (!stopped) {
log.info("Reconnecting {} in {} seconds", type.getName(), retrySeconds);
connectionExec.remove(connectionCheckTask);
connectionExec.remove(reconnectTask);
if (retrySeconds > 0) {
reconnectAt = System.currentTimeMillis() + retrySeconds * 1000;
connectionExec.schedule(reconnectTask, retrySeconds, TimeUnit.SECONDS);
} else {
connectionExec.execute(reconnectTask);
Expand Down Expand Up @@ -154,11 +175,11 @@ private StratumClient createStratumClient() {
}

private ServerAddress getServerAddress() {
// If we blacklisted all servers, reset and increase back-off time
// If we blacklisted all servers, reset
if (failedAddresses.size() == addresses.size()) {
failedAddresses.clear();
retrySeconds = Math.min(Math.max(1, retrySeconds * 2), MAX_WAIT);
}
retrySeconds = Math.min(Math.max(1, retrySeconds * 2), MAX_WAIT);

ServerAddress address;
// Not the most efficient, but does the job
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,6 @@ private void clearTransientState() {
fetchingTransactions.clear();
outOfOrderTransactions.clear();
lastBalance = type.value(0);
lastConnectivity = WalletConnectivityStatus.DISCONNECTED;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,7 @@ public void onLoadFinished(final Loader<Cursor> loader, final Cursor data) {
}

@Override
public void onLoaderReset(final Loader<Cursor> loader) {
}
public void onLoaderReset(final Loader<Cursor> loader) { }
};

@Override
Expand Down

0 comments on commit cd9ccb7

Please sign in to comment.