Skip to content

Commit

Permalink
Use a singleton threadpool for kex message handler flushing (#459) (f…
Browse files Browse the repository at this point in the history
…ixes #458)
  • Loading branch information
FliegenKLATSCH committed Jan 30, 2024
1 parent 7498f7a commit 7d77e6d
Showing 1 changed file with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -75,18 +74,18 @@ public class KeyExchangeMessageHandler {
// re-acquires the write lock, so normally not many readers (i.e., writePacket() calls) will get a chance to enqueue
// new packets.

/**
* We need the flushing thread to have priority over writing threads. So we use a lock that favors writers over
* readers, and any state updates and the flushing thread are writers, while writePacket() is a reader.
*/
protected final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(false);

/**
* An {@link ExecutorService} used to flush the queue asynchronously.
*
* @see #flushQueue(DefaultKeyExchangeFuture)
*/
protected final ExecutorService flushRunner = Executors.newSingleThreadExecutor();
protected static ExecutorService flushRunner = ThreadUtils.newCachedThreadPool("kex-flusher");

/**
* We need the flushing thread to have priority over writing threads. So we use a lock that favors writers over
* readers, and any state updates and the flushing thread are writers, while writePacket() is a reader.
*/
protected final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(false);

/**
* The {@link AbstractSession} this {@link KeyExchangeMessageHandler} belongs to.
Expand Down Expand Up @@ -209,7 +208,6 @@ public void shutdown() {
kexFlushedFuture.get());
});
items.getValue().setValue(Boolean.valueOf(items.getKey().intValue() == 0));
flushRunner.shutdownNow();
}

/**
Expand Down

0 comments on commit 7d77e6d

Please sign in to comment.