Skip to content

Commit 2d2114c

Browse files
authored
Share clientBuilder between Config constructor calls (#70)
1 parent 5e48330 commit 2d2114c

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Config.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ data class Config(
3737
},
3838

3939
/**
40-
* Provider of an [OkHttpClient.Builder] to use when building the library's internal client.
41-
* Has a default value and shouldn't be needed in scripts.
40+
* [OkHttpClient.Builder] to use when building the library's internal [OkHttpClient].
4241
*
43-
* This is aimed at using the library inside a full Kotlin project. Allows the internal client to
44-
* share resources such as thread pools with another [OkHttpClient], useful for full Kotlin projects
45-
* and rarely needed for scripting. See [OkHttpClient] for all that is shared.
42+
* This is aimed at using the library inside a full Kotlin project. Allows the internal client
43+
* to share resources such as thread pools with another [OkHttpClient]. See [OkHttpClient]
44+
* for all that is shared.
45+
*
46+
* The default is to share resources only within the library, i.e. multiple `Config()` with
47+
* the default [clientBuilder] will already share resources.
4648
*/
47-
val clientBuilder: OkHttpClient.Builder =
48-
OkHttpClient.Builder(),
49+
val clientBuilder: OkHttpClient.Builder = basicOkHttpClient.newBuilder(),
4950

5051
/**
5152
* Maximum amount of concurrent requests allowed. Further requests will be queued. By default,

library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/OkHttpClient.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ import java.time.Duration
1212
import java.util.logging.Level
1313
import java.util.logging.Logger
1414

15+
/**
16+
* Base instance just so that multiple created [Config]s will share resources by default.
17+
*/
18+
internal val basicOkHttpClient by lazy {
19+
OkHttpClient.Builder().build()
20+
}
21+
22+
/**
23+
* Builds the final `OkHttpClient` with a `Config`.
24+
*/
1525
internal fun buildOkHttpClient(
1626
config: Config,
1727
) = with(config.clientBuilder) {

0 commit comments

Comments
 (0)