fix(stability): 修复慢查询雪崩隐患、系统配置死锁与提号翻页错误
- 消除 system_configs 写锁争抢:使用读写锁与启动预热将默认配置初始化收敛为单例,杜绝读请求开事务导致的 Lock wait timeout exceeded - 强化财务看板缓存:将 Redis 缓存有效期由 1 分钟延长为 5 分钟,接入 singleflight.Group 防并发击穿 - 修复提号列表 GORM 翻页 Bug:分离 Count 查询与列表查询构建,避免重复 Join 导致的 Error 1066 - 生产环境资源加固:docker-compose.prod.yml 中 MySQL 资源限制提升至 2.0 核 CPU、2G 内存 - 补充 systemconfig 并发测试与 pickup 分页 Count 回归测试
This commit is contained in:
@@ -2,6 +2,7 @@ package adminfinance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"hfb_sys/backend/internal/timeutil"
|
||||
@@ -11,6 +12,26 @@ func (r *Repository) Dashboard(ctx context.Context, query DashboardQuery) (*Dash
|
||||
if cached := r.loadDashboardCache(ctx, query); cached != nil {
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
key := r.dashboardCacheKey(query)
|
||||
val, err, _ := r.sfGroup.Do(key, func() (any, error) {
|
||||
// 在 singleflight 内部二次检查缓存,避免排队等待的并发请求重复执行大聚合
|
||||
if cached := r.loadDashboardCache(ctx, query); cached != nil {
|
||||
return cached, nil
|
||||
}
|
||||
return r.calculateDashboard(ctx, query)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res, ok := val.(*DashboardDTO)
|
||||
if !ok {
|
||||
return nil, errors.New("invalid dashboard result type")
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (r *Repository) calculateDashboard(ctx context.Context, query DashboardQuery) (*DashboardDTO, error) {
|
||||
// 各板块的读取互不依赖。并行执行能避免单个请求串行等待二十余条统计 SQL,
|
||||
// 首次打开或缓存失效时仍可尽快返回;每个查询继续复用 GORM 的连接池。
|
||||
var (
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const dashboardCacheTTL = time.Minute
|
||||
const dashboardCacheTTL = 5 * time.Minute
|
||||
|
||||
func (r *Repository) dashboardCacheKey(query DashboardQuery) string {
|
||||
return fmt.Sprintf("admin-finance:dashboard:v1:%d:%d", query.StartDate.Unix(), query.EndDate.Unix())
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package adminfinance
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -369,3 +371,33 @@ func TestSummaryFromDailyRowsPreservesDashboardTotals(t *testing.T) {
|
||||
t.Fatalf("订单状态汇总错误: %+v", summary)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDashboardCacheKeyAndSingleflight(t *testing.T) {
|
||||
repo := NewRepository(nil)
|
||||
q := DashboardQuery{
|
||||
StartDate: time.Date(2026, 9, 1, 0, 0, 0, 0, time.UTC),
|
||||
EndDate: time.Date(2026, 9, 7, 0, 0, 0, 0, time.UTC),
|
||||
}
|
||||
key := repo.dashboardCacheKey(q)
|
||||
if key == "" {
|
||||
t.Fatal("cache key should not be empty")
|
||||
}
|
||||
|
||||
var callCount atomic.Int32
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
_, _, _ = repo.sfGroup.Do(key, func() (any, error) {
|
||||
callCount.Add(1)
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
return &DashboardDTO{}, nil
|
||||
})
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
if count := callCount.Load(); count != 1 {
|
||||
t.Fatalf("singleflight 并发合并失败: got %d, want 1", count)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,14 @@ package adminfinance
|
||||
|
||||
import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
"golang.org/x/sync/singleflight"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
db *gorm.DB
|
||||
redis *redis.Client
|
||||
sfGroup singleflight.Group
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB, redisClient ...*redis.Client) *Repository {
|
||||
|
||||
Reference in New Issue
Block a user