refactor: 使用标准库替换第三方HTTP客户端并清理依赖
Some checks failed
ci/woodpecker/push/my-first-workflow Pipeline failed
Some checks failed
ci/woodpecker/push/my-first-workflow Pipeline failed
This commit is contained in:
@@ -19,7 +19,7 @@ require (
|
||||
github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 // indirect
|
||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
github.com/imroc/req/v3 v3.43.3 // indirect
|
||||
|
||||
github.com/klauspost/compress v1.17.7 // indirect
|
||||
github.com/onsi/ginkgo/v2 v2.16.0 // indirect
|
||||
github.com/orcaman/concurrent-map/v2 v2.0.1 // indirect
|
||||
|
||||
@@ -3,7 +3,7 @@ module github.com/zmexing/go-sensitive-word
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/imroc/req/v3 v3.42.3
|
||||
|
||||
github.com/orcaman/concurrent-map/v2 v2.0.1
|
||||
)
|
||||
|
||||
|
||||
@@ -2,13 +2,15 @@ package store
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"github.com/imroc/req/v3"
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
)
|
||||
|
||||
// MemoryModel 使用并发 map 实现的内存词库
|
||||
@@ -62,26 +64,40 @@ func (m *MemoryModel) LoadDictEmbed(contents ...string) error {
|
||||
}
|
||||
|
||||
// 从远程 HTTP 地址加载词库
|
||||
// LoadDictHttp 批量从 HTTP 地址加载字典(标准库 net/http 实现)
|
||||
func (m *MemoryModel) LoadDictHttp(urls ...string) error {
|
||||
// 【标准库】创建带超时的客户端,防止请求卡死
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second, // 超时控制,非常重要
|
||||
}
|
||||
|
||||
for _, url := range urls {
|
||||
err := func(url string) error {
|
||||
httpRes, err := req.Get(url)
|
||||
// 立即执行函数,解决 defer 循环变量问题
|
||||
err := func(u string) error {
|
||||
// 标准库 GET 请求
|
||||
resp, err := client.Get(u)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if httpRes == nil {
|
||||
return errors.New("nil http response")
|
||||
}
|
||||
if httpRes.StatusCode != http.StatusOK {
|
||||
return errors.New(httpRes.GetStatus())
|
||||
return fmt.Errorf("请求失败 %s: %w", u, err)
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(httpRes.Body)
|
||||
// 必须 defer 关闭 body,防止资源泄漏(标准库固定写法)
|
||||
defer func() {
|
||||
closeErr := resp.Body.Close()
|
||||
if closeErr != nil {
|
||||
fmt.Printf("警告: 关闭响应体失败 url=%s, err=%v\n", u, closeErr)
|
||||
}
|
||||
}()
|
||||
|
||||
return m.LoadDict(httpRes.Body)
|
||||
// 状态码判断
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("http 状态码错误 url=%s, code=%d", u, resp.StatusCode)
|
||||
}
|
||||
|
||||
// 加载字典(和你原来逻辑一样)
|
||||
return m.LoadDict(resp.Body)
|
||||
}(url)
|
||||
|
||||
// 任意一个失败,立即返回
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ require (
|
||||
require (
|
||||
github.com/Baidu-AIP/golang-sdk v1.1.1 // indirect
|
||||
github.com/FloatTech/AnimeAPI v1.7.1-0.20251028071248-0c948e3db65c // indirect
|
||||
github.com/FloatTech/ZeroBot-Plugin v1.10.22 // indirect
|
||||
github.com/FloatTech/floatbox v0.0.0-20251002074805-f95cbc7edb31 // indirect
|
||||
github.com/FloatTech/gg v1.1.3 // indirect
|
||||
github.com/FloatTech/imgfactory v0.2.2-0.20230413152719-e101cc3606ef // indirect
|
||||
@@ -39,7 +40,7 @@ require (
|
||||
github.com/fumiama/ahsai v0.1.1 // indirect
|
||||
github.com/fumiama/cron v1.3.0 // indirect
|
||||
github.com/fumiama/deepinfra v0.0.0-20251221163610-e98ee3ba437a // indirect
|
||||
github.com/fumiama/go-base16384 v1.7.1 // indirect
|
||||
|
||||
github.com/fumiama/go-onebot-agent v0.0.0-20260314041356-bc4ca0e119d5 // indirect
|
||||
github.com/fumiama/go-registry v0.2.7 // indirect
|
||||
github.com/fumiama/go-simple-protobuf v0.2.0 // indirect
|
||||
@@ -105,9 +106,7 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
github.com/BurntSushi/toml v1.5.0 // indirect
|
||||
github.com/FloatTech/ZeroBot-Plugin v1.10.22
|
||||
github.com/clbanning/mxj/v2 v2.7.0 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/fatih/color v1.18.0 // indirect
|
||||
|
||||
@@ -91,8 +91,7 @@ github.com/fumiama/cron v1.3.0 h1:ZWlwuexF+HQHl3cYytEE5HNwD99q+3vNZF1GrEiXCFo=
|
||||
github.com/fumiama/cron v1.3.0/go.mod h1:bz5Izvgi/xEUI8tlBN8BI2jr9Moo8N4or0KV8xXuPDY=
|
||||
github.com/fumiama/deepinfra v0.0.0-20251221163610-e98ee3ba437a h1:a0+2vaXajfxsNcIaYG7A/cAb2e4+M3s5J6oyIiPsL3c=
|
||||
github.com/fumiama/deepinfra v0.0.0-20251221163610-e98ee3ba437a/go.mod h1:uqsWK/GM9OvKV0pXZOQB63rWugBbiXInY8E1JoRKhkg=
|
||||
github.com/fumiama/go-base16384 v1.7.1 h1:1P1x6FWRvd7PtbH4idDAGWAjKKcVxggxlROYKRXbw58=
|
||||
github.com/fumiama/go-base16384 v1.7.1/go.mod h1:OEn+947GV5gsbTAnyuUW/SrfxJYUdYupSIQXOuGOcXM=
|
||||
|
||||
github.com/fumiama/go-onebot-agent v0.0.0-20260314041356-bc4ca0e119d5 h1:e2WSMApv2JvDsoO+htcccJcOGy/ArZLwyorT85gU6uA=
|
||||
github.com/fumiama/go-onebot-agent v0.0.0-20260314041356-bc4ca0e119d5/go.mod h1:rTrS23rvTYuZcSngENJTvcBFTz1nGsImSv+bW7yfhqs=
|
||||
github.com/fumiama/go-registry v0.2.7 h1:tLEqgEpsiybQMqBv0dLHm5leia/z1DhajMupwnOHeNs=
|
||||
|
||||
@@ -3,25 +3,25 @@ package cmd
|
||||
import (
|
||||
_ "blazing/modules/player/controller/robot"
|
||||
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/antiabuse" // 违禁词
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/antiabuse" // 违禁词
|
||||
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/chat" // 基础词库
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/chat" // 基础词库
|
||||
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/chatcount" // 聊天时长统计
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/chatcount" // 聊天时长统计
|
||||
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/airecord" // 群应用:AI声聊
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/score"
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/sleepmanage" // 统计睡眠时间
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/airecord" // 群应用:AI声聊
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/score"
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/sleepmanage" // 统计睡眠时间
|
||||
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/atri" // ATRI词库
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/choose" // 选择困难症帮手
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/drawlots" // 多功能抽签
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/pig" // 来份猪猪
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/atri" // ATRI词库
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/choose" // 选择困难症帮手
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/drawlots" // 多功能抽签
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/pig" // 来份猪猪
|
||||
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/manager" // 群管
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/manager" // 群管
|
||||
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/aifalse" // 服务器监控
|
||||
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/poker" // 抽扑克
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/aifalse" // 服务器监控
|
||||
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/poker" // 抽扑克
|
||||
|
||||
//_ "github.com/FloatTech/zbputils/job" // 定时指令触发器
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
|
||||
Reference in New Issue
Block a user