⚡️ perf(fqapi): 移除缓存层全局串行锁

- 删除 cache_manager/audio_proxy/device_key_pool 中 21 处 cacheMutex:database/sql 连接池本身并发安全,SQLite 已配 WAL + busy_timeout,PostgreSQL 无并发写限制
- contentRequestCount 三函数由互斥锁改为 atomic.Int64
- cache_manager 中 config.GetConfig() 改为 GetAppConfig(),消除配置未初始化时 log.Fatal 崩溃整个进程的隐患
- VACUUM 与写操作的低频并发冲突由 busy_timeout 兜底(已注释说明)
- 新增 TestConcurrentCacheAccess:8 goroutine 并发读写缓存,-race 验证无死锁无竞争
This commit is contained in:
chenxin committed 2026-09-29 23:21:10 +08:00
1 parent 24c7e4b6fd
commit f0d89b462b
4 files changed
+68 -68

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
package fqapi
import (
"database/sql"
"fanqie/internal/config"
"fmt"
"sync"
"testing"
_ "github.com/mattn/go-sqlite3"
)
// TestConcurrentCacheAccess 验证删除全局串行锁后并发读写缓存不死锁、不 panic、数据正确(配合 -race)
func TestConcurrentCacheAccess(t *testing.T) {
if err := config.LoadConfig("../../config/config.yaml"); err != nil {
t.Skipf("无法加载配置: %v", err)
}
db, err := sql.Open("sqlite3", t.TempDir()+"/test.db?_journal_mode=WAL&_busy_timeout=5000")
if err != nil {
t.Fatal(err)
}
oldDB, oldDriver := cacheDB, dbDriver
cacheDB, dbDriver = db, "sqlite3"
defer func() {
cacheDB, dbDriver = oldDB, oldDriver
db.Close()
}()
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS cache (
key TEXT PRIMARY KEY,
value TEXT,
created_at INTEGER DEFAULT (strftime('%s', 'now'))
)`); err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
for i := 0; i < 8; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
for j := 0; j < 30; j++ {
key := fmt.Sprintf("k-%d-%d", i, j)
if err := UpdateCache(key, "value-"+key, 60); err != nil {
t.Errorf("UpdateCache(%s): %v", key, err)
return
}
if got := GetCache(key); got != "value-"+key {
t.Errorf("GetCache(%s) = %q, want %q", key, got, "value-"+key)
return
}
}
}(i)
}
wg.Wait()
}