diff --git a/temerity/src/androidMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.android.kt b/temerity/src/androidMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.android.kt new file mode 100644 index 0000000000000000000000000000000000000000..69dabdb8bf8f62f1a0bb8e25fa045ff21e281036 --- /dev/null +++ b/temerity/src/androidMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.android.kt @@ -0,0 +1,24 @@ +/* + * Designed and developed in 2022-2024 by William Walker (wnwalker@ucsc.edu) + * Copyright 2022-2024 The Regents of the University of California. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +package edu.ucsc.its.temerity.di + +import kotlinx.coroutines.CoroutineDispatcher + +internal actual fun createLibraryDispatcher(): CoroutineDispatcher { + TODO("Not yet implemented") +} diff --git a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/TemClientConfig.kt b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/TemClientConfig.kt index a31ac918f9c15e6c42b80a57783b7388dd3ec99d..f0b2a6f449851711bfb6caf80965273820477bc0 100644 --- a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/TemClientConfig.kt +++ b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/TemClientConfig.kt @@ -39,6 +39,7 @@ import kotlin.time.Duration * @property optWebTimeoutDuration Specifies the timeout [kotlin.time.Duration] to use when making web requests * @property optCacheTimeoutDuration Specifies the timeout [kotlin.time.Duration] to use when storing cache entries. Affects keep-alive time for cached instance data like user role type fields. * @property optThreadCount Specifies the number of threads to use for each client instance. Defaults to 2 at a minimum + * @property optUseVirtualThreads Reserved for future use. Will configure library dispatchers to use virtual threads when available * @property optEnableCompression Specifies whether to turn on compression for HTTP requests. Enabled by default. */ @TemDsl @@ -62,6 +63,14 @@ public class TemClientConfig { public var optThreadCount: Int? = null + /** + * [optUseVirtualThreads] Option flag to be used in future to switch to using Project Loom virtual thread-backed coroutine dispatchers + * Inop for now, as Android Gradle Plugin does not yet support compiling with JDK > 17 + * Not to be converted to mutable field until JDK build target >= 21; required for Project Loom availability + * Treated as enum: null = unsupported by current library, false means disabled, true means enabled + */ + public val optUseVirtualThreads: Boolean? = null + public var optEnableCompression: Boolean = true /** diff --git a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/Temerity.kt b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/Temerity.kt index 422a74f29750e060454fd5e6a3a6898b67b901bc..cb06e78b48ae481e4b5d14a72a62c2295ea70e68 100644 --- a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/Temerity.kt +++ b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/core/Temerity.kt @@ -234,8 +234,25 @@ public class Temerity internal constructor( "Service url must be provided. Set it using TemClientConfig.serviceUrl(url)" } - libraryCoroutineDispatcher = get<CoroutineDispatcher>(named("libraryCoroutineDispatcher")) { - parametersOf(availableThreads(config.optThreadCount), "Temerity Library Dispatcher") + libraryCoroutineDispatcher = when (config.optUseVirtualThreads) { + // Case 1: User requests to enable virtual threads + // Need to check if JDK supports virtual threads + true -> { + val jvmVersion = Version.parse(BuildConfig.JVM_VERSION) + if (jvmVersion >= Version(21, 0, 0)) { + TODO("Configure virtual thread per task executor, create dispatcher") + } else { + get<CoroutineDispatcher>(named("libraryCoroutineDispatcher")) { + parametersOf(availableThreads(config.optThreadCount), "Temerity Library Dispatcher") + } + } + } + // Case 2: Virtual threads are identified as unsupported by null value as developer constant, or user requests to disable them: null | false + else -> { + get<CoroutineDispatcher>(named("libraryCoroutineDispatcher")) { + parametersOf(availableThreads(config.optThreadCount), "Temerity Library Dispatcher") + } + } } libraryCoroutineScope = get<CoroutineScope>(named("libraryCoroutineScope")) { parametersOf(libraryCoroutineDispatcher) diff --git a/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.kt b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.kt new file mode 100644 index 0000000000000000000000000000000000000000..13fbbf14d06ec6347043969a6a14e8300cf55e6e --- /dev/null +++ b/temerity/src/commonMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.kt @@ -0,0 +1,22 @@ +/* + * Designed and developed in 2022-2024 by William Walker (wnwalker@ucsc.edu) + * Copyright 2022-2024 The Regents of the University of California. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +package edu.ucsc.its.temerity.di + +import kotlinx.coroutines.CoroutineDispatcher + +internal expect fun createLibraryDispatcher(): CoroutineDispatcher diff --git a/temerity/src/jvmMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.jvm.kt b/temerity/src/jvmMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.jvm.kt new file mode 100644 index 0000000000000000000000000000000000000000..69dabdb8bf8f62f1a0bb8e25fa045ff21e281036 --- /dev/null +++ b/temerity/src/jvmMain/kotlin/edu/ucsc/its/temerity/di/PlatformDispatcher.jvm.kt @@ -0,0 +1,24 @@ +/* + * Designed and developed in 2022-2024 by William Walker (wnwalker@ucsc.edu) + * Copyright 2022-2024 The Regents of the University of California. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +package edu.ucsc.its.temerity.di + +import kotlinx.coroutines.CoroutineDispatcher + +internal actual fun createLibraryDispatcher(): CoroutineDispatcher { + TODO("Not yet implemented") +}