From 3a0d0ca54b7957323f0fca4a3ec10778263f6866 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Wed, 8 Jan 2025 18:55:40 +0000 Subject: [PATCH] Add autocomplete coroutine context, switch to less delicate API --- .../kotlin/dev/kordex/core/ExtensibleBot.kt | 18 +++++++++++++++--- .../core/builders/ExtensibleBotBuilder.kt | 7 +++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/kord-extensions/src/main/kotlin/dev/kordex/core/ExtensibleBot.kt b/kord-extensions/src/main/kotlin/dev/kordex/core/ExtensibleBot.kt index 3460bbb2b..7e8c82390 100644 --- a/kord-extensions/src/main/kotlin/dev/kordex/core/ExtensibleBot.kt +++ b/kord-extensions/src/main/kotlin/dev/kordex/core/ExtensibleBot.kt @@ -53,6 +53,7 @@ import kotlinx.serialization.json.Json import kotlinx.serialization.json.decodeFromJsonElement import org.koin.core.component.inject import org.koin.dsl.bind +import java.util.concurrent.Executors import kotlin.Throws import kotlin.concurrent.thread @@ -75,9 +76,20 @@ public open class ExtensibleBot( override var mutex: Mutex? = Mutex() override var locking: Boolean = settings.membersBuilder.lockMemberRequests - @OptIn(DelicateCoroutinesApi::class) + protected var autoCompleteCoroutineThreads: Int = 0 + protected var interactionCoroutineThreads: Int = 0 + + public val autoCompleteCoroutineContext: CoroutineDispatcher = + Executors.newFixedThreadPool(settings.autoCompleteContextThreads) { r -> + autoCompleteCoroutineThreads++ + Thread(r, "kordex-interactions-${autoCompleteCoroutineThreads - 1}") + }.asCoroutineDispatcher() + public val interactionCoroutineContext: CoroutineDispatcher = - newFixedThreadPoolContext(settings.interactionContextThreads, "kord-extensions-interactions") + Executors.newFixedThreadPool(settings.interactionContextThreads) { r -> + interactionCoroutineThreads++ + Thread(r, "kordex-autocomplete-${interactionCoroutineThreads - 1}") + }.asCoroutineDispatcher() /** @suppress Meant for internal use by public inline function. **/ public val kordRef: Kord by inject() @@ -453,7 +465,7 @@ public open class ExtensibleBot( is AutoCompleteInteractionCreateEvent -> if (settings.applicationCommandsBuilder.enabled) { - kordRef.launch(interactionCoroutineContext) { + kordRef.launch(autoCompleteCoroutineContext) { try { getKoin().get().handle(event) } catch (e: Exception) { diff --git a/kord-extensions/src/main/kotlin/dev/kordex/core/builders/ExtensibleBotBuilder.kt b/kord-extensions/src/main/kotlin/dev/kordex/core/builders/ExtensibleBotBuilder.kt index b686ad994..c7eb32d77 100644 --- a/kord-extensions/src/main/kotlin/dev/kordex/core/builders/ExtensibleBotBuilder.kt +++ b/kord-extensions/src/main/kotlin/dev/kordex/core/builders/ExtensibleBotBuilder.kt @@ -79,6 +79,13 @@ public open class ExtensibleBotBuilder { /** Called to create an [ExtensibleBot], can be set to the constructor of your own subtype if needed. **/ public var constructor: (ExtensibleBotBuilder, String) -> ExtensibleBot = ::ExtensibleBot + /** + * The number of threads to use for autocomplete event coroutines. + * + * Defaults to the available CPU cores, as returned by `Runtime.getRuntime().availableProcessors()`. + */ + public var autoCompleteContextThreads: Int = Runtime.getRuntime().availableProcessors() + /** * The number of threads to use for interaction event coroutines. *