Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 619fb8e

Browse files
committedFeb 26, 2023
Improve watch reconnect, backoff and log disconnect error
1 parent e18d7b3 commit 619fb8e

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed
 

‎core/src/main/java/com/minekube/connect/register/WatcherRegister.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ private class WatcherImpl implements Watcher {
145145

146146
@Override
147147
public void onOpen() {
148-
// Reset the retry backoff after a successful connection
149-
resetBackOff();
150148
logger.translatedInfo("connect.watch.started");
149+
150+
// Reset the retry backoff after the connection is healthy for some seconds
151+
startResetBackOffTimer();
151152
}
152153

153154
@Override
@@ -194,7 +195,39 @@ public void onError(Throwable t) {
194195
: " (cause: " + t.getCause().toString() + ")"
195196
)
196197
);
198+
cancelResetBackOffTimer();
197199
retry();
198200
}
201+
202+
@Override
203+
public void onCompleted() {
204+
cancelResetBackOffTimer();
205+
retry();
206+
}
207+
208+
private Timer resetBackOffTimer;
209+
210+
void startResetBackOffTimer() {
211+
if (resetBackOffTimer != null) {
212+
resetBackOffTimer.cancel();
213+
}
214+
resetBackOffTimer = new Timer();
215+
resetBackOffTimer.schedule(new TimerTask() {
216+
@Override
217+
public void run() {
218+
if (started.get()) {
219+
resetBackOff();
220+
}
221+
}
222+
}, Duration.ofSeconds(10).toMillis());
223+
}
224+
225+
void cancelResetBackOffTimer() {
226+
if (resetBackOffTimer != null) {
227+
resetBackOffTimer.cancel();
228+
resetBackOffTimer = null;
229+
}
230+
}
231+
199232
}
200233
}

‎core/src/main/java/com/minekube/connect/watch/WatchClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public WebSocket watch(Watcher watcher) {
7171
return httpClient.newWebSocket(req, new WebSocketListener() {
7272
@Override
7373
public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
74+
if (code != 1000) {
75+
watcher.onError(new RuntimeException("Watch closed with code " + code + ": " + reason));
76+
return;
77+
}
7478
watcher.onCompleted();
7579
}
7680

0 commit comments

Comments
 (0)
Please sign in to comment.