Commit efa9b3b
authored
Don't re-use OkHttpClient outside Kotlin notebooks (#469)
Addresses #451 by isolating the OkHttpClient-sharing behavior to Kolin
notebooks.
Tests are added to prevent regressions for both notebooks (should share
client resources by default) and elsewhere (should not share client
resources by default).
# #451 copy
Raised by @mcumings. First worked around in
[eBay/metrics-for-develocity-plugin
code](https://github.com/eBay/metrics-for-develocity-plugin/blob/6748cd762ff8c1d6bf7b1f2e4001a46f956f940e/src/main/kotlin/com/ebay/plugins/metrics/develocity/service/DevelocityBuildService.kt#L50).
The workaround was since replicated in one of the library examples,
`example-gradle-task`:
[`DevelocityApiService`](https://github.com/gabrielfeo/develocity-api-kotlin/blob/main/examples/example-gradle-task/buildSrc/src/main/kotlin/build/logic/DevelocityApiService.kt#L19-L24).
OkHttpClient internal resources (via `OkHttpClient.newBuilder`) are
re-used by default to fit the Kotlin notebook use case, in which the
notebook cell that creates an API instance (`DevelocityApi.newInstance`)
may be ran multiple times, with or without changes, in the same Kotlin
kernel. If OkHttpClient resources were not re-used, this common scenario
would result in high resource usage due to several sets of connection
pools and executors being kept. At least in the case of variable renames
(e.g. one cell executes with `val api = [...]` then value renamed to
`val develocityApi = [...]`), the old `api` variable would not get
GC'ed, since the Kotlin kernel holds on to past cell execution
variables.
Once client resources are shut down or `DevelocityApi.shutdown` is
called, however, the client can no longer be used. In the case of usage
in a Gradle plugin, this leads to the following:
```kotlin
abstract class DevelocityApiService
: DevelocityApi by DevelocityApi.newInstance(),
BuildService<BuildServiceParameters.None>,
AutoCloseable {
override fun close() {
shutdown()
}
}
```
0. Build starts
1. Gradle constructs the `BuildService`. `DevelocityApi.newInstance`
without a specified `clientBuilder` will create a new builder out of a
[static and global client
instance](https://github.com/gabrielfeo/develocity-api-kotlin/blob/cbc135a9c0d125dbeb4abbbd6ed8b9c9ff4360e3/library/src/main/kotlin/com/gabrielfeo/develocity/api/internal/OkHttpClient.kt#L21)
that is meant to support the re-using in Kotlin notebooks
3. Build ends
4. Gradle calls `close`, leading to the static and global client's
resources to be closed
Any new builds with that Gradle daemon will lead to the service being
re-created without the "static and global client instance" being
re-initialized. Since a client cannot be used after its resources
(including the executor are closed). An "executor rejected" error is
thrown by OkHttp. Can be reproduced by checking out parent of this [fix
commit](a704a53).
### Workarounds:
- Define your own `clientBuilder` as in the fixed version of
[`DevelocityApiService`](https://github.com/gabrielfeo/develocity-api-kotlin/blob/main/examples/example-gradle-task/buildSrc/src/main/kotlin/build/logic/DevelocityApiService.kt#L19-L24)
- [Untested] Don't shutdown the client (thread pool and connections
would live on in the GradleDaemon process to be re-used by the next
build, which avoids the problem and makes subsequent API requests faster
at the cost of slightly increased memory usage by the idle daemon)1 parent cbc135a commit efa9b3b
File tree
8 files changed
+91
-22
lines changed- examples/example-gradle-task/buildSrc/src/main/kotlin/build/logic
- library
- api
- src
- integrationTest/kotlin/com/gabrielfeo/develocity/api/internal/jupyter
- main/kotlin/com/gabrielfeo/develocity/api
- internal
- jupyter
- test/kotlin/com/gabrielfeo/develocity/api
8 files changed
+91
-22
lines changedLines changed: 1 addition & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
203 | 203 | | |
204 | 204 | | |
205 | 205 | | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
206 | 226 | | |
207 | 227 | | |
208 | 228 | | |
| |||
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
24 | 44 | | |
25 | 45 | | |
26 | 46 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | | - | |
| 91 | + | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| |||
Lines changed: 3 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
7 | 6 | | |
8 | | - | |
9 | 7 | | |
10 | 8 | | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
14 | | - | |
15 | 12 | | |
16 | 13 | | |
17 | 14 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | 15 | | |
26 | 16 | | |
27 | 17 | | |
| |||
Lines changed: 28 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
16 | 26 | | |
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
83 | 91 | | |
84 | 92 | | |
85 | 93 | | |
| |||
0 commit comments