Commit b244697d authored by Him188's avatar Him188

Add timing on reconnecting

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