Use retryCatching in place of tryNTimes

This commit is contained in:
Him188 2020-04-05 23:36:19 +08:00
parent 8486926694
commit 7e6badb8db
2 changed files with 23 additions and 38 deletions

View File

@ -39,8 +39,7 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.login.WtLogin
import net.mamoe.mirai.qqandroid.utils.PlatformSocket
import net.mamoe.mirai.qqandroid.utils.io.readPacketExact
import net.mamoe.mirai.qqandroid.utils.io.useBytes
import net.mamoe.mirai.qqandroid.utils.tryNTimes
import net.mamoe.mirai.qqandroid.utils.tryNTimesOrException
import net.mamoe.mirai.qqandroid.utils.retryCatching
import net.mamoe.mirai.utils.*
import kotlin.coroutines.CoroutineContext
import kotlin.jvm.Volatile
@ -267,7 +266,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
lateinit var loadGroup: suspend () -> Unit
loadGroup = suspend {
tryNTimesOrException(3) {
retryCatching(3) {
bot.groups.delegate.addLast(
@Suppress("DuplicatedCode")
(GroupImpl(
@ -298,7 +297,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
)
))
)
}?.let {
}.exceptionOrNull()?.let {
logger.error { "${troopNum.groupCode}的列表拉取失败, 一段时间后将会重试" }
logger.error(it)
this@QQAndroidBotNetworkHandler.launch {
@ -618,7 +617,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
packetListeners.remove(handler)
}
} else this.delegate.useBytes { data, length ->
return tryNTimes(retry + 1) {
return retryCatching(retry + 1) {
val handler = PacketListener(commandName = commandName, sequenceId = sequenceId)
packetListeners.addLast(handler)
try {
@ -626,7 +625,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
} finally {
packetListeners.remove(handler)
}
}
}.getOrThrow()
}
}

View File

@ -13,46 +13,32 @@
package net.mamoe.mirai.qqandroid.utils
import net.mamoe.mirai.utils.MiraiInternalAPI
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
@PublishedApi
internal expect fun Throwable.addSuppressedMirai(e: Throwable)
@MiraiInternalAPI
@Suppress("DuplicatedCode")
internal inline fun <R> tryNTimes(repeat: Int, block: (Int) -> R): R {
var lastException: Throwable? = null
repeat(repeat) {
@OptIn(ExperimentalContracts::class)
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
@kotlin.internal.InlineOnly
internal inline fun <R> retryCatching(n: Int, block: () -> R): Result<R> {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
require(n >= 0) { "param n for retryCatching must not be negative" }
var exception: Throwable? = null
repeat(n) {
try {
return block(it)
return Result.success(block())
} catch (e: Throwable) {
if (lastException == null) {
lastException = e
} else lastException!!.addSuppressedMirai(e)
exception?.addSuppressedMirai(e)
exception = e
}
}
throw lastException!!
return Result.failure(exception!!)
}
@MiraiInternalAPI
@Suppress("DuplicatedCode")
inline fun <R> tryNTimesOrException(repeat: Int, block: (Int) -> R): Throwable? {
var lastException: Throwable? = null
repeat(repeat) {
try {
block(it)
return null
} catch (e: Throwable) {
if (lastException == null) {
lastException = e
} else lastException!!.addSuppressedMirai(e)
}
}
return lastException!!
}