Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

added fix to rest body closing #32

Merged
merged 1 commit into from
Jul 27, 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
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Kotlin](https://img.shields.io/badge/kotlin-1.8.10-blue.svg?logo=kotlin)](http://kotlinlang.org)
[![Kotlin](https://img.shields.io/badge/kotlin-1.9.0-blue.svg?logo=kotlin)](http://kotlinlang.org)
[![codecov](https://codecov.io/gh/YDWK/YDWK/branch/master/graph/badge.svg?token=LKIA8T6N6J)](https://codecov.io/gh/YDWK/YDWK)
[![yde](https://img.shields.io/badge/YDE--Version-v1.0.9-blue)](https://github.com/YDWK/YDE/releases/tag/v1.0.9)
[![yde](https://img.shields.io/badge/YDE--Version-v1.1.0-blue)](https://github.com/YDWK/YDE/releases/tag/v1.1.0)

# YDE

Expand Down Expand Up @@ -36,6 +36,4 @@ dependencies {
<artifactId>yde</artifactId>
<version>${project.version}</version>
</dependency>
```
</CodeGroupItem>
</CodeGroup>
```
14 changes: 7 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
kotlin.code.style=official
version= 1.0.9
version= 1.1.0

jvmVersion = 1.8.20
pluginAllOpenVersion = 1.8.20
jvmVersion = 1.9.0
pluginAllOpenVersion = 1.9.0
spotlessVersion = 6.12.0
dokkaVersion = 1.8.10
detektVersion = 1.22.0

# Dependencies
jacksonModuleKotlinVersion = 2.14.2
logBackClassicVersion = 1.4.5
logBackCoreVersion = 1.4.5
logBackClassicVersion = 1.4.8
logBackCoreVersion = 1.4.8
sysoutOverSlf4jVersion = 1.0.2
okhttp3Version = 5.0.0-alpha.11
kotlinPoetVersion = 1.12.0
coroutinesVersion = 1.7.0-Beta
kotlinPoetVersion = 1.14.2
coroutinesVersion = 1.7.3
guavaVersion = 31.1-jre
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.time.Instant
import java.util.function.Function
import java.util.logging.Level
import java.util.logging.Logger
import kotlinx.coroutines.CompletableDeferred
import okhttp3.*
import org.slf4j.LoggerFactory
Expand All @@ -37,7 +39,12 @@ open class SimilarRestApiImpl(
private val builder: Request.Builder,
private val client: OkHttpClient,
) : SimilarRestApi {
private val logger = LoggerFactory.getLogger(SimilarRestApi::class.java)
private val httpLogger: Logger = Logger.getLogger(OkHttpClient::class.simpleName)
private val logger = LoggerFactory.getLogger(SimilarRestApiImpl::class.java)

init {
httpLogger.level = Level.ALL
}

override fun header(name: String, value: String): SimilarRestApi {
builder.header(name, value)
Expand Down Expand Up @@ -100,21 +107,26 @@ open class SimilarRestApiImpl(
}

override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
val code = response.code
error(response.body, code, function, null)
try {
if (!response.isSuccessful) {
val code = response.code
error(response.body, code, function, null)
}
responseBody = response.body
val manager = CompletableFutureManager(response, yde)
val result = function.apply(manager)
queue.complete(result)
} catch (e: Exception) {
queue.completeExceptionally(e)
} finally {
responseBody?.close() // Close the response body after using it
}
responseBody = response.body
val manager = CompletableFutureManager(response, yde)
val result = function.apply(manager)
queue.complete(result)
}
})
} catch (e: Exception) {
throw RuntimeException("Error while executing request", e)
} finally {
responseBody?.close()
}

return queue
}

Expand All @@ -131,20 +143,27 @@ open class SimilarRestApiImpl(
}

override fun onResponse(call: Call, response: Response) {
if (!response.isSuccessful) {
val code = response.code
error<NoResult>(
response.body, code, null, NoResult(Instant.now().toString()))
try {
if (!response.isSuccessful) {
val code = response.code
error<NoResult>(
response.body,
code,
null,
NoResult(Instant.now().toString()))
}
queue.complete(NoResult(Instant.now().toString()))
} catch (e: Exception) {
queue.completeExceptionally(e)
} finally {
responseBody?.close() // Close the response body after using it
}
responseBody = response.body
queue.complete(NoResult(Instant.now().toString()))
}
})
} catch (e: Exception) {
throw RuntimeException("Error while executing request", e)
} finally {
responseBody?.close()
}

return queue
}

Expand All @@ -161,7 +180,7 @@ open class SimilarRestApiImpl(
} else if (JsonErrorCode.fromInt(code) != JsonErrorCode.UNKNOWN) {
handleJsonError(body, code)
} else {
yde.logger.error("Unknown error occurred while executing request")
logger.error("Unknown error occurred while executing request")
}
}

Expand All @@ -170,19 +189,25 @@ open class SimilarRestApiImpl(
function: Function<CompletableFutureManager, T>?,
noResult: NoResult?
) {
val jsonNode = yde.objectMapper.readTree(body.string())
val retryAfter = jsonNode.get("retry_after").asLong()
val global = jsonNode.get("global").asBoolean()
val message = jsonNode.get("message").asText()
yde.logger.error("Error while executing request: $message")
if (global) {
yde.logger.error("Global rate limit reached, retrying in $retryAfter ms")
Thread.sleep(retryAfter)
completeReTry(function, noResult)
} else {
yde.logger.error("Rate limit reached, retrying in $retryAfter ms")
Thread.sleep(retryAfter)
completeReTry(function, noResult)
try {
val jsonNode = yde.objectMapper.readTree(body.string())
val retryAfter = jsonNode.get("retry_after").asLong()
val global = jsonNode.get("global").asBoolean()
val message = jsonNode.get("message").asText()
logger.error("Error while executing request: $message")
if (global) {
logger.error("Global rate limit reached, retrying in $retryAfter ms")
Thread.sleep(retryAfter)
completeReTry(function, noResult)
} else {
logger.error("Rate limit reached, retrying in $retryAfter ms")
Thread.sleep(retryAfter)
completeReTry(function, noResult)
}
} catch (e: Exception) {
logger.error("Error while executing request: ${e.message}")
} finally {
responseBody?.close() // Close the response body after using it
}
}

Expand All @@ -207,7 +232,7 @@ open class SimilarRestApiImpl(
reason +=
" This body contains more detail : " +
yde.objectMapper.readTree(body.string()).toPrettyString()
yde.logger.error("Error while executing request: $codeAndName $reason")
logger.error("Error while executing request: $codeAndName $reason")
}

private fun handleJsonError(body: ResponseBody, code: Int) {
Expand All @@ -218,7 +243,7 @@ open class SimilarRestApiImpl(
" This body contains more detail : " +
yde.objectMapper.readTree(body.string()).toPrettyString()

yde.logger.error("Error while executing request: $jsonCode $jsonMessage")
logger.error("Error while executing request: $jsonCode $jsonMessage")
}

var responseBody: ResponseBody? = null
Expand Down