Skip to content

Commit

Permalink
fix: ignore to logger
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Jul 5, 2021
1 parent 09e79dc commit 17f2981
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
24 changes: 12 additions & 12 deletions src/main/kotlin/xyz/cssxsh/bilibili/BiliClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlinx.serialization.json.Json
import xyz.cssxsh.bilibili.api.SPACE
import java.io.IOException

class BiliClient(val ignore: suspend (exception: Throwable) -> Boolean = DefaultIgnore, interval: Long = 10 * 1000L) {
open class BiliClient {
companion object {
val Json = Json {
prettyPrint = true
Expand All @@ -29,6 +29,8 @@ class BiliClient(val ignore: suspend (exception: Throwable) -> Boolean = Default
val DefaultIgnore: suspend (Throwable) -> Boolean = { it is IOException || it is HttpRequestTimeoutException }
}

protected open val ignore: suspend (exception: Throwable) -> Boolean = DefaultIgnore

private val cookiesStorage = AcceptAllCookiesStorage()

private fun client() = HttpClient(OkHttp) {
Expand All @@ -51,20 +53,18 @@ class BiliClient(val ignore: suspend (exception: Throwable) -> Boolean = Default
ContentEncoding()
}

internal val mutex = BiliApiMutex(interval)
internal open val mutex = BiliApiMutex(10 * 1000L)

suspend fun <T> useHttpClient(block: suspend (HttpClient) -> T): T = supervisorScope {
client().use {
while (isActive) {
runCatching {
block(it)
}.onFailure {
if (ignore(it).not()) throw it
}.onSuccess {
return@use it
}
while (isActive) {
runCatching {
client().use { block(it) }
}.onFailure {
if (ignore(it).not()) throw it
}.onSuccess {
return@supervisorScope it
}
throw CancellationException()
}
throw CancellationException()
}
}
2 changes: 0 additions & 2 deletions src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliHelperPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ object BiliHelperPlugin : KotlinPlugin(
}
) {

val client by lazy { BiliClient(interval = BiliHelperSettings.api * 1000) }

lateinit var driver: RemoteWebDriver
private set

Expand Down
12 changes: 11 additions & 1 deletion src/main/kotlin/xyz/cssxsh/mirai/plugin/BiliUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ import java.time.format.DateTimeFormatter

internal val logger by BiliHelperPlugin::logger

internal val client by BiliHelperPlugin::client
internal val client by lazy {
object : BiliClient() {
override val ignore: suspend (exception: Throwable) -> Boolean = { throwable ->
super.ignore(throwable).also {
if (it) logger.warning { "Ignore $throwable" }
}
}

override val mutex: BiliApiMutex = BiliApiMutex(interval = BiliHelperSettings.api * 1000)
}
}

internal val ImageCache by lazy { File(BiliHelperSettings.cache) }

Expand Down

0 comments on commit 17f2981

Please sign in to comment.