Fix endless reconnection on LoginFailedException, close #895

This commit is contained in:
Him188 2021-01-25 12:41:07 +08:00
parent ff9cc6a74e
commit af9eadf34e

View File

@ -163,46 +163,46 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
private inner class Reconnect { private inner class Reconnect {
suspend fun reconnect(event: BotOfflineEvent): Boolean { suspend fun reconnect(event: BotOfflineEvent): Boolean {
while (true) { retryCatchingExceptions<Unit>(
retryCatchingExceptions<Unit>( configuration.reconnectionRetryTimes,
configuration.reconnectionRetryTimes, except = LoginFailedException::class
except = LoginFailedException::class ) { tryCount, _ ->
) { tryCount, _ -> if (tryCount != 0) {
if (tryCount != 0) { delay(configuration.reconnectPeriodMillis)
delay(configuration.reconnectPeriodMillis) }
}
// Close network to avoid endless reconnection while network is ok // Close network to avoid endless reconnection while network is ok
// https://github.com/mamoe/mirai/issues/894 // https://github.com/mamoe/mirai/issues/894
kotlin.runCatching { network.close(event.castOrNull<BotOfflineEvent.CauseAware>()?.cause) } kotlin.runCatching { network.close(event.castOrNull<BotOfflineEvent.CauseAware>()?.cause) }
login() login()
_network.postInitActions() _network.postInitActions()
// network.withConnectionLock { // network.withConnectionLock {
// /** // /**
// * [AbstractBot.relogin] only, no [BotNetworkHandler.init] // * [AbstractBot.relogin] only, no [BotNetworkHandler.init]
// */ // */
// @OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class) // @OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class)
// relogin((event as? BotOfflineEvent.Dropped)?.cause) // relogin((event as? BotOfflineEvent.Dropped)?.cause)
// } // }
launch { launch {
BotReloginEvent(bot, (event as? BotOfflineEvent.CauseAware)?.cause).broadcast() BotReloginEvent(bot, (event as? BotOfflineEvent.CauseAware)?.cause).broadcast()
} }
return true return true
}.getOrElse { exception -> }.getOrElse { exception ->
if (exception is LoginFailedException && !exception.killBot) { if (exception is LoginFailedException && !exception.killBot) {
logger.info { "Cannot reconnect." } logger.info { "Cannot reconnect." }
logger.warning(exception) logger.error(exception)
logger.info { "Retrying in 3s..." } // logger.info { "Retrying in 3s..." }
delay(3000) // delay(3000)
return@getOrElse
}
logger.info { "Cannot reconnect due to fatal error." }
bot.cancel(CancellationException("Cannot reconnect due to fatal error.", exception))
return false return false
} }
logger.info { "Cannot reconnect." }
bot.cancel(CancellationException("Cannot reconnect.", exception))
return false
} }
return false
} }
} }