Commit b244697d authored by Him188's avatar Him188

Add timing on reconnecting

parent 73c700b4
......@@ -24,6 +24,8 @@ import net.mamoe.mirai.network.closeAndJoin
import net.mamoe.mirai.utils.*
import net.mamoe.mirai.utils.internal.retryCatching
import kotlin.coroutines.CoroutineContext
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime
/*
* 泛型 N 不需要向外(接口)暴露.
......@@ -86,6 +88,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
@Throws(LoginFailedException::class) // only
protected abstract suspend fun relogin(cause: Throwable?)
@OptIn(ExperimentalTime::class)
@Suppress("unused")
private val offlineListener: Listener<BotOfflineEvent> =
this@BotImpl.subscribeAlways(concurrency = Listener.ConcurrencyKind.LOCKED) { event ->
......@@ -103,6 +106,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
}
bot.logger.info { "Connection dropped by server or lost, retrying login" }
val time = measureTime {
tailrec suspend fun reconnect() {
retryCatching<Unit>(configuration.reconnectionRetryTimes,
except = LoginFailedException::class) { tryCount, _ ->
......@@ -116,7 +120,6 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
@OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class)
relogin((event as? BotOfflineEvent.Dropped)?.cause)
}
logger.info { "Reconnected successfully" }
BotReloginEvent(bot, (event as? BotOfflineEvent.Dropped)?.cause).broadcast()
return
}.getOrElse {
......@@ -132,9 +135,11 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
}
reconnect()
}
reconnect()
}
logger.info { "Reconnected successfully in ${time.inMilliseconds} ms" }
}
is BotOfflineEvent.Active -> {
val msg = if (event.cause == null) {
""
......
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