Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changes/4d3a104a-3225-4dcb-be05-f5155d320952.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "4d3a104a-3225-4dcb-be05-f5155d320952",
"type": "feature",
"description": "Add executorService configuration to OkHttpEngine",
"issues": [
"https://github.com/aws/aws-sdk-kotlin/issues/1707"
]
}
5 changes: 1 addition & 4 deletions runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
*/
import aws.sdk.kotlin.gradle.dsl.configurePublishing
import aws.sdk.kotlin.gradle.kmp.*
import aws.sdk.kotlin.gradle.kmp.configureKmpTargets
import aws.sdk.kotlin.gradle.kmp.kotlin
import aws.sdk.kotlin.gradle.kmp.needsKmpConfigured
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeSimulatorTest
Expand Down Expand Up @@ -144,7 +141,7 @@ subprojects {
}
}

// configureIosSimulatorTasks()
configureIosSimulatorTasks()

val excludeFromDocumentation = listOf(
":runtime:testing",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public final class aws/smithy/kotlin/runtime/http/engine/okhttp/OkHttpEngineConf
public final fun getCertificatePinner ()Lokhttp3/CertificatePinner;
public final fun getCipherSuites ()Ljava/util/List;
public final fun getConnectionIdlePollingInterval-FghU774 ()Lkotlin/time/Duration;
public final fun getExecutorService ()Ljava/util/concurrent/ExecutorService;
public final fun getHostnameVerifier ()Ljavax/net/ssl/HostnameVerifier;
public final fun getKeyManager ()Ljavax/net/ssl/KeyManager;
public final fun getMaxConcurrencyPerHost-pVg5ArA ()I
Expand All @@ -83,6 +84,7 @@ public final class aws/smithy/kotlin/runtime/http/engine/okhttp/OkHttpEngineConf
public final fun getCertificatePinner ()Lokhttp3/CertificatePinner;
public final fun getCipherSuites ()Ljava/util/List;
public final fun getConnectionIdlePollingInterval-FghU774 ()Lkotlin/time/Duration;
public final fun getExecutorService ()Ljava/util/concurrent/ExecutorService;
public final fun getHostnameVerifier ()Ljavax/net/ssl/HostnameVerifier;
public final fun getKeyManager ()Ljavax/net/ssl/KeyManager;
public final fun getMaxConcurrencyPerHost-0hXNFcg ()Lkotlin/UInt;
Expand All @@ -91,6 +93,7 @@ public final class aws/smithy/kotlin/runtime/http/engine/okhttp/OkHttpEngineConf
public final fun setCertificatePinner (Lokhttp3/CertificatePinner;)V
public final fun setCipherSuites (Ljava/util/List;)V
public final fun setConnectionIdlePollingInterval-BwNAW2A (Lkotlin/time/Duration;)V
public final fun setExecutorService (Ljava/util/concurrent/ExecutorService;)V
public final fun setHostnameVerifier (Ljavax/net/ssl/HostnameVerifier;)V
public final fun setKeyManager (Ljavax/net/ssl/KeyManager;)V
public final fun setMaxConcurrencyPerHost-ExVfyTY (Lkotlin/UInt;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public fun OkHttpEngineConfig.buildClient(
)
connectionPool(pool)

val dispatcher = Dispatcher().apply {
val dispatcher = (executorService?.let { Dispatcher(it) } ?: Dispatcher()).apply {
maxRequests = config.maxConcurrency.toInt()
maxRequestsPerHost = config.maxConcurrencyPerHost.toInt()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import aws.smithy.kotlin.runtime.http.engine.HttpClientEngineConfigImpl
import aws.smithy.kotlin.runtime.telemetry.Global
import aws.smithy.kotlin.runtime.telemetry.TelemetryProvider
import okhttp3.CertificatePinner
import okhttp3.Dispatcher
import java.util.concurrent.ExecutorService
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.KeyManager
import javax.net.ssl.X509TrustManager
Expand Down Expand Up @@ -87,6 +89,13 @@ public class OkHttpEngineConfig private constructor(builder: Builder) : HttpClie
*/
public val hostnameVerifier: HostnameVerifier? = builder.hostnameVerifier

/**
* Which [ExecutorService] to use for the engine's [Dispatcher].
* Use this to customize the engine's concurrency behavior, such as enabling use of virtual threads.
* If you supply your own executor, it must be able to run [maxConcurrency] calls in parallel.
*/
public val executorService: ExecutorService? = builder.executorService

override fun toBuilderApplicator(): HttpClientEngineConfig.Builder.() -> Unit = {
super.toBuilderApplicator()()

Expand Down Expand Up @@ -159,6 +168,13 @@ public class OkHttpEngineConfig private constructor(builder: Builder) : HttpClie
*/
public var hostnameVerifier: HostnameVerifier? = null

/**
* Which [ExecutorService] to use for the engine's [Dispatcher].
* Use this to customize the engine's concurrency behavior, such as enabling use of virtual threads.
* If you supply your own executor, it must be able to run [maxConcurrency] calls in parallel.
*/
public var executorService: ExecutorService? = null

override var telemetryProvider: TelemetryProvider = TelemetryProvider.Global
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.smithy.kotlin.runtime.http.engine.okhttp

import aws.smithy.kotlin.runtime.content.ByteStream
import aws.smithy.kotlin.runtime.http.Headers
import aws.smithy.kotlin.runtime.http.HttpMethod
import aws.smithy.kotlin.runtime.http.engine.internal.HttpClientMetrics
import aws.smithy.kotlin.runtime.http.request.HttpRequest
import aws.smithy.kotlin.runtime.http.toHttpBody
import aws.smithy.kotlin.runtime.net.url.Url
import aws.smithy.kotlin.runtime.operation.ExecutionContext
import aws.smithy.kotlin.runtime.telemetry.TelemetryProvider
import kotlinx.coroutines.test.runTest
import okhttp3.coroutines.executeAsync
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import kotlin.test.Test
import kotlin.test.assertFailsWith

class OkHttpEngineConfigTest {
@Test
fun testExecutorService() = runTest {
val config = OkHttpEngineConfig {
executorService = DummyExecutorService
}

val metrics = HttpClientMetrics("foo", TelemetryProvider.None)
val engine = config.buildClient(metrics)

val data = "a".repeat(100)
val url = Url.parse("https://aws.amazon.com")
val request = HttpRequest(HttpMethod.POST, url, Headers.Empty, ByteStream.fromString(data).toHttpBody())
val okRequest = request.toOkHttpRequest(ExecutionContext(), coroutineContext, metrics)

assertFailsWith<DummyExecutorException> {
engine.newCall(okRequest).executeAsync()
}
}

private object DummyExecutorService : ExecutorService by Executors.newSingleThreadExecutor() {
override fun execute(command: Runnable?) = throw DummyExecutorException()
}

private class DummyExecutorException : Exception()
}
Loading