Skip to content

Commit

Permalink
release: 0.18.1 (#186)
Browse files Browse the repository at this point in the history
* fix(client): don't leak responses when retrying (#185)

* release: 0.18.1

---------

Co-authored-by: stainless-app[bot] <142633134+stainless-app[bot]@users.noreply.github.com>
  • Loading branch information
stainless-app[bot] authored Jan 30, 2025
1 parent e453789 commit 41309c1
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.18.0"
".": "0.18.1"
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.18.1 (2025-01-29)

Full Changelog: [v0.18.0...v0.18.1](https://github.com/openai/openai-java/compare/v0.18.0...v0.18.1)

### Bug Fixes

* **client:** don't leak responses when retrying ([#185](https://github.com/openai/openai-java/issues/185)) ([a13f967](https://github.com/openai/openai-java/commit/a13f967d4406cb447b14260c5435f77d91b6b1be))

## 0.18.0 (2025-01-29)

Full Changelog: [v0.17.0...v0.18.0](https://github.com/openai/openai-java/compare/v0.17.0...v0.18.0)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<!-- x-release-please-start-version -->

[![Maven Central](https://img.shields.io/maven-central/v/com.openai/openai-java)](https://central.sonatype.com/artifact/com.openai/openai-java/0.18.0)
[![javadoc](https://javadoc.io/badge2/com.openai/openai-java/0.18.0/javadoc.svg)](https://javadoc.io/doc/com.openai/openai-java/0.18.0)
[![Maven Central](https://img.shields.io/maven-central/v/com.openai/openai-java)](https://central.sonatype.com/artifact/com.openai/openai-java/0.18.1)
[![javadoc](https://javadoc.io/badge2/com.openai/openai-java/0.18.1/javadoc.svg)](https://javadoc.io/doc/com.openai/openai-java/0.18.1)

<!-- x-release-please-end -->

Expand All @@ -25,7 +25,7 @@ The REST API documentation can be found on [platform.openai.com](https://platfor
### Gradle

```kotlin
implementation("com.openai:openai-java:0.18.0")
implementation("com.openai:openai-java:0.18.1")
```

### Maven
Expand All @@ -34,7 +34,7 @@ implementation("com.openai:openai-java:0.18.0")
<dependency>
<groupId>com.openai</groupId>
<artifactId>openai-java</artifactId>
<version>0.18.0</version>
<version>0.18.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repositories {

allprojects {
group = "com.openai"
version = "0.18.0" // x-release-please-version
version = "0.18.1" // x-release-please-version
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ private constructor(
}

response
} catch (t: Throwable) {
if (++retries > maxRetries || !shouldRetry(t)) {
throw t
} catch (throwable: Throwable) {
if (++retries > maxRetries || !shouldRetry(throwable)) {
throw throwable
}

null
}

val backoffMillis = getRetryBackoffMillis(retries, response)
// All responses must be closed, so close the failed one before retrying.
response?.close()
Thread.sleep(backoffMillis.toMillis())
}
}
Expand Down Expand Up @@ -113,6 +115,8 @@ private constructor(
}

val backoffMillis = getRetryBackoffMillis(retries, response)
// All responses must be closed, so close the failed one before retrying.
response?.close()
return sleepAsync(backoffMillis.toMillis()).thenCompose {
executeWithRetries(requestWithRetryCount, requestOptions)
}
Expand Down Expand Up @@ -223,23 +227,23 @@ private constructor(
return Duration.ofNanos((TimeUnit.SECONDS.toNanos(1) * backoffSeconds * jitter).toLong())
}

private fun sleepAsync(millis: Long): CompletableFuture<Void> {
val future = CompletableFuture<Void>()
TIMER.schedule(
object : TimerTask() {
override fun run() {
future.complete(null)
}
},
millis
)
return future
}

companion object {

private val TIMER = Timer("RetryingHttpClient", true)

private fun sleepAsync(millis: Long): CompletableFuture<Void> {
val future = CompletableFuture<Void>()
TIMER.schedule(
object : TimerTask() {
override fun run() {
future.complete(null)
}
},
millis
)
return future
}

@JvmStatic fun builder() = Builder()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo
import com.github.tomakehurst.wiremock.junit5.WireMockTest
import com.github.tomakehurst.wiremock.stubbing.Scenario
import com.openai.client.okhttp.OkHttpClient
import com.openai.core.RequestOptions
import java.io.InputStream
import java.util.concurrent.CompletableFuture
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.params.ParameterizedTest
Expand All @@ -13,11 +16,49 @@ import org.junit.jupiter.params.provider.ValueSource
@WireMockTest
internal class RetryingHttpClientTest {

private var openResponseCount = 0
private lateinit var httpClient: HttpClient

@BeforeEach
fun beforeEach(wmRuntimeInfo: WireMockRuntimeInfo) {
httpClient = OkHttpClient.builder().baseUrl(wmRuntimeInfo.httpBaseUrl).build()
val okHttpClient = OkHttpClient.builder().baseUrl(wmRuntimeInfo.httpBaseUrl).build()
httpClient =
object : HttpClient {
override fun execute(
request: HttpRequest,
requestOptions: RequestOptions
): HttpResponse = trackClose(okHttpClient.execute(request, requestOptions))

override fun executeAsync(
request: HttpRequest,
requestOptions: RequestOptions
): CompletableFuture<HttpResponse> =
okHttpClient.executeAsync(request, requestOptions).thenApply { trackClose(it) }

override fun close() = okHttpClient.close()

private fun trackClose(response: HttpResponse): HttpResponse {
openResponseCount++
return object : HttpResponse {
private var isClosed = false

override fun statusCode(): Int = response.statusCode()

override fun headers(): Headers = response.headers()

override fun body(): InputStream = response.body()

override fun close() {
response.close()
if (isClosed) {
return
}
openResponseCount--
isClosed = true
}
}
}
}
resetAllScenarios()
}

Expand All @@ -35,6 +76,7 @@ internal class RetryingHttpClientTest {

assertThat(response.statusCode()).isEqualTo(200)
verify(1, postRequestedFor(urlPathEqualTo("/something")))
assertNoResponseLeaks()
}

@ParameterizedTest
Expand All @@ -60,6 +102,7 @@ internal class RetryingHttpClientTest {

assertThat(response.statusCode()).isEqualTo(200)
verify(1, postRequestedFor(urlPathEqualTo("/something")))
assertNoResponseLeaks()
}

@ParameterizedTest
Expand Down Expand Up @@ -116,6 +159,7 @@ internal class RetryingHttpClientTest {
postRequestedFor(urlPathEqualTo("/something"))
.withHeader("x-stainless-retry-count", equalTo("2"))
)
assertNoResponseLeaks()
}

@ParameterizedTest
Expand Down Expand Up @@ -156,6 +200,7 @@ internal class RetryingHttpClientTest {
postRequestedFor(urlPathEqualTo("/something"))
.withHeader("x-stainless-retry-count", equalTo("42"))
)
assertNoResponseLeaks()
}

@ParameterizedTest
Expand Down Expand Up @@ -186,8 +231,13 @@ internal class RetryingHttpClientTest {

assertThat(response.statusCode()).isEqualTo(200)
verify(2, postRequestedFor(urlPathEqualTo("/something")))
assertNoResponseLeaks()
}

private fun HttpClient.execute(request: HttpRequest, async: Boolean): HttpResponse =
if (async) executeAsync(request).get() else execute(request)

// When retrying, all failed responses should be closed. Only the final returned response should
// be open.
private fun assertNoResponseLeaks() = assertThat(openResponseCount).isEqualTo(1)
}

0 comments on commit 41309c1

Please sign in to comment.