Skip to content

Commit a9a7732

Browse files
chore(client): refactor closing / shutdown
1 parent 9095865 commit a9a7732

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

scrapegraphai-java-core/src/main/kotlin/com/scrapegraphai/api/client/ScrapegraphaiClientAsyncImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class ScrapegraphaiClientAsyncImpl(private val clientOptions: ClientOptions) :
103103

104104
override fun healthz(): HealthzServiceAsync = healthz
105105

106-
override fun close() = clientOptions.httpClient.close()
106+
override fun close() = clientOptions.close()
107107

108108
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
109109
ScrapegraphaiClientAsync.WithRawResponse {

scrapegraphai-java-core/src/main/kotlin/com/scrapegraphai/api/client/ScrapegraphaiClientImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class ScrapegraphaiClientImpl(private val clientOptions: ClientOptions) : Scrape
9898

9999
override fun healthz(): HealthzService = healthz
100100

101-
override fun close() = clientOptions.httpClient.close()
101+
override fun close() = clientOptions.close()
102102

103103
class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
104104
ScrapegraphaiClient.WithRawResponse {

scrapegraphai-java-core/src/main/kotlin/com/scrapegraphai/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 `scrapegraphai-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
/**
@@ -162,6 +164,8 @@ private constructor(
162164
* The HTTP client to use in the SDK.
163165
*
164166
* Use the one published in `scrapegraphai-java-client-okhttp` or implement your own.
167+
*
168+
* This class takes ownership of the client and closes it when closed.
165169
*/
166170
fun httpClient(httpClient: HttpClient) = apply {
167171
this.httpClient = PhantomReachableClosingHttpClient(httpClient)
@@ -413,4 +417,18 @@ private constructor(
413417
)
414418
}
415419
}
420+
421+
/**
422+
* Closes these client options, relinquishing any underlying resources.
423+
*
424+
* This is purposefully not inherited from [AutoCloseable] because the client options are
425+
* long-lived and usually should not be synchronously closed via try-with-resources.
426+
*
427+
* It's also usually not necessary to call this method at all. the default client automatically
428+
* releases threads and connections if they remain idle, but if you are writing an application
429+
* that needs to aggressively release unused resources, then you may call this method.
430+
*/
431+
fun close() {
432+
httpClient.close()
433+
}
416434
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.scrapegraphai.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)