From af1904951965107598165f26ec8f4bc9f0b3da29 Mon Sep 17 00:00:00 2001 From: Robinson Date: Fri, 3 Nov 2023 18:15:34 +0100 Subject: [PATCH] Added multi-dispatch, for on the server when conducting handshakes (and waiting for a connection to complete). Under load, we cannot block the main thread --- .../network/connection/EventDispatcher.kt | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/dorkbox/network/connection/EventDispatcher.kt b/src/dorkbox/network/connection/EventDispatcher.kt index 5b939b22..2702b041 100644 --- a/src/dorkbox/network/connection/EventDispatcher.kt +++ b/src/dorkbox/network/connection/EventDispatcher.kt @@ -34,12 +34,36 @@ internal class EventDispatcher(val type: String) { CONNECT, ERROR, CLOSE } + internal class ED(private val dispatcher: EventDispatcher, private val type: EDType) { + fun launch(function: () -> Unit) { + dispatcher.launch(type, function) + } + + fun isDispatch(): Boolean { + return dispatcher.isDispatch(type) + } + } + + internal class Dispatch() { + companion object { + val executor = Executors.newCachedThreadPool( + NamedThreadFactory("Multi", Configuration.networkThreadGroup) + ) + } + + fun launch(function: () -> Unit) { + executor.submit(function) + } + } + companion object { private val DEBUG_EVENTS = false private val traceId = atomic(0) private val typedEntries: Array + val MULTI = Dispatch() + init { typedEntries = EDType.entries.toTypedArray() } @@ -65,15 +89,7 @@ internal class EventDispatcher(val type: String) { ) }.toTypedArray() - internal class ED(private val dispatcher: EventDispatcher, private val type: EDType) { - fun launch(function: () -> Unit) { - dispatcher.launch(type, function) - } - fun isDispatch(): Boolean { - return dispatcher.isDispatch(type) - } - } val CONNECT: ED val ERROR: ED