Skip to content

Commit a96f2f2

Browse files
chore(client): refactor closing / shutdown
1 parent c564310 commit a96f2f2

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

cas-parser-java-core/src/main/kotlin/com/cas_parser/api/client/CasParserClientAsyncImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CasParserClientAsyncImpl(private val clientOptions: ClientOptions) : CasPa
4646

4747
override fun casGenerator(): CasGeneratorServiceAsync = casGenerator
4848

49-
override fun close() = clientOptions.httpClient.close()
49+
override fun close() = clientOptions.close()
5050

5151
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
5252
CasParserClientAsync.WithRawResponse {

cas-parser-java-core/src/main/kotlin/com/cas_parser/api/client/CasParserClientImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CasParserClientImpl(private val clientOptions: ClientOptions) : CasParserC
4646

4747
override fun casGenerator(): CasGeneratorService = casGenerator
4848

49-
override fun close() = clientOptions.httpClient.close()
49+
override fun close() = clientOptions.close()
5050

5151
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
5252
CasParserClient.WithRawResponse {

cas-parser-java-core/src/main/kotlin/com/cas_parser/api/core/ClientOptions.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ private constructor(
2121
* The HTTP client to use in the SDK.
2222
*
2323
* Use the one published in `cas-parser-java-client-okhttp` or implement your own.
24+
*
25+
* This class takes ownership of the client and closes it when closed.
2426
*/
2527
@get:JvmName("httpClient") val httpClient: HttpClient,
2628
/**
@@ -157,6 +159,8 @@ private constructor(
157159
* The HTTP client to use in the SDK.
158160
*
159161
* Use the one published in `cas-parser-java-client-okhttp` or implement your own.
162+
*
163+
* This class takes ownership of the client and closes it when closed.
160164
*/
161165
fun httpClient(httpClient: HttpClient) = apply {
162166
this.httpClient = PhantomReachableClosingHttpClient(httpClient)
@@ -404,4 +408,18 @@ private constructor(
404408
)
405409
}
406410
}
411+
412+
/**
413+
* Closes these client options, relinquishing any underlying resources.
414+
*
415+
* This is purposefully not inherited from [AutoCloseable] because the client options are
416+
* long-lived and usually should not be synchronously closed via try-with-resources.
417+
*
418+
* It's also usually not necessary to call this method at all. the default client automatically
419+
* releases threads and connections if they remain idle, but if you are writing an application
420+
* that needs to aggressively release unused resources, then you may call this method.
421+
*/
422+
fun close() {
423+
httpClient.close()
424+
}
407425
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.cas_parser.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)