Skip to content

Commit b67a0d6

Browse files
feat(client): allow configuring dispatcher executor service
1 parent 2481fc6 commit b67a0d6

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

stagehand-java-client-okhttp/src/main/kotlin/com/browserbase/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
@@ -202,6 +204,7 @@ private constructor(@JvmSynthetic internal val okHttpClient: okhttp3.OkHttpClien
202204

203205
private var timeout: Timeout = Timeout.default()
204206
private var proxy: Proxy? = null
207+
private var dispatcherExecutorService: ExecutorService? = null
205208
private var sslSocketFactory: SSLSocketFactory? = null
206209
private var trustManager: X509TrustManager? = null
207210
private var hostnameVerifier: HostnameVerifier? = null
@@ -212,6 +215,10 @@ private constructor(@JvmSynthetic internal val okHttpClient: okhttp3.OkHttpClien
212215

213216
fun proxy(proxy: Proxy?) = apply { this.proxy = proxy }
214217

218+
fun dispatcherExecutorService(dispatcherExecutorService: ExecutorService?) = apply {
219+
this.dispatcherExecutorService = dispatcherExecutorService
220+
}
221+
215222
fun sslSocketFactory(sslSocketFactory: SSLSocketFactory?) = apply {
216223
this.sslSocketFactory = sslSocketFactory
217224
}
@@ -233,6 +240,8 @@ private constructor(@JvmSynthetic internal val okHttpClient: okhttp3.OkHttpClien
233240
.callTimeout(timeout.request())
234241
.proxy(proxy)
235242
.apply {
243+
dispatcherExecutorService?.let { dispatcher(Dispatcher(it)) }
244+
236245
val sslSocketFactory = sslSocketFactory
237246
val trustManager = trustManager
238247
if (sslSocketFactory != null && trustManager != null) {

stagehand-java-client-okhttp/src/main/kotlin/com/browserbase/api/client/okhttp/StagehandOkHttpClient.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 StagehandOkHttpClient 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)`. */
@@ -320,6 +341,7 @@ class StagehandOkHttpClient private constructor() {
320341
OkHttpClient.builder()
321342
.timeout(clientOptions.timeout())
322343
.proxy(proxy)
344+
.dispatcherExecutorService(dispatcherExecutorService)
323345
.sslSocketFactory(sslSocketFactory)
324346
.trustManager(trustManager)
325347
.hostnameVerifier(hostnameVerifier)

stagehand-java-client-okhttp/src/main/kotlin/com/browserbase/api/client/okhttp/StagehandOkHttpClientAsync.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 StagehandOkHttpClientAsync 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)`. */
@@ -320,6 +341,7 @@ class StagehandOkHttpClientAsync private constructor() {
320341
OkHttpClient.builder()
321342
.timeout(clientOptions.timeout())
322343
.proxy(proxy)
344+
.dispatcherExecutorService(dispatcherExecutorService)
323345
.sslSocketFactory(sslSocketFactory)
324346
.trustManager(trustManager)
325347
.hostnameVerifier(hostnameVerifier)

0 commit comments

Comments
 (0)