Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share clientBuilder between Config constructor calls #70

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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