Skip to content

Commit

Permalink
Share clientBuilder between Config constructor calls (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfeo authored May 22, 2023
1 parent 5e48330 commit 2d2114c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ data class Config(
},

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

/**
* Maximum amount of concurrent requests allowed. Further requests will be queued. By default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import java.time.Duration
import java.util.logging.Level
import java.util.logging.Logger

/**
* Base instance just so that multiple created [Config]s will share resources by default.
*/
internal val basicOkHttpClient by lazy {
OkHttpClient.Builder().build()
}

/**
* Builds the final `OkHttpClient` with a `Config`.
*/
internal fun buildOkHttpClient(
config: Config,
) = with(config.clientBuilder) {
Expand Down

0 comments on commit 2d2114c

Please sign in to comment.