Implement HeartbeatStrategy, #1226

This commit is contained in:
Him188 2021-06-13 10:48:14 +08:00
parent e9d9d56489
commit 43b13d158f
3 changed files with 38 additions and 7 deletions

View File

@ -24,6 +24,9 @@ internal interface HeartbeatProcessor {
@Throws(Exception::class) @Throws(Exception::class)
suspend fun doStatHeartbeatNow(networkHandler: NetworkHandler) suspend fun doStatHeartbeatNow(networkHandler: NetworkHandler)
@Throws(Exception::class)
suspend fun doRegisterNow(networkHandler: NetworkHandler): StatSvc.Register.Response
companion object : ComponentKey<HeartbeatProcessor> companion object : ComponentKey<HeartbeatProcessor>
} }
@ -45,4 +48,9 @@ internal class HeartbeatProcessorImpl : HeartbeatProcessor {
retry = 2 retry = 2
) )
} }
@Throws(Exception::class)
override suspend fun doRegisterNow(networkHandler: NetworkHandler): StatSvc.Register.Response {
return networkHandler.context[SsoProcessor].sendRegister(networkHandler)
}
} }

View File

@ -14,6 +14,7 @@ import net.mamoe.mirai.internal.network.component.ComponentKey
import net.mamoe.mirai.internal.network.component.ComponentStorage import net.mamoe.mirai.internal.network.component.ComponentStorage
import net.mamoe.mirai.internal.network.context.SsoProcessorContext import net.mamoe.mirai.internal.network.context.SsoProcessorContext
import net.mamoe.mirai.internal.network.handler.NetworkHandlerSupport import net.mamoe.mirai.internal.network.handler.NetworkHandlerSupport
import net.mamoe.mirai.utils.BotConfiguration.HeartbeatStrategy.*
import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.info import net.mamoe.mirai.utils.info
@ -44,13 +45,29 @@ internal class TimeBasedHeartbeatSchedulerImpl(
val heartbeatProcessor = context[HeartbeatProcessor] val heartbeatProcessor = context[HeartbeatProcessor]
val list = mutableListOf<Job>() val list = mutableListOf<Job>()
list += launchHeartbeatJobAsync( when (context[SsoProcessorContext].configuration.heartbeatStrategy) {
scope = scope, STAT_HB -> {
name = "StatHeartbeat", list += launchHeartbeatJobAsync(
timeout = { context[SsoProcessorContext].configuration.statHeartbeatPeriodMillis }, scope = scope,
action = { heartbeatProcessor.doStatHeartbeatNow(network) }, name = "StatHeartbeat",
onHeartFailure = onHeartFailure timeout = { context[SsoProcessorContext].configuration.statHeartbeatPeriodMillis },
) action = { heartbeatProcessor.doStatHeartbeatNow(network) },
onHeartFailure = onHeartFailure
)
}
REGISTER -> {
list += launchHeartbeatJobAsync(
scope = scope,
name = "RegisterHeartbeat",
timeout = { context[SsoProcessorContext].configuration.statHeartbeatPeriodMillis },
action = { heartbeatProcessor.doRegisterNow(network) },
onHeartFailure = onHeartFailure
)
}
NONE -> {
}
}
list += launchHeartbeatJobAsync( list += launchHeartbeatJobAsync(
scope = scope, scope = scope,
name = "AliveHeartbeat", name = "AliveHeartbeat",

View File

@ -63,6 +63,8 @@ internal interface SsoProcessor {
suspend fun logout(handler: NetworkHandler) suspend fun logout(handler: NetworkHandler)
suspend fun sendRegister(handler: NetworkHandler): StatSvc.Register.Response
companion object : ComponentKey<SsoProcessor> companion object : ComponentKey<SsoProcessor>
} }
@ -125,6 +127,10 @@ internal class SsoProcessorImpl(
ssoContext.bot.logger.info { "Login successful." } ssoContext.bot.logger.info { "Login successful." }
} }
override suspend fun sendRegister(handler: NetworkHandler): StatSvc.Register.Response {
return registerClientOnline(handler).also { registerResp = it }
}
private suspend fun registerClientOnline(handler: NetworkHandler): StatSvc.Register.Response { private suspend fun registerClientOnline(handler: NetworkHandler): StatSvc.Register.Response {
return StatSvc.Register.online(client).sendAndExpect(handler).also { registerResp = it } return StatSvc.Register.online(client).sendAndExpect(handler).also { registerResp = it }
} }