Skip to content

Commit

Permalink
Add autocomplete coroutine context, switch to less delicate API
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Jan 8, 2025
1 parent c3d4baa commit 3a0d0ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
18 changes: 15 additions & 3 deletions kord-extensions/src/main/kotlin/dev/kordex/core/ExtensibleBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -453,7 +465,7 @@ public open class ExtensibleBot(

is AutoCompleteInteractionCreateEvent ->
if (settings.applicationCommandsBuilder.enabled) {
kordRef.launch(interactionCoroutineContext) {
kordRef.launch(autoCompleteCoroutineContext) {
try {
getKoin().get<ApplicationCommandRegistry>().handle(event)
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 3a0d0ca

Please sign in to comment.