Skip to content

Commit 14307ff

Browse files
feat(client): allow configuring dispatcher executor service
1 parent 268bca0 commit 14307ff

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

orb-java-client-okhttp/src/main/kotlin/com/withorb/api/client/okhttp/OkHttpClient.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import java.net.Proxy
1515
import java.time.Duration
1616
import java.util.concurrent.CancellationException
1717
import java.util.concurrent.CompletableFuture
18+
import java.util.concurrent.ExecutorService
1819
import javax.net.ssl.HostnameVerifier
1920
import javax.net.ssl.SSLSocketFactory
2021
import javax.net.ssl.X509TrustManager
2122
import okhttp3.Call
2223
import okhttp3.Callback
24+
import okhttp3.Dispatcher
2325
import okhttp3.HttpUrl.Companion.toHttpUrl
2426
import okhttp3.MediaType
2527
import okhttp3.MediaType.Companion.toMediaType
@@ -198,6 +200,7 @@ private constructor(@JvmSynthetic internal val okHttpClient: okhttp3.OkHttpClien
198200

199201
private var timeout: Timeout = Timeout.default()
200202
private var proxy: Proxy? = null
203+
private var dispatcherExecutorService: ExecutorService? = null
201204
private var sslSocketFactory: SSLSocketFactory? = null
202205
private var trustManager: X509TrustManager? = null
203206
private var hostnameVerifier: HostnameVerifier? = null
@@ -208,6 +211,10 @@ private constructor(@JvmSynthetic internal val okHttpClient: okhttp3.OkHttpClien
208211

209212
fun proxy(proxy: Proxy?) = apply { this.proxy = proxy }
210213

214+
fun dispatcherExecutorService(dispatcherExecutorService: ExecutorService?) = apply {
215+
this.dispatcherExecutorService = dispatcherExecutorService
216+
}
217+
211218
fun sslSocketFactory(sslSocketFactory: SSLSocketFactory?) = apply {
212219
this.sslSocketFactory = sslSocketFactory
213220
}
@@ -229,6 +236,8 @@ private constructor(@JvmSynthetic internal val okHttpClient: okhttp3.OkHttpClien
229236
.callTimeout(timeout.request())
230237
.proxy(proxy)
231238
.apply {
239+
dispatcherExecutorService?.let { dispatcher(Dispatcher(it)) }
240+
232241
val sslSocketFactory = sslSocketFactory
233242
val trustManager = trustManager
234243
if (sslSocketFactory != null && trustManager != null) {

orb-java-client-okhttp/src/main/kotlin/com/withorb/api/client/okhttp/OrbOkHttpClient.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import java.time.Clock
1818
import java.time.Duration
1919
import java.util.Optional
2020
import java.util.concurrent.Executor
21+
import java.util.concurrent.ExecutorService
2122
import javax.net.ssl.HostnameVerifier
2223
import javax.net.ssl.SSLSocketFactory
2324
import javax.net.ssl.X509TrustManager
@@ -46,11 +47,31 @@ class OrbOkHttpClient private constructor() {
4647
class Builder internal constructor() {
4748

4849
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
50+
private var dispatcherExecutorService: ExecutorService? = null
4951
private var proxy: Proxy? = null
5052
private var sslSocketFactory: SSLSocketFactory? = null
5153
private var trustManager: X509TrustManager? = null
5254
private var hostnameVerifier: HostnameVerifier? = null
5355

56+
/**
57+
* The executor service to use for running HTTP requests.
58+
*
59+
* Defaults to OkHttp's
60+
* [default executor service](https://github.com/square/okhttp/blob/ace792f443b2ffb17974f5c0d1cecdf589309f26/okhttp/src/commonJvmAndroid/kotlin/okhttp3/Dispatcher.kt#L98-L104).
61+
*
62+
* This class takes ownership of the executor service and shuts it down when closed.
63+
*/
64+
fun dispatcherExecutorService(dispatcherExecutorService: ExecutorService?) = apply {
65+
this.dispatcherExecutorService = dispatcherExecutorService
66+
}
67+
68+
/**
69+
* Alias for calling [Builder.dispatcherExecutorService] with
70+
* `dispatcherExecutorService.orElse(null)`.
71+
*/
72+
fun dispatcherExecutorService(dispatcherExecutorService: Optional<ExecutorService>) =
73+
dispatcherExecutorService(dispatcherExecutorService.getOrNull())
74+
5475
fun proxy(proxy: Proxy?) = apply { this.proxy = proxy }
5576

5677
/** Alias for calling [Builder.proxy] with `proxy.orElse(null)`. */
@@ -317,6 +338,7 @@ class OrbOkHttpClient private constructor() {
317338
OkHttpClient.builder()
318339
.timeout(clientOptions.timeout())
319340
.proxy(proxy)
341+
.dispatcherExecutorService(dispatcherExecutorService)
320342
.sslSocketFactory(sslSocketFactory)
321343
.trustManager(trustManager)
322344
.hostnameVerifier(hostnameVerifier)

orb-java-client-okhttp/src/main/kotlin/com/withorb/api/client/okhttp/OrbOkHttpClientAsync.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import java.time.Clock
1818
import java.time.Duration
1919
import java.util.Optional
2020
import java.util.concurrent.Executor
21+
import java.util.concurrent.ExecutorService
2122
import javax.net.ssl.HostnameVerifier
2223
import javax.net.ssl.SSLSocketFactory
2324
import javax.net.ssl.X509TrustManager
@@ -46,11 +47,31 @@ class OrbOkHttpClientAsync private constructor() {
4647
class Builder internal constructor() {
4748

4849
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
50+
private var dispatcherExecutorService: ExecutorService? = null
4951
private var proxy: Proxy? = null
5052
private var sslSocketFactory: SSLSocketFactory? = null
5153
private var trustManager: X509TrustManager? = null
5254
private var hostnameVerifier: HostnameVerifier? = null
5355

56+
/**
57+
* The executor service to use for running HTTP requests.
58+
*
59+
* Defaults to OkHttp's
60+
* [default executor service](https://github.com/square/okhttp/blob/ace792f443b2ffb17974f5c0d1cecdf589309f26/okhttp/src/commonJvmAndroid/kotlin/okhttp3/Dispatcher.kt#L98-L104).
61+
*
62+
* This class takes ownership of the executor service and shuts it down when closed.
63+
*/
64+
fun dispatcherExecutorService(dispatcherExecutorService: ExecutorService?) = apply {
65+
this.dispatcherExecutorService = dispatcherExecutorService
66+
}
67+
68+
/**
69+
* Alias for calling [Builder.dispatcherExecutorService] with
70+
* `dispatcherExecutorService.orElse(null)`.
71+
*/
72+
fun dispatcherExecutorService(dispatcherExecutorService: Optional<ExecutorService>) =
73+
dispatcherExecutorService(dispatcherExecutorService.getOrNull())
74+
5475
fun proxy(proxy: Proxy?) = apply { this.proxy = proxy }
5576

5677
/** Alias for calling [Builder.proxy] with `proxy.orElse(null)`. */
@@ -317,6 +338,7 @@ class OrbOkHttpClientAsync private constructor() {
317338
OkHttpClient.builder()
318339
.timeout(clientOptions.timeout())
319340
.proxy(proxy)
341+
.dispatcherExecutorService(dispatcherExecutorService)
320342
.sslSocketFactory(sslSocketFactory)
321343
.trustManager(trustManager)
322344
.hostnameVerifier(hostnameVerifier)

0 commit comments

Comments
 (0)