Skip to content

Commit 94cdfcd

Browse files
chore(client): refactor closing / shutdown
1 parent 6f9c783 commit 94cdfcd

File tree

6 files changed

+92
-3
lines changed

6 files changed

+92
-3
lines changed

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class OpenAIOkHttpClient private constructor() {
128128
* The executor to use for running [AsyncStreamResponse.Handler] callbacks.
129129
*
130130
* Defaults to a dedicated cached thread pool.
131+
*
132+
* This class takes ownership of the executor and shuts it down, if possible, when closed.
131133
*/
132134
fun streamHandlerExecutor(streamHandlerExecutor: Executor) = apply {
133135
clientOptions.streamHandlerExecutor(streamHandlerExecutor)

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class OpenAIOkHttpClientAsync private constructor() {
128128
* The executor to use for running [AsyncStreamResponse.Handler] callbacks.
129129
*
130130
* Defaults to a dedicated cached thread pool.
131+
*
132+
* This class takes ownership of the executor and shuts it down, if possible, when closed.
131133
*/
132134
fun streamHandlerExecutor(streamHandlerExecutor: Executor) = apply {
133135
clientOptions.streamHandlerExecutor(streamHandlerExecutor)

openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsyncImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class OpenAIClientAsyncImpl(private val clientOptions: ClientOptions) : OpenAICl
166166

167167
override fun containers(): ContainerServiceAsync = containers
168168

169-
override fun close() = clientOptions.httpClient.close()
169+
override fun close() = clientOptions.close()
170170

171171
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
172172
OpenAIClientAsync.WithRawResponse {

openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class OpenAIClientImpl(private val clientOptions: ClientOptions) : OpenAIClient
152152

153153
override fun containers(): ContainerService = containers
154154

155-
override fun close() = clientOptions.httpClient.close()
155+
override fun close() = clientOptions.close()
156156

157157
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
158158
OpenAIClient.WithRawResponse {

openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import java.time.Clock
1919
import java.time.Duration
2020
import java.util.Optional
2121
import java.util.concurrent.Executor
22+
import java.util.concurrent.ExecutorService
2223
import java.util.concurrent.Executors
2324
import java.util.concurrent.ThreadFactory
2425
import java.util.concurrent.atomic.AtomicLong
@@ -32,6 +33,8 @@ private constructor(
3233
* The HTTP client to use in the SDK.
3334
*
3435
* Use the one published in `openai-java-client-okhttp` or implement your own.
36+
*
37+
* This class takes ownership of the client and closes it when closed.
3538
*/
3639
@get:JvmName("httpClient") val httpClient: HttpClient,
3740
/**
@@ -53,6 +56,8 @@ private constructor(
5356
* The executor to use for running [AsyncStreamResponse.Handler] callbacks.
5457
*
5558
* Defaults to a dedicated cached thread pool.
59+
*
60+
* This class takes ownership of the executor and shuts it down, if possible, when closed.
5661
*/
5762
@get:JvmName("streamHandlerExecutor") val streamHandlerExecutor: Executor,
5863
/**
@@ -196,6 +201,8 @@ private constructor(
196201
* The HTTP client to use in the SDK.
197202
*
198203
* Use the one published in `openai-java-client-okhttp` or implement your own.
204+
*
205+
* This class takes ownership of the client and closes it when closed.
199206
*/
200207
fun httpClient(httpClient: HttpClient) = apply {
201208
this.httpClient = PhantomReachableClosingHttpClient(httpClient)
@@ -224,9 +231,14 @@ private constructor(
224231
* The executor to use for running [AsyncStreamResponse.Handler] callbacks.
225232
*
226233
* Defaults to a dedicated cached thread pool.
234+
*
235+
* This class takes ownership of the executor and shuts it down, if possible, when closed.
227236
*/
228237
fun streamHandlerExecutor(streamHandlerExecutor: Executor) = apply {
229-
this.streamHandlerExecutor = streamHandlerExecutor
238+
this.streamHandlerExecutor =
239+
if (streamHandlerExecutor is ExecutorService)
240+
PhantomReachableExecutorService(streamHandlerExecutor)
241+
else streamHandlerExecutor
230242
}
231243

232244
/**
@@ -554,4 +566,19 @@ private constructor(
554566
)
555567
}
556568
}
569+
570+
/**
571+
* Closes these client options, relinquishing any underlying resources.
572+
*
573+
* This is purposefully not inherited from [AutoCloseable] because the client options are
574+
* long-lived and usually should not be synchronously closed via try-with-resources.
575+
*
576+
* It's also usually not necessary to call this method at all. the default client automatically
577+
* releases threads and connections if they remain idle, but if you are writing an application
578+
* that needs to aggressively release unused resources, then you may call this method.
579+
*/
580+
fun close() {
581+
httpClient.close()
582+
(streamHandlerExecutor as? ExecutorService)?.shutdown()
583+
}
557584
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.openai.core
2+
3+
import java.util.concurrent.Callable
4+
import java.util.concurrent.ExecutorService
5+
import java.util.concurrent.Future
6+
import java.util.concurrent.TimeUnit
7+
8+
/**
9+
* A delegating wrapper around an [ExecutorService] that shuts it down once it's only phantom
10+
* reachable.
11+
*
12+
* This class ensures the [ExecutorService] is shut down even if the user forgets to do it.
13+
*/
14+
internal class PhantomReachableExecutorService(private val executorService: ExecutorService) :
15+
ExecutorService {
16+
init {
17+
closeWhenPhantomReachable(this) { executorService.shutdown() }
18+
}
19+
20+
override fun execute(command: Runnable) = executorService.execute(command)
21+
22+
override fun shutdown() = executorService.shutdown()
23+
24+
override fun shutdownNow(): MutableList<Runnable> = executorService.shutdownNow()
25+
26+
override fun isShutdown(): Boolean = executorService.isShutdown
27+
28+
override fun isTerminated(): Boolean = executorService.isTerminated
29+
30+
override fun awaitTermination(timeout: Long, unit: TimeUnit): Boolean =
31+
executorService.awaitTermination(timeout, unit)
32+
33+
override fun <T : Any?> submit(task: Callable<T>): Future<T> = executorService.submit(task)
34+
35+
override fun <T : Any?> submit(task: Runnable, result: T): Future<T> =
36+
executorService.submit(task, result)
37+
38+
override fun submit(task: Runnable): Future<*> = executorService.submit(task)
39+
40+
override fun <T : Any?> invokeAll(
41+
tasks: MutableCollection<out Callable<T>>
42+
): MutableList<Future<T>> = executorService.invokeAll(tasks)
43+
44+
override fun <T : Any?> invokeAll(
45+
tasks: MutableCollection<out Callable<T>>,
46+
timeout: Long,
47+
unit: TimeUnit,
48+
): MutableList<Future<T>> = executorService.invokeAll(tasks, timeout, unit)
49+
50+
override fun <T : Any?> invokeAny(tasks: MutableCollection<out Callable<T>>): T =
51+
executorService.invokeAny(tasks)
52+
53+
override fun <T : Any?> invokeAny(
54+
tasks: MutableCollection<out Callable<T>>,
55+
timeout: Long,
56+
unit: TimeUnit,
57+
): T = executorService.invokeAny(tasks, timeout, unit)
58+
}

0 commit comments

Comments
 (0)