onesvm-browser-server/server/internal/gateway/util.go
chii 347c94c121 fix: 搜索缓存往返保留 results,并加检索源规则过滤
缓存命中改写信封时补 UnmarshalJSON,避免 results 被抹空;零结果不进 300s 缓存。模版层对 SearXNG hits 做广告标记丢弃与同域限额 1 条,减轻 1688 霸屏。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-08 11:33:13 +08:00

49 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// util.go:管线结果类型与工具函数。
package gateway
import (
"encoding/json"
"strconv"
"strings"
)
// jsonUnmarshal 解码辅助。
func jsonUnmarshal(b []byte, v any) error { return json.Unmarshal(b, v) }
// itoa int→string。
func itoa(n int) string { return strconv.Itoa(n) }
// itoa64 int64→string。
func itoa64(n int64) string { return strconv.FormatInt(n, 10) }
// errMsg 错误消息提取(nil 安全)。
func errMsg(err error) string {
if err == nil {
return "未知错误"
}
return strings.TrimSpace(err.Error())
}
// isOKEnvelope 判断信封字节是否为 ok=true(缓存写入仅成功结果)。
func isOKEnvelope(raw []byte) bool {
var probe struct {
OK bool `json:"ok"`
}
if err := jsonUnmarshal(raw, &probe); err != nil {
return false
}
return probe.OK
}
// isEmptySearchEnvelope 判断 search 信封是否零结果。
// SearXNG 引擎全挂仍可能 HTTP 200 + ok=true + results=[];写进 300s 缓存会把瞬时空结果冻成「消费端空值」。
func isEmptySearchEnvelope(raw []byte) bool {
var probe struct {
OK bool `json:"ok"`
Results []json.RawMessage `json:"results"`
}
if err := jsonUnmarshal(raw, &probe); err != nil {
return true
}
return probe.OK && len(probe.Results) == 0
}