Skip to content

Commit dcafeaf

Browse files
committed
Fix subscriber timer memory leak
1 parent a9b156e commit dcafeaf

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

pubsub/src/main/java/com/fastcomments/pubsub/LiveEventSubscriber.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ private class FastCommentsWebSocketListener extends WebSocketListener {
131131
private final boolean[] isIntentionallyClosed;
132132
private final CommentWidgetConfig config;
133133
private final Timer pingTimer = new Timer();
134+
private Timer reconnectTimer;
134135
private final String tenantId;
135136
private final String urlId;
136137
private final String userIdWS;
@@ -240,6 +241,12 @@ private void cleanup() {
240241
// Clean up ping timer
241242
pingTimer.cancel();
242243

244+
// Cancel any existing reconnect timer
245+
if (reconnectTimer != null) {
246+
reconnectTimer.cancel();
247+
reconnectTimer = null;
248+
}
249+
243250
// Initialize last event time if not set
244251
if (lastEventTime <= 0) {
245252
lastEventTime = new Date().getTime();
@@ -255,7 +262,8 @@ private void cleanup() {
255262
double jitter = Math.random();
256263
long reconnectDelay = (long) (RECONNECT_INTERVAL_BASE * jitter);
257264

258-
new Timer().schedule(new TimerTask() {
265+
reconnectTimer = new Timer();
266+
reconnectTimer.schedule(new TimerTask() {
259267
@Override
260268
public void run() {
261269
if (!isIntentionallyClosed[0]) {

0 commit comments

Comments
 (0)