From eca2fcd56862aab447dd0b724adaf58a01dbcd50 Mon Sep 17 00:00:00 2001 From: RealYusufIsmail Date: Thu, 27 Jul 2023 15:48:57 +0100 Subject: [PATCH] added fix to rest body closing --- README.md | 8 +- gradle.properties | 14 +-- .../yde/impl/rest/type/SimilarRestApiImpl.kt | 93 ++++++++++++------- 3 files changed, 69 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 76ec9d3..9de2702 100644 --- a/README.md +++ b/README.md @@ -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 @@ -36,6 +36,4 @@ dependencies { yde ${project.version} -``` - - +``` \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 9a82062..b22c07a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/kotlin/io/github/ydwk/yde/impl/rest/type/SimilarRestApiImpl.kt b/src/main/kotlin/io/github/ydwk/yde/impl/rest/type/SimilarRestApiImpl.kt index a439a3f..37cafb8 100644 --- a/src/main/kotlin/io/github/ydwk/yde/impl/rest/type/SimilarRestApiImpl.kt +++ b/src/main/kotlin/io/github/ydwk/yde/impl/rest/type/SimilarRestApiImpl.kt @@ -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 @@ -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) @@ -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 } @@ -131,20 +143,27 @@ open class SimilarRestApiImpl( } override fun onResponse(call: Call, response: Response) { - if (!response.isSuccessful) { - val code = response.code - error( - response.body, code, null, NoResult(Instant.now().toString())) + try { + if (!response.isSuccessful) { + val code = response.code + error( + 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 } @@ -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") } } @@ -170,19 +189,25 @@ open class SimilarRestApiImpl( function: Function?, 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 } } @@ -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) { @@ -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