Skip to content

Commit

Permalink
移除OnebotFramework中的请求超时时间常量
Browse files Browse the repository at this point in the history
  • Loading branch information
kosaka-bun committed Feb 14, 2025
1 parent b67d89d commit 585293c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.honoka.qqrobot.framework.impl.onebot

import cn.hutool.core.exceptions.ExceptionUtil
import cn.hutool.core.io.FileUtil
import cn.hutool.core.io.IoUtil
import cn.hutool.core.util.IdUtil
Expand Down Expand Up @@ -27,6 +28,7 @@ import org.springframework.web.socket.*
import org.springframework.web.socket.client.standard.StandardWebSocketClient
import java.io.File
import java.io.InputStream
import java.net.SocketTimeoutException
import java.util.concurrent.ConcurrentHashMap
import kotlin.io.path.Path

Expand All @@ -44,8 +46,6 @@ class OnebotFramework(
companion object {

const val TIME_TO_WAIT_ONLINE = 5000L

const val HTTP_REQUEST_TIMEOUT = 10 * 1000
}

inner class WebSocketHandlerImpl : WebSocketHandler {
Expand Down Expand Up @@ -212,7 +212,7 @@ class OnebotFramework(
}
val url = "${onebotProperties.urlPrefix}/get_status"
online = try {
HttpUtil.post(url, "{}", HTTP_REQUEST_TIMEOUT).toJsonWrapper().run {
HttpUtil.post(url, "{}", 3000).toJsonWrapper().run {
getBool("data.online")
}
} catch(t: Throwable) {
Expand Down Expand Up @@ -309,7 +309,7 @@ class OnebotFramework(
val content = IoUtil.readBytes(`in`)
HttpUtil.createPost(url).run {
form("file", content, fileName)
timeout(HTTP_REQUEST_TIMEOUT)
timeout(10 * 1000)
val res = JSONUtil.parseObj(execute().body())
if(res.getBool("status") != true) {
throw Exception("File receiver returns failed status: ${res.getStr("msg")}")
Expand Down Expand Up @@ -344,16 +344,23 @@ class OnebotFramework(
it["message"] = m.parts
it.toString()
},
HTTP_REQUEST_TIMEOUT
5000
).let { JSONUtil.parseObj(it) }
val retcode = res.getInt("retcode")
val errMsg = res.getStr("message")
if(retcode != 0) exception("retcode = $retcode,errMsg = $errMsg")
} catch(t: Throwable) {
throwable = t
log.error("\n消息发送失败!已尝试次数:$i\n要发送的内容:\n${m.toRawString()}", t)
if(!basicProperties.resendOnSendFailed) break
continue
when(ExceptionUtil.getRootCause(t)) {
is SocketTimeoutException -> {
log.warn("\n消息可能未发送成功\n要发送的内容:\n${m.toRawString()}", t)
}
else -> {
throwable = t
log.error("\n消息发送失败!已尝试次数:$i\n要发送的内容:\n${m.toRawString()}", t)
if(!basicProperties.resendOnSendFailed) break
continue
}
}
}
throwable = null
if(i > 1) log.info("\n消息重发成功:\n${m.toRawString()}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import cn.hutool.http.HttpUtil
import cn.hutool.json.JSONObject
import cn.hutool.json.JSONUtil
import de.honoka.qqrobot.framework.config.OnebotProperties
import de.honoka.qqrobot.framework.impl.onebot.OnebotFramework
import de.honoka.qqrobot.starter.util.GlobalThreadPools
import de.honoka.sdk.util.kotlin.basic.log
import jakarta.annotation.PostConstruct
Expand Down Expand Up @@ -47,7 +46,7 @@ class ContactManager(private val onebotProperties: OnebotProperties) {

private fun readFriends() {
val url = "${onebotProperties.urlPrefix}/get_friend_list"
val res = HttpUtil.post(url, "{}", OnebotFramework.HTTP_REQUEST_TIMEOUT).let {
val res = HttpUtil.post(url, "{}", 10 * 1000).let {
JSONUtil.parseObj(it)
}
val friends = HashMap<Long, Contact>()
Expand All @@ -62,7 +61,7 @@ class ContactManager(private val onebotProperties: OnebotProperties) {

private fun readGroups() {
val url = "${onebotProperties.urlPrefix}/get_group_list"
val res = HttpUtil.post(url, "{}", OnebotFramework.HTTP_REQUEST_TIMEOUT).let {
val res = HttpUtil.post(url, "{}", 5000).let {
JSONUtil.parseObj(it)
}
val groupsJson = res.getJSONArray("data")
Expand Down Expand Up @@ -93,7 +92,7 @@ class ContactManager(private val onebotProperties: OnebotProperties) {
it["group_id"] = group
it.toString()
},
OnebotFramework.HTTP_REQUEST_TIMEOUT + 30 * 1000
30 * 1000
).let { JSONUtil.parseObj(it) }
val memberList = HashMap<Long, Contact>()
res.getJSONArray("data").forEach {
Expand All @@ -118,4 +117,4 @@ class ContactManager(private val onebotProperties: OnebotProperties) {
}

fun containsGroup(group: Long): Boolean = groupCache.containsKey(group)
}
}

0 comments on commit 585293c

Please sign in to comment.