Просмотр исходного кода

Use `retryCatching` in place of `tryNTimes`

Him188 6 лет назад
Родитель
Сommit
7e6badb8db

+ 5 - 6
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/QQAndroidBotNetworkHandler.kt

@@ -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)
                                 [email protected] {
@@ -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()
         }
     }
 

+ 18 - 32
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/tryNTimes.kt

@@ -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) {
-        try {
-            return block(it)
-        } catch (e: Throwable) {
-            if (lastException == null) {
-                lastException = e
-            } else lastException!!.addSuppressedMirai(e)
-        }
+@OptIn(ExperimentalContracts::class)
+@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE")
[email protected]
+internal inline fun <R> retryCatching(n: Int, block: () -> R): Result<R> {
+    contract {
+        callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
     }
-
-    throw lastException!!
-}
-
-@MiraiInternalAPI
-@Suppress("DuplicatedCode")
-inline fun <R> tryNTimesOrException(repeat: Int, block: (Int) -> R): Throwable? {
-    var lastException: Throwable? = null
-
-    repeat(repeat) {
+    require(n >= 0) { "param n for retryCatching must not be negative" }
+    var exception: Throwable? = null
+    repeat(n) {
         try {
-            block(it)
-            return null
+            return Result.success(block())
         } catch (e: Throwable) {
-            if (lastException == null) {
-                lastException = e
-            } else lastException!!.addSuppressedMirai(e)
+            exception?.addSuppressedMirai(e)
+            exception = e
         }
     }
-
-    return lastException!!
-}
+    return Result.failure(exception!!)
+}