refactor: 使用标准库替换第三方HTTP客户端并清理依赖
Some checks failed
ci/woodpecker/push/my-first-workflow Pipeline failed

This commit is contained in:
xinian
2026-03-23 22:39:24 +08:00
committed by cnb
parent 3ad96070a3
commit 15764ee027
6 changed files with 50 additions and 36 deletions

View File

@@ -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

View File

@@ -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
)

View File

@@ -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
}