From 2713127466504531897862b642fbf9a576d5c442 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 3 Jul 2021 21:55:09 +0800 Subject: [PATCH] Fix reconnection on Heartbeat timeout: add `PacketTimeoutException` --- .../network/components/HeartbeatScheduler.kt | 7 ++++--- .../handler/selector/PacketTimeoutException.kt | 15 +++++++++++++++ .../impl/netty/HeartbeatFailedException.kt | 1 - 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 mirai-core/src/commonMain/kotlin/network/handler/selector/PacketTimeoutException.kt diff --git a/mirai-core/src/commonMain/kotlin/network/components/HeartbeatScheduler.kt b/mirai-core/src/commonMain/kotlin/network/components/HeartbeatScheduler.kt index ea64c1bef..23321590f 100644 --- a/mirai-core/src/commonMain/kotlin/network/components/HeartbeatScheduler.kt +++ b/mirai-core/src/commonMain/kotlin/network/components/HeartbeatScheduler.kt @@ -14,6 +14,7 @@ import net.mamoe.mirai.internal.network.component.ComponentKey import net.mamoe.mirai.internal.network.component.ComponentStorage import net.mamoe.mirai.internal.network.context.SsoProcessorContext import net.mamoe.mirai.internal.network.handler.NetworkHandlerSupport +import net.mamoe.mirai.internal.network.handler.selector.PacketTimeoutException import net.mamoe.mirai.utils.BotConfiguration.HeartbeatStrategy.* import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.info @@ -101,11 +102,11 @@ internal class TimeBasedHeartbeatSchedulerImpl( } try { - withTimeoutOrNull(timeout()) { + withTimeout(timeout()) { action() - } ?: error("Timeout $name.") + } } catch (e: Throwable) { - onHeartFailure(name, e) + onHeartFailure(name, PacketTimeoutException("Timeout receiving Heartbeat response", e)) } } }.apply { diff --git a/mirai-core/src/commonMain/kotlin/network/handler/selector/PacketTimeoutException.kt b/mirai-core/src/commonMain/kotlin/network/handler/selector/PacketTimeoutException.kt new file mode 100644 index 000000000..d5306ad2a --- /dev/null +++ b/mirai-core/src/commonMain/kotlin/network/handler/selector/PacketTimeoutException.kt @@ -0,0 +1,15 @@ +/* + * Copyright 2019-2021 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/dev/LICENSE + */ + +package net.mamoe.mirai.internal.network.handler.selector + +internal data class PacketTimeoutException( + override val message: String, + override val cause: Throwable? = null, +) : NetworkException(true) \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/network/impl/netty/HeartbeatFailedException.kt b/mirai-core/src/commonMain/kotlin/network/impl/netty/HeartbeatFailedException.kt index 94e9a9802..bcdc97b6c 100644 --- a/mirai-core/src/commonMain/kotlin/network/impl/netty/HeartbeatFailedException.kt +++ b/mirai-core/src/commonMain/kotlin/network/impl/netty/HeartbeatFailedException.kt @@ -10,7 +10,6 @@ package net.mamoe.mirai.internal.network.impl.netty import net.mamoe.mirai.internal.network.handler.selector.NetworkException -import net.mamoe.mirai.utils.unwrap import net.mamoe.mirai.utils.unwrapCancellationException import java.io.IOException