Commit 7e6badb8 authored by Him188's avatar Him188

Use `retryCatching` in place of `tryNTimes`

parent 84869266
...@@ -39,8 +39,7 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.login.WtLogin ...@@ -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.PlatformSocket
import net.mamoe.mirai.qqandroid.utils.io.readPacketExact import net.mamoe.mirai.qqandroid.utils.io.readPacketExact
import net.mamoe.mirai.qqandroid.utils.io.useBytes import net.mamoe.mirai.qqandroid.utils.io.useBytes
import net.mamoe.mirai.qqandroid.utils.tryNTimes import net.mamoe.mirai.qqandroid.utils.retryCatching
import net.mamoe.mirai.qqandroid.utils.tryNTimesOrException
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import kotlin.jvm.Volatile import kotlin.jvm.Volatile
...@@ -267,7 +266,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -267,7 +266,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
lateinit var loadGroup: suspend () -> Unit lateinit var loadGroup: suspend () -> Unit
loadGroup = suspend { loadGroup = suspend {
tryNTimesOrException(3) { retryCatching(3) {
bot.groups.delegate.addLast( bot.groups.delegate.addLast(
@Suppress("DuplicatedCode") @Suppress("DuplicatedCode")
(GroupImpl( (GroupImpl(
...@@ -298,7 +297,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -298,7 +297,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
) )
)) ))
) )
}?.let { }.exceptionOrNull()?.let {
logger.error { "群${troopNum.groupCode}的列表拉取失败, 一段时间后将会重试" } logger.error { "群${troopNum.groupCode}的列表拉取失败, 一段时间后将会重试" }
logger.error(it) logger.error(it)
this@QQAndroidBotNetworkHandler.launch { this@QQAndroidBotNetworkHandler.launch {
...@@ -618,7 +617,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -618,7 +617,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
packetListeners.remove(handler) packetListeners.remove(handler)
} }
} else this.delegate.useBytes { data, length -> } else this.delegate.useBytes { data, length ->
return tryNTimes(retry + 1) { return retryCatching(retry + 1) {
val handler = PacketListener(commandName = commandName, sequenceId = sequenceId) val handler = PacketListener(commandName = commandName, sequenceId = sequenceId)
packetListeners.addLast(handler) packetListeners.addLast(handler)
try { try {
...@@ -626,7 +625,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -626,7 +625,7 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
} finally { } finally {
packetListeners.remove(handler) packetListeners.remove(handler)
} }
} }.getOrThrow()
} }
} }
......
...@@ -13,46 +13,32 @@ ...@@ -13,46 +13,32 @@
package net.mamoe.mirai.qqandroid.utils 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.JvmMultifileClass
import kotlin.jvm.JvmName import kotlin.jvm.JvmName
@PublishedApi @PublishedApi
internal expect fun Throwable.addSuppressedMirai(e: Throwable) internal expect fun Throwable.addSuppressedMirai(e: Throwable)
@MiraiInternalAPI @OptIn(ExperimentalContracts::class)
@Suppress("DuplicatedCode") @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
internal inline fun <R> tryNTimes(repeat: Int, block: (Int) -> R): R { @kotlin.internal.InlineOnly
var lastException: Throwable? = null internal inline fun <R> retryCatching(n: Int, block: () -> R): Result<R> {
contract {
repeat(repeat) { callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
try {
return block(it)
} catch (e: Throwable) {
if (lastException == null) {
lastException = e
} else lastException!!.addSuppressedMirai(e)
}
} }
require(n >= 0) { "param n for retryCatching must not be negative" }
throw lastException!! var exception: Throwable? = null
} repeat(n) {
@MiraiInternalAPI
@Suppress("DuplicatedCode")
inline fun <R> tryNTimesOrException(repeat: Int, block: (Int) -> R): Throwable? {
var lastException: Throwable? = null
repeat(repeat) {
try { try {
block(it) return Result.success(block())
return null
} catch (e: Throwable) { } catch (e: Throwable) {
if (lastException == null) { exception?.addSuppressedMirai(e)
lastException = e exception = e
} else lastException!!.addSuppressedMirai(e)
} }
} }
return Result.failure(exception!!)
return lastException!! }
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment