Skip to content

Commit

Permalink
add retry connect config
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Jan 7, 2024
1 parent e80d629 commit b52acf1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ class BotConfig(
*/
val miraiHttp: Boolean = false,
/**
* 是否开启重连
* 重连尝试次数
*/
val reconnect: Boolean = true,
val retryTimes: Int = 5,
/**
* 重连间隔
* 重连间隔 (毫秒)
*/
val maxReconnectAttempts: Int = 20,
val retryWaitMills: Long = 5000L,
/**
* 重连休息时间 (毫秒)
*/
val retryRestMills: Long = 60000L,
)
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ConnectFactory private constructor(
}
val url = builder.toString()
try {
ws = WSClient.createAndConnect(scope, URI.create(url), logger, actionHandler, header)
ws = WSClient.createAndConnect(scope, URI.create(url), logger, actionHandler, config.retryTimes, config.retryWaitMills, config.retryRestMills, header)
} catch (e: Exception) {
logger.error("▌ {} 连接错误,请检查服务端是否开启 ┈━═☆", url)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class WSClient(
uri: URI,
private val logger: Logger,
private val actionHandler: ActionHandler,
header: Map<String, String> = mapOf()
val retryTimes: Int = 5,
val retryWaitMills: Long = 5000L,
val retryRestMills: Long = 60000L,
header: Map<String, String> = mapOf(),
) : WebSocketClient(uri, header) {
private var eventBus: EventBus? = null
fun createBot(): Bot {
Expand Down Expand Up @@ -79,8 +82,8 @@ class WSClient(
private const val HEART_BEAT = "heartbeat"

val mutex = Mutex()
fun createAndConnect(scope: CoroutineScope, uri: URI, logger: Logger, actionHandler: ActionHandler, header: Map<String, String> = mapOf()): WSClient? {
val ws = WSClient(scope, uri, logger, actionHandler, header)
fun createAndConnect(scope: CoroutineScope, uri: URI, logger: Logger, actionHandler: ActionHandler, retryTimes: Int, retryWaitMills: Long, retryRestMills: Long, header: Map<String, String> = mapOf()): WSClient? {
val ws = WSClient(scope, uri, logger, actionHandler, retryTimes, retryWaitMills, retryRestMills, header)
return if (ws.connectBlocking()) ws else null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ data class Config(
@SerialName("reversed_ws_port")
var reversedWSPort: Int = -1,
@SerialName("token")
var token: String = ""
var token: String = "",
@SerialName("retry_times")
var retryTimes: Int = 5,
@SerialName("retry_wait_mills")
var retryWaitMills: Long = 5_000L,
@SerialName("retry_rest_mills")
var retryRestMills: Long = 60_000L,
)
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {
url = config.wsHost,
reversedPort = config.reversedWSPort,
token = config.token,
isAccessToken = config.token.isNotBlank()
isAccessToken = config.token.isNotBlank(),
retryTimes = config.retryTimes,
retryWaitMills = config.retryWaitMills,
retryRestMills = config.retryRestMills,
), logger
)
val dispatchers: EventBus
Expand Down

0 comments on commit b52acf1

Please sign in to comment.