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

This commit is contained in:
Robinson 2023-11-03 18:15:34 +01:00
parent f40e8cf14d
commit af19049519
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C
1 changed files with 24 additions and 8 deletions

View File

@ -34,12 +34,36 @@ internal class EventDispatcher(val type: String) {
CONNECT, ERROR, CLOSE 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 { companion object {
private val DEBUG_EVENTS = false private val DEBUG_EVENTS = false
private val traceId = atomic(0) private val traceId = atomic(0)
private val typedEntries: Array<EDType> private val typedEntries: Array<EDType>
val MULTI = Dispatch()
init { init {
typedEntries = EDType.entries.toTypedArray() typedEntries = EDType.entries.toTypedArray()
} }
@ -65,15 +89,7 @@ internal class EventDispatcher(val type: String) {
) )
}.toTypedArray() }.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 CONNECT: ED
val ERROR: ED val ERROR: ED