Skip to content

Commit ffdb0de

Browse files
chore(client): refactor closing / shutdown
1 parent f883c39 commit ffdb0de

6 files changed

Lines changed: 92 additions & 3 deletions

File tree

lithic-java-client-okhttp/src/main/kotlin/com/lithic/api/client/okhttp/LithicOkHttpClient.kt

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

lithic-java-client-okhttp/src/main/kotlin/com/lithic/api/client/okhttp/LithicOkHttpClientAsync.kt

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

lithic-java-core/src/main/kotlin/com/lithic/api/client/LithicClientAsyncImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class LithicClientAsyncImpl(private val clientOptions: ClientOptions) : LithicCl
288288
// get /v1/status
289289
withRawResponse().apiStatus(params, requestOptions).thenApply { it.parse() }
290290

291-
override fun close() = clientOptions.httpClient.close()
291+
override fun close() = clientOptions.close()
292292

293293
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
294294
LithicClientAsync.WithRawResponse {

lithic-java-core/src/main/kotlin/com/lithic/api/client/LithicClientImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class LithicClientImpl(private val clientOptions: ClientOptions) : LithicClient
270270
// get /v1/status
271271
withRawResponse().apiStatus(params, requestOptions).parse()
272272

273-
override fun close() = clientOptions.httpClient.close()
273+
override fun close() = clientOptions.close()
274274

275275
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
276276
LithicClient.WithRawResponse {

lithic-java-core/src/main/kotlin/com/lithic/api/core/ClientOptions.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import java.time.Clock
1313
import java.time.Duration
1414
import java.util.Optional
1515
import java.util.concurrent.Executor
16+
import java.util.concurrent.ExecutorService
1617
import java.util.concurrent.Executors
1718
import java.util.concurrent.ThreadFactory
1819
import java.util.concurrent.atomic.AtomicLong
@@ -26,6 +27,8 @@ private constructor(
2627
* The HTTP client to use in the SDK.
2728
*
2829
* Use the one published in `lithic-java-client-okhttp` or implement your own.
30+
*
31+
* This class takes ownership of the client and closes it when closed.
2932
*/
3033
@get:JvmName("httpClient") val httpClient: HttpClient,
3134
/**
@@ -47,6 +50,8 @@ private constructor(
4750
* The executor to use for running [AsyncStreamResponse.Handler] callbacks.
4851
*
4952
* Defaults to a dedicated cached thread pool.
53+
*
54+
* This class takes ownership of the executor and shuts it down, if possible, when closed.
5055
*/
5156
@get:JvmName("streamHandlerExecutor") val streamHandlerExecutor: Executor,
5257
/**
@@ -179,6 +184,8 @@ private constructor(
179184
* The HTTP client to use in the SDK.
180185
*
181186
* Use the one published in `lithic-java-client-okhttp` or implement your own.
187+
*
188+
* This class takes ownership of the client and closes it when closed.
182189
*/
183190
fun httpClient(httpClient: HttpClient) = apply {
184191
this.httpClient = PhantomReachableClosingHttpClient(httpClient)
@@ -207,9 +214,14 @@ private constructor(
207214
* The executor to use for running [AsyncStreamResponse.Handler] callbacks.
208215
*
209216
* Defaults to a dedicated cached thread pool.
217+
*
218+
* This class takes ownership of the executor and shuts it down, if possible, when closed.
210219
*/
211220
fun streamHandlerExecutor(streamHandlerExecutor: Executor) = apply {
212-
this.streamHandlerExecutor = streamHandlerExecutor
221+
this.streamHandlerExecutor =
222+
if (streamHandlerExecutor is ExecutorService)
223+
PhantomReachableExecutorService(streamHandlerExecutor)
224+
else streamHandlerExecutor
213225
}
214226

215227
/**
@@ -466,4 +478,19 @@ private constructor(
466478
)
467479
}
468480
}
481+
482+
/**
483+
* Closes these client options, relinquishing any underlying resources.
484+
*
485+
* This is purposefully not inherited from [AutoCloseable] because the client options are
486+
* long-lived and usually should not be synchronously closed via try-with-resources.
487+
*
488+
* It's also usually not necessary to call this method at all. the default client automatically
489+
* releases threads and connections if they remain idle, but if you are writing an application
490+
* that needs to aggressively release unused resources, then you may call this method.
491+
*/
492+
fun close() {
493+
httpClient.close()
494+
(streamHandlerExecutor as? ExecutorService)?.shutdown()
495+
}
469496
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.lithic.api.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)