mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-07 16:40:43 +08:00
Implement HeartbeatStrategy
, #1226
This commit is contained in:
parent
e9d9d56489
commit
43b13d158f
@ -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)
|
||||
}
|
||||
}
|
@ -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",
|
||||
|
@ -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 }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user