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,37 +106,39 @@ abstract class BotImpl<N : BotNetworkHandler> constructor( ...@@ -103,37 +106,39 @@ 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" }
tailrec suspend fun reconnect() { val time = measureTime {
retryCatching<Unit>(configuration.reconnectionRetryTimes, tailrec suspend fun reconnect() {
except = LoginFailedException::class) { tryCount, _ -> retryCatching<Unit>(configuration.reconnectionRetryTimes,
if (tryCount != 0) { except = LoginFailedException::class) { tryCount, _ ->
delay(configuration.reconnectPeriodMillis) if (tryCount != 0) {
} delay(configuration.reconnectPeriodMillis)
network.withConnectionLock { }
/** network.withConnectionLock {
* [BotImpl.relogin] only, no [BotNetworkHandler.init] /**
*/ * [BotImpl.relogin] only, no [BotNetworkHandler.init]
@OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class) */
relogin((event as? BotOfflineEvent.Dropped)?.cause) @OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class)
} 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 {
if (it is LoginFailedException && !it.killBot) { if (it is LoginFailedException && !it.killBot) {
logger.info { "Cannot reconnect" }
logger.warning(it)
logger.info { "Retrying in 3s..." }
delay(3000)
return@getOrElse
}
logger.info { "Cannot reconnect" } logger.info { "Cannot reconnect" }
logger.warning(it) throw it
logger.info { "Retrying in 3s..." }
delay(3000)
return@getOrElse
} }
logger.info { "Cannot reconnect" } reconnect()
throw it
} }
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