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)
suspend fun doStatHeartbeatNow(networkHandler: NetworkHandler)
@Throws(Exception::class)
suspend fun doRegisterNow(networkHandler: NetworkHandler): StatSvc.Register.Response
companion object : ComponentKey<HeartbeatProcessor>
}
@ -45,4 +48,9 @@ internal class HeartbeatProcessorImpl : HeartbeatProcessor {
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.context.SsoProcessorContext
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.info
@ -44,13 +45,29 @@ internal class TimeBasedHeartbeatSchedulerImpl(
val heartbeatProcessor = context[HeartbeatProcessor]
val list = mutableListOf<Job>()
list += launchHeartbeatJobAsync(
scope = scope,
name = "StatHeartbeat",
timeout = { context[SsoProcessorContext].configuration.statHeartbeatPeriodMillis },
action = { heartbeatProcessor.doStatHeartbeatNow(network) },
onHeartFailure = onHeartFailure
)
when (context[SsoProcessorContext].configuration.heartbeatStrategy) {
STAT_HB -> {
list += launchHeartbeatJobAsync(
scope = scope,
name = "StatHeartbeat",
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(
scope = scope,
name = "AliveHeartbeat",

View File

@ -63,6 +63,8 @@ internal interface SsoProcessor {
suspend fun logout(handler: NetworkHandler)
suspend fun sendRegister(handler: NetworkHandler): StatSvc.Register.Response
companion object : ComponentKey<SsoProcessor>
}
@ -125,6 +127,10 @@ internal class SsoProcessorImpl(
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 {
return StatSvc.Register.online(client).sendAndExpect(handler).also { registerResp = it }
}