Skip to content

Commit

Permalink
Improve watch reconnect, backoff and log disconnect error
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Feb 26, 2023
1 parent e18d7b3 commit 619fb8e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,10 @@ private class WatcherImpl implements Watcher {

@Override
public void onOpen() {
// Reset the retry backoff after a successful connection
resetBackOff();
logger.translatedInfo("connect.watch.started");

// Reset the retry backoff after the connection is healthy for some seconds
startResetBackOffTimer();
}

@Override
Expand Down Expand Up @@ -194,7 +195,39 @@ public void onError(Throwable t) {
: " (cause: " + t.getCause().toString() + ")"
)
);
cancelResetBackOffTimer();
retry();
}

@Override
public void onCompleted() {
cancelResetBackOffTimer();
retry();
}

private Timer resetBackOffTimer;

void startResetBackOffTimer() {
if (resetBackOffTimer != null) {
resetBackOffTimer.cancel();
}
resetBackOffTimer = new Timer();
resetBackOffTimer.schedule(new TimerTask() {
@Override
public void run() {
if (started.get()) {
resetBackOff();
}
}
}, Duration.ofSeconds(10).toMillis());
}

void cancelResetBackOffTimer() {
if (resetBackOffTimer != null) {
resetBackOffTimer.cancel();
resetBackOffTimer = null;
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public WebSocket watch(Watcher watcher) {
return httpClient.newWebSocket(req, new WebSocketListener() {
@Override
public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
if (code != 1000) {
watcher.onError(new RuntimeException("Watch closed with code " + code + ": " + reason));
return;
}
watcher.onCompleted();
}

Expand Down

0 comments on commit 619fb8e

Please sign in to comment.