Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
尝试优化Bot内的重连机制
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Jan 5, 2024
1 parent e802b34 commit 6386c8f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
12 changes: 11 additions & 1 deletion .idea/deployment.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/P.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object P : ProjectDetail() {
override val homepage: String
get() = HOMEPAGE

private val baseVersion = v(0, 1, 0)
private val baseVersion = v(0, 1, 1)

// private val alphaSuffix = v("alpha", 1)
// baseVersion - alphaSuffix
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023. ForteScarlet.
* Copyright (c) 2023-2024. ForteScarlet.
*
* This file is part of simbot-component-miyoushe.
*
Expand Down Expand Up @@ -436,36 +436,36 @@ internal class BotImpl(

logger.trace("Receiving next frame...")
val frameCatching = session.incoming.receiveCatching()
frameCatching.onClosed { e ->

frameCatching.onFailure { e ->
logger.error(
"Bot({}) session receive failed: session closed and reason: {}.",
bot,
e?.message,
e
"Bot({}) session receive on failure: {}.", bot, e?.message, e
)
session.cancel(CreateCancellationException("Session receive frame on failure", e))
if (bot.isActive) {
logger.debug("Bot is active now, try to reconnect.")
return GetWebsocketInfo(bot)
} else {
logger.debug("Bot is not active now, just cancel the session and bot.")
return null
}
// bot.cancel(CancellationException(e?.message?.let { "Session closed: $it" } ?: "Session closed", e))
return null
}

frameCatching.onFailure { e ->
frameCatching.onClosed { e ->
logger.error(
"Bot({}) session receive failed: {}.", bot, e?.message, e
"Bot({}) session receive failed: session closed and reason: {}.",
bot,
e?.message,
e
)
session.cancel(CreateCancellationException("Session receive frame on closed", e))
if (bot.isActive) {
logger.debug("Bot is active now, try to reconnect.")
return GetWebsocketInfo(bot)
} else {
logger.debug("Bot is not active now, just cancel the session and bot.")
return null
}
// bot.cancel(CancellationException(e?.message?.let { "Session receive onFailure: $it" }
// ?: "Session receive onFailure", e))
return null
}

try {
Expand Down Expand Up @@ -498,7 +498,9 @@ internal class BotImpl(
logoutReply,
bot
)
bot.cancel(CreateCancellationException("PLogoutReply received: $logoutReply", null))
val cancelCause = CreateCancellationException("PLogoutReply received: $logoutReply", null)
session.cancel(cancelCause)
bot.cancel(cancelCause)
return null
} else {
bot.logger.error(
Expand Down Expand Up @@ -532,8 +534,9 @@ internal class BotImpl(
session,
bot
)
session.cancel(CreateCancellationException(BizType.KICK_OFF.toString(), null))
bot.cancel(CreateCancellationException(BizType.KICK_OFF.toString(), null))
val cancelReason = CreateCancellationException(BizType.KICK_OFF.toString(), null)
session.cancel(cancelReason)
bot.cancel(cancelReason)
return null
}

Expand All @@ -554,8 +557,14 @@ internal class BotImpl(

} catch (serEx: SerializationException) {
// ser e?
logger.error(
"Unexpected serialization exception when receiving and processing frames: {}",
serEx.message,
serEx
)
} catch (e: Throwable) {
// e?
logger.error("Unexpected exception when receiving and processing frames: {}", e.message, e)
}


Expand Down

0 comments on commit 6386c8f

Please sign in to comment.