feat(govai): 0617 优化首批 — 安全/私有化/深度研究/服务层/可观测性
借鉴 odysseus 的能力设计,全程净室实现、零 AGPL 代码、不引入 AGPL 依赖。
T1 提示注入防护: pkg/promptguard 包裹外部/知识库内容为不可信数据,buildMessages 移出 system 指令区。 T2 安全 CI: .github/workflows(ci+security: govulncheck/gitleaks/actionlint/hadolint/trivy)+dependabot+.hadolint.yaml;go.mod 加 toolchain go1.25.11 修复 20 个 stdlib CVE。 T3 管理员 2FA: 迁移 000016 + RFC6238 TOTP/备份码(pkg/auth, 零依赖) + 登录流程集成(后端)。 T4 本地模型: LLM/embedding 支持本地 vLLM/Ollama(OpenAI 兼容, 鉴权头条件发送, NoAuth) + docs/local-deploy.md。 T6 深度研究: 迁移 000017 + Python research-worker(净室多步流水线, 检索避开 SearXNG) + Go research 服务/handler/路由。 T7 service 层: 新增 internal/service/{research,twofa}, 2FA 业务逻辑从胖 handler 下沉, 接口注入可单测。 T10 缓存/可观测性: internal/cache(Redis+内存, 优雅降级) 接入 store 热点列表; Prometheus 指标+/metrics; docs/openapi.yaml。 验证: go build/vet/test ./... 全绿(8 包); research-worker 12 单测过; 真实 PG 应用迁移并烟测。
This commit is contained in:
Vendored
+119
@@ -0,0 +1,119 @@
|
||||
// Package cache 提供轻量的 JSON 缓存抽象,用于缓存热点只读数据。
|
||||
//
|
||||
// 所有方法在后端不可用/出错时都"优雅降级"(视为未命中 / 静默跳过),
|
||||
// 因此调用方始终能回退到数据库,缓存层不会成为故障点。
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// Cache 是一个简单的 JSON 键值缓存。
|
||||
type Cache interface {
|
||||
// GetJSON 命中则把值反序列化到 dest 并返回 true;未命中/出错返回 false。
|
||||
GetJSON(ctx context.Context, key string, dest any) bool
|
||||
// SetJSON 写入(带 TTL);出错静默忽略。
|
||||
SetJSON(ctx context.Context, key string, val any, ttl time.Duration)
|
||||
// Delete 删除若干键;出错静默忽略。
|
||||
Delete(ctx context.Context, keys ...string)
|
||||
}
|
||||
|
||||
// ---------------- Redis 实现 ----------------
|
||||
|
||||
type redisCache struct {
|
||||
rdb *redis.Client
|
||||
}
|
||||
|
||||
// NewRedis 返回基于 Redis 的缓存实现。rdb 为 nil 时所有操作均为安全空操作。
|
||||
func NewRedis(rdb *redis.Client) Cache {
|
||||
return &redisCache{rdb: rdb}
|
||||
}
|
||||
|
||||
func (c *redisCache) GetJSON(ctx context.Context, key string, dest any) bool {
|
||||
if c.rdb == nil {
|
||||
return false
|
||||
}
|
||||
b, err := c.rdb.Get(ctx, key).Bytes()
|
||||
if err != nil || len(b) == 0 {
|
||||
return false
|
||||
}
|
||||
return json.Unmarshal(b, dest) == nil
|
||||
}
|
||||
|
||||
func (c *redisCache) SetJSON(ctx context.Context, key string, val any, ttl time.Duration) {
|
||||
if c.rdb == nil {
|
||||
return
|
||||
}
|
||||
b, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_ = c.rdb.Set(ctx, key, b, ttl).Err()
|
||||
}
|
||||
|
||||
func (c *redisCache) Delete(ctx context.Context, keys ...string) {
|
||||
if c.rdb == nil || len(keys) == 0 {
|
||||
return
|
||||
}
|
||||
_ = c.rdb.Del(ctx, keys...).Err()
|
||||
}
|
||||
|
||||
// ---------------- 内存实现(测试 / 开发 / 降级) ----------------
|
||||
|
||||
type memItem struct {
|
||||
data []byte
|
||||
exp time.Time
|
||||
}
|
||||
|
||||
type memCache struct {
|
||||
mu sync.RWMutex
|
||||
items map[string]memItem
|
||||
}
|
||||
|
||||
// NewMemory 返回进程内内存缓存实现。
|
||||
func NewMemory() Cache {
|
||||
return &memCache{items: make(map[string]memItem)}
|
||||
}
|
||||
|
||||
func (c *memCache) GetJSON(ctx context.Context, key string, dest any) bool {
|
||||
c.mu.RLock()
|
||||
it, ok := c.items[key]
|
||||
c.mu.RUnlock()
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if !it.exp.IsZero() && time.Now().After(it.exp) {
|
||||
c.mu.Lock()
|
||||
delete(c.items, key)
|
||||
c.mu.Unlock()
|
||||
return false
|
||||
}
|
||||
return json.Unmarshal(it.data, dest) == nil
|
||||
}
|
||||
|
||||
func (c *memCache) SetJSON(ctx context.Context, key string, val any, ttl time.Duration) {
|
||||
b, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var exp time.Time
|
||||
if ttl > 0 {
|
||||
exp = time.Now().Add(ttl)
|
||||
}
|
||||
c.mu.Lock()
|
||||
c.items[key] = memItem{data: b, exp: exp}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func (c *memCache) Delete(ctx context.Context, keys ...string) {
|
||||
c.mu.Lock()
|
||||
for _, k := range keys {
|
||||
delete(c.items, k)
|
||||
}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
Vendored
+63
@@ -0,0 +1,63 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMemCache_SetGetRoundTrip(t *testing.T) {
|
||||
c := NewMemory()
|
||||
ctx := context.Background()
|
||||
in := []map[string]any{{"id": "1", "name": "测试"}}
|
||||
c.SetJSON(ctx, "k", in, time.Minute)
|
||||
|
||||
var out []map[string]any
|
||||
if !c.GetJSON(ctx, "k", &out) {
|
||||
t.Fatal("应命中")
|
||||
}
|
||||
if len(out) != 1 || out[0]["name"] != "测试" {
|
||||
t.Fatalf("JSON 往返不一致: %v", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemCache_MissOnAbsent(t *testing.T) {
|
||||
var out []map[string]any
|
||||
if NewMemory().GetJSON(context.Background(), "none", &out) {
|
||||
t.Fatal("不存在的键应未命中")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemCache_Expiry(t *testing.T) {
|
||||
c := NewMemory()
|
||||
ctx := context.Background()
|
||||
c.SetJSON(ctx, "k", map[string]any{"a": 1}, 10*time.Millisecond)
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
var out map[string]any
|
||||
if c.GetJSON(ctx, "k", &out) {
|
||||
t.Fatal("过期键应未命中")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMemCache_Delete(t *testing.T) {
|
||||
c := NewMemory()
|
||||
ctx := context.Background()
|
||||
c.SetJSON(ctx, "k", map[string]any{"a": 1}, time.Minute)
|
||||
c.Delete(ctx, "k")
|
||||
var out map[string]any
|
||||
if c.GetJSON(ctx, "k", &out) {
|
||||
t.Fatal("删除后应未命中")
|
||||
}
|
||||
}
|
||||
|
||||
// nil 客户端的 Redis 实现应安全降级,不 panic、不命中。
|
||||
func TestRedisCache_NilClientGraceful(t *testing.T) {
|
||||
c := NewRedis(nil)
|
||||
ctx := context.Background()
|
||||
c.SetJSON(ctx, "k", map[string]any{"a": 1}, time.Minute) // 不应 panic
|
||||
c.Delete(ctx, "k") // 不应 panic
|
||||
var out map[string]any
|
||||
if c.GetJSON(ctx, "k", &out) {
|
||||
t.Fatal("nil 客户端应始终未命中")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user