Skip to content

Commit 133f431

Browse files
chore(client): refactor closing / shutdown
1 parent 95a334a commit 133f431

File tree

6 files changed

+92
-3
lines changed

6 files changed

+92
-3
lines changed

finch-java-client-okhttp/src/main/kotlin/com/tryfinch/api/client/okhttp/FinchOkHttpClient.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ class FinchOkHttpClient 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)

finch-java-client-okhttp/src/main/kotlin/com/tryfinch/api/client/okhttp/FinchOkHttpClientAsync.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ class FinchOkHttpClientAsync 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)

finch-java-core/src/main/kotlin/com/tryfinch/api/client/FinchClientAsyncImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class FinchClientAsyncImpl(private val clientOptions: ClientOptions) : FinchClie
200200
@get:JsonProperty("provider_id") val providerId: String,
201201
)
202202

203-
override fun close() = clientOptions.httpClient.close()
203+
override fun close() = clientOptions.close()
204204

205205
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
206206
FinchClientAsync.WithRawResponse {

finch-java-core/src/main/kotlin/com/tryfinch/api/client/FinchClientImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class FinchClientImpl(private val clientOptions: ClientOptions) : FinchClient {
189189
@get:JsonProperty("provider_id") val providerId: String,
190190
)
191191

192-
override fun close() = clientOptions.httpClient.close()
192+
override fun close() = clientOptions.close()
193193

194194
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
195195
FinchClient.WithRawResponse {

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import java.time.Duration
1414
import java.util.Base64
1515
import java.util.Optional
1616
import java.util.concurrent.Executor
17+
import java.util.concurrent.ExecutorService
1718
import java.util.concurrent.Executors
1819
import java.util.concurrent.ThreadFactory
1920
import java.util.concurrent.atomic.AtomicLong
@@ -27,6 +28,8 @@ private constructor(
2728
* The HTTP client to use in the SDK.
2829
*
2930
* Use the one published in `finch-java-client-okhttp` or implement your own.
31+
*
32+
* This class takes ownership of the client and closes it when closed.
3033
*/
3134
@get:JvmName("httpClient") val httpClient: HttpClient,
3235
/**
@@ -48,6 +51,8 @@ private constructor(
4851
* The executor to use for running [AsyncStreamResponse.Handler] callbacks.
4952
*
5053
* Defaults to a dedicated cached thread pool.
54+
*
55+
* This class takes ownership of the executor and shuts it down, if possible, when closed.
5156
*/
5257
@get:JvmName("streamHandlerExecutor") val streamHandlerExecutor: Executor,
5358
/**
@@ -186,6 +191,8 @@ private constructor(
186191
* The HTTP client to use in the SDK.
187192
*
188193
* Use the one published in `finch-java-client-okhttp` or implement your own.
194+
*
195+
* This class takes ownership of the client and closes it when closed.
189196
*/
190197
fun httpClient(httpClient: HttpClient) = apply {
191198
this.httpClient = PhantomReachableClosingHttpClient(httpClient)
@@ -214,9 +221,14 @@ private constructor(
214221
* The executor to use for running [AsyncStreamResponse.Handler] callbacks.
215222
*
216223
* Defaults to a dedicated cached thread pool.
224+
*
225+
* This class takes ownership of the executor and shuts it down, if possible, when closed.
217226
*/
218227
fun streamHandlerExecutor(streamHandlerExecutor: Executor) = apply {
219-
this.streamHandlerExecutor = streamHandlerExecutor
228+
this.streamHandlerExecutor =
229+
if (streamHandlerExecutor is ExecutorService)
230+
PhantomReachableExecutorService(streamHandlerExecutor)
231+
else streamHandlerExecutor
220232
}
221233

222234
/**
@@ -493,4 +505,19 @@ private constructor(
493505
)
494506
}
495507
}
508+
509+
/**
510+
* Closes these client options, relinquishing any underlying resources.
511+
*
512+
* This is purposefully not inherited from [AutoCloseable] because the client options are
513+
* long-lived and usually should not be synchronously closed via try-with-resources.
514+
*
515+
* It's also usually not necessary to call this method at all. the default client automatically
516+
* releases threads and connections if they remain idle, but if you are writing an application
517+
* that needs to aggressively release unused resources, then you may call this method.
518+
*/
519+
fun close() {
520+
httpClient.close()
521+
(streamHandlerExecutor as? ExecutorService)?.shutdown()
522+
}
496523
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.tryfinch.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)