Skip to content

Commit

Permalink
feat: create heartbeat scheduler with named threads and as daemon (#106)
Browse files Browse the repository at this point in the history
Co-authored-by: Pavel Valodzka <pavel@valodzka.name>
  • Loading branch information
valodzka and Pavel Valodzka authored Jan 22, 2021
1 parent 686ac08 commit 7c9c382
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/main/java/io/socket/engineio/client/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -37,6 +30,8 @@ public class Socket extends Emitter {

private static final Logger logger = Logger.getLogger(Socket.class.getName());

private static final AtomicInteger HEARTBEAT_THREAD_COUNTER = new AtomicInteger();

private static final String PROBE_ERROR = "probe error";


Expand Down Expand Up @@ -848,11 +843,22 @@ public String id() {

private ScheduledExecutorService getHeartbeatScheduler() {
if (this.heartbeatScheduler == null || this.heartbeatScheduler.isShutdown()) {
this.heartbeatScheduler = Executors.newSingleThreadScheduledExecutor();
this.heartbeatScheduler = createHeartbeatScheduler();
}
return this.heartbeatScheduler;
}

private ScheduledExecutorService createHeartbeatScheduler() {
return Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "engine.io-client.heartbeat-" + HEARTBEAT_THREAD_COUNTER.getAndIncrement());
thread.setDaemon(true);
return thread;
}
});
}

public static class Options extends Transport.Options {

/**
Expand Down

0 comments on commit 7c9c382

Please sign in to comment.