perf(chat): 使用预聚合快照优化客服会话统计
This commit is contained in:
@@ -6,19 +6,68 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const adminChatCountsCacheTTL = 5 * time.Second
|
||||
const (
|
||||
adminChatCountsCacheTTL = 30 * time.Minute
|
||||
adminChatCountsFreshFor = 15 * time.Second
|
||||
adminChatCountsRefreshTTL = 20 * time.Second
|
||||
)
|
||||
|
||||
type adminChatCountsSnapshot struct {
|
||||
Counts *AdminConversationCountsDTO `json:"counts"`
|
||||
GeneratedAt time.Time `json:"generated_at"`
|
||||
Generation int64 `json:"generation"`
|
||||
}
|
||||
|
||||
func (r *Repository) adminChatCountsCacheKey(ctx context.Context, principal Principal, filter, stage, keyword string) string {
|
||||
sum := sha256.Sum256([]byte(keyword))
|
||||
return fmt.Sprintf("admin-chat:counts:v3:%s:%d:%s:%s:%s:%s", principal.Type, principal.ID, tenancy.ScopeKey(ctx), filter, stage, hex.EncodeToString(sum[:8]))
|
||||
return fmt.Sprintf("admin-chat:counts:v4:%s:%d:%s:%s:%s:%s", principal.Type, principal.ID, tenancy.ScopeKey(ctx), filter, stage, hex.EncodeToString(sum[:8]))
|
||||
}
|
||||
|
||||
func (r *Repository) loadAdminChatCountsCache(ctx context.Context, principal Principal, filter, stage, keyword string) *AdminConversationCountsDTO {
|
||||
func adminChatCountsGenerationKey(scope string) string {
|
||||
return "admin-chat:counts:generation:" + scope
|
||||
}
|
||||
|
||||
func (r *Repository) adminChatCountsGeneration(ctx context.Context) int64 {
|
||||
if r.redis == nil {
|
||||
return 0
|
||||
}
|
||||
raw, err := r.redis.Get(ctx, adminChatCountsGenerationKey(tenancy.ScopeKey(ctx))).Result()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
value, _ := strconv.ParseInt(raw, 10, 64)
|
||||
return value
|
||||
}
|
||||
|
||||
func (r *Repository) invalidateAdminChatCounts(ctx context.Context, tenantIDs []uint64) {
|
||||
if r.redis == nil {
|
||||
return
|
||||
}
|
||||
seen := make(map[uint64]struct{}, len(tenantIDs))
|
||||
for _, tenantID := range tenantIDs {
|
||||
if tenantID == 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[tenantID]; ok {
|
||||
continue
|
||||
}
|
||||
seen[tenantID] = struct{}{}
|
||||
_ = r.redis.Incr(ctx, adminChatCountsGenerationKey(fmt.Sprintf("tenant:%d", tenantID))).Err()
|
||||
}
|
||||
if len(seen) == 0 {
|
||||
_ = r.redis.Incr(ctx, adminChatCountsGenerationKey(tenancy.ScopeKey(ctx))).Err()
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Repository) loadAdminChatCountsSnapshot(ctx context.Context, principal Principal, filter, stage, keyword string) *adminChatCountsSnapshot {
|
||||
if r.redis == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -26,20 +75,73 @@ func (r *Repository) loadAdminChatCountsCache(ctx context.Context, principal Pri
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var value AdminConversationCountsDTO
|
||||
var value adminChatCountsSnapshot
|
||||
if err := json.Unmarshal(raw, &value); err != nil {
|
||||
return nil
|
||||
}
|
||||
if value.Counts == nil || value.GeneratedAt.IsZero() {
|
||||
return nil
|
||||
}
|
||||
return &value
|
||||
}
|
||||
|
||||
func (r *Repository) storeAdminChatCountsCache(ctx context.Context, principal Principal, filter, stage, keyword string, value *AdminConversationCountsDTO) {
|
||||
func (r *Repository) storeAdminChatCountsSnapshot(ctx context.Context, principal Principal, filter, stage, keyword string, value *AdminConversationCountsDTO, generation int64) {
|
||||
if r.redis == nil || value == nil {
|
||||
return
|
||||
}
|
||||
raw, err := json.Marshal(value)
|
||||
raw, err := json.Marshal(adminChatCountsSnapshot{
|
||||
Counts: value,
|
||||
GeneratedAt: time.Now(),
|
||||
Generation: generation,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_ = r.redis.Set(ctx, r.adminChatCountsCacheKey(ctx, principal, filter, stage, keyword), raw, adminChatCountsCacheTTL).Err()
|
||||
}
|
||||
|
||||
func (r *Repository) refreshAdminChatCounts(ctx context.Context, principal Principal, filter, stage, keyword string) (*AdminConversationCountsDTO, error) {
|
||||
key := r.adminChatCountsCacheKey(ctx, principal, filter, stage, keyword)
|
||||
value, err, _ := r.countsGroup.Do(key, func() (any, error) {
|
||||
generation := r.adminChatCountsGeneration(ctx)
|
||||
result, err := r.adminConversationCounts(ctx, principal, filter, stage, keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.storeAdminChatCountsSnapshot(ctx, principal, filter, stage, keyword, result.DTO, generation)
|
||||
return result.DTO, nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
counts, ok := value.(*AdminConversationCountsDTO)
|
||||
if !ok || counts == nil {
|
||||
return nil, fmt.Errorf("invalid admin chat counts snapshot")
|
||||
}
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
func (r *Repository) refreshAdminChatCountsAsync(ctx context.Context, principal Principal, filter, stage, keyword string) {
|
||||
if r.redis == nil {
|
||||
return
|
||||
}
|
||||
key := r.adminChatCountsCacheKey(ctx, principal, filter, stage, keyword)
|
||||
ch := r.countsGroup.DoChan(key, func() (any, error) {
|
||||
refreshCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), adminChatCountsRefreshTTL)
|
||||
defer cancel()
|
||||
generation := r.adminChatCountsGeneration(refreshCtx)
|
||||
result, err := r.adminConversationCounts(refreshCtx, principal, filter, stage, keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.storeAdminChatCountsSnapshot(refreshCtx, principal, filter, stage, keyword, result.DTO, generation)
|
||||
return result.DTO, nil
|
||||
})
|
||||
select {
|
||||
case result := <-ch:
|
||||
if result.Err != nil && r.logger != nil {
|
||||
r.logger.Warn("后台客服统计刷新失败", zap.Error(result.Err))
|
||||
}
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func TestAdminConversationCountsUsesFreshSnapshot(t *testing.T) {
|
||||
db := setupAdminListTestDB(t)
|
||||
client := redis.NewClient(&redis.Options{Addr: "127.0.0.1:16379", DB: 15})
|
||||
t.Cleanup(func() {
|
||||
_ = client.Close()
|
||||
})
|
||||
if err := client.Ping(t.Context()).Err(); err != nil {
|
||||
t.Skipf("Redis 测试服务不可用: %v", err)
|
||||
}
|
||||
|
||||
admin := model.AdminUser{Username: "counts-cache", Nickname: "缓存测试", Status: "active"}
|
||||
if err := db.Create(&admin).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conversation := model.ChatConversation{
|
||||
TenantID: 1,
|
||||
Type: ConversationTypeGeneralSupport,
|
||||
Title: "缓存测试会话",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&conversation).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
participant := model.ChatParticipant{
|
||||
ConversationID: conversation.ID,
|
||||
ParticipantType: "admin",
|
||||
ParticipantID: admin.ID,
|
||||
Role: "support",
|
||||
JoinedAt: time.Now(),
|
||||
}
|
||||
if err := db.Create(&participant).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
repo := NewRepository(db, nil, client)
|
||||
ctx := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: 1})
|
||||
principal := Principal{Type: "admin", ID: admin.ID}
|
||||
cacheKey := repo.adminChatCountsCacheKey(ctx, principal, adminChatFilterMine, adminChatStageAll, "")
|
||||
t.Cleanup(func() {
|
||||
_ = client.Del(t.Context(), cacheKey).Err()
|
||||
})
|
||||
|
||||
first, err := repo.AdminConversationCounts(ctx, principal, adminChatFilterMine, adminChatStageAll, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first.Ownership[adminChatFilterMine] != 1 {
|
||||
t.Fatalf("首次统计 mine = %d, want 1", first.Ownership[adminChatFilterMine])
|
||||
}
|
||||
|
||||
if err := db.Delete(&participant).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cached, err := repo.AdminConversationCounts(ctx, principal, adminChatFilterMine, adminChatStageAll, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cached.Ownership[adminChatFilterMine] != 1 {
|
||||
t.Fatalf("15 秒内应读取快照,mine = %d, want 1", cached.Ownership[adminChatFilterMine])
|
||||
}
|
||||
|
||||
repo.invalidateAdminChatCounts(ctx, []uint64{1})
|
||||
|
||||
stale, err := repo.AdminConversationCounts(ctx, principal, adminChatFilterMine, adminChatStageAll, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !stale.Refreshing {
|
||||
t.Fatal("过期快照应标记 refreshing")
|
||||
}
|
||||
|
||||
raw, err := client.Get(t.Context(), cacheKey).Bytes()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var snapshot adminChatCountsSnapshot
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
raw, err = client.Get(t.Context(), cacheKey).Bytes()
|
||||
if err == nil && json.Unmarshal(raw, &snapshot) == nil && snapshot.Counts.Ownership[adminChatFilterMine] == 0 {
|
||||
return
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("过期快照未在超时前完成后台刷新")
|
||||
}
|
||||
@@ -186,6 +186,7 @@ func (r *Repository) SendMessage(ctx context.Context, principal Principal, conve
|
||||
},
|
||||
}
|
||||
tenantScopedEvent(r.db.WithContext(ctx), event)
|
||||
r.invalidateAdminChatCounts(ctx, event.TenantIDs)
|
||||
r.hub.NotifyConversation(conversationID, event)
|
||||
// 同时广播给所有在线客服,使「未分配 / 全部」视图实时刷新;
|
||||
// 参与者客服会重复收到,由前端按消息 id 去重。
|
||||
|
||||
@@ -805,6 +805,7 @@ func (r *Repository) notifyDeliveredQrCodeMessages(messages []model.ChatMessage)
|
||||
},
|
||||
}
|
||||
tenantScopedEvent(r.db, event)
|
||||
r.invalidateAdminChatCounts(context.Background(), event.TenantIDs)
|
||||
r.hub.NotifyConversation(msg.ConversationID, event)
|
||||
r.hub.NotifyAllAdmins(event)
|
||||
}
|
||||
|
||||
@@ -9,8 +9,10 @@ import (
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/sync/singleflight"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/modules/chathub"
|
||||
)
|
||||
@@ -24,6 +26,7 @@ type Repository struct {
|
||||
ocrGate *ocrRequestGate
|
||||
ocrSleep func(context.Context, time.Duration) error
|
||||
ocrServiceURL string
|
||||
countsGroup singleflight.Group
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -177,14 +180,15 @@ func EnsureOrderConversation(tx *gorm.DB, order model.RentalOrder) (*model.ChatC
|
||||
return &conversation, nil
|
||||
}
|
||||
func (r *Repository) NotifyNewConversation(conversationID uint64) {
|
||||
if r.hub == nil {
|
||||
return
|
||||
}
|
||||
event := &chathub.ChatEvent{
|
||||
Type: "conversation_updated",
|
||||
ConversationID: conversationID,
|
||||
}
|
||||
tenantScopedEvent(r.db, event)
|
||||
r.invalidateAdminChatCounts(context.Background(), event.TenantIDs)
|
||||
if r.hub == nil {
|
||||
return
|
||||
}
|
||||
r.hub.NotifyConversation(conversationID, event)
|
||||
// 新会话(如未分配的平台咨询)需让所有在线客服的列表实时出现。
|
||||
r.hub.NotifyAllAdmins(event)
|
||||
|
||||
@@ -30,8 +30,9 @@ var adminChatFilters = []string{adminChatFilterMine, adminChatFilterAll, adminCh
|
||||
var adminChatStages = []string{adminChatStageAll, adminChatStagePending, adminChatStageUnjoined, adminChatStageHandoff, adminChatStageRenting, adminChatStageAfterSale, adminChatStageEnded}
|
||||
|
||||
type AdminConversationCountsDTO struct {
|
||||
Ownership map[string]int64 `json:"ownership"`
|
||||
Stages map[string]int64 `json:"stages"`
|
||||
Ownership map[string]int64 `json:"ownership"`
|
||||
Stages map[string]int64 `json:"stages"`
|
||||
Refreshing bool `json:"refreshing,omitempty"`
|
||||
}
|
||||
|
||||
func (r *Repository) TransferConversation(ctx context.Context, principal Principal, conversationID uint64, toAdminID uint64) error {
|
||||
@@ -98,10 +99,13 @@ func (r *Repository) TransferConversation(ctx context.Context, principal Princip
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err == nil && r.hub != nil {
|
||||
if err == nil {
|
||||
event := tenantScopedEvent(r.db.WithContext(ctx), &chathub.ChatEvent{Type: "conversation_updated", ConversationID: conversationID})
|
||||
r.hub.NotifyConversation(conversationID, event)
|
||||
r.hub.NotifyAllAdmins(event)
|
||||
r.invalidateAdminChatCounts(ctx, event.TenantIDs)
|
||||
if r.hub != nil {
|
||||
r.hub.NotifyConversation(conversationID, event)
|
||||
r.hub.NotifyAllAdmins(event)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -288,15 +292,25 @@ func (r *Repository) AdminConversationCounts(ctx context.Context, principal Prin
|
||||
filter = normalizeAdminChatFilter(filter)
|
||||
stage = normalizeAdminChatStage(stage)
|
||||
keyword = strings.TrimSpace(keyword)
|
||||
if cached := r.loadAdminChatCountsCache(ctx, principal, filter, stage, keyword); cached != nil {
|
||||
return cached, nil
|
||||
if r.redis == nil || keyword != "" {
|
||||
result, err := r.adminConversationCounts(ctx, principal, filter, stage, keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.DTO, nil
|
||||
}
|
||||
result, err := r.adminConversationCounts(ctx, principal, filter, stage, keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
generation := r.adminChatCountsGeneration(ctx)
|
||||
if cached := r.loadAdminChatCountsSnapshot(ctx, principal, filter, stage, keyword); cached != nil {
|
||||
if cached.Generation == generation && time.Since(cached.GeneratedAt) <= adminChatCountsFreshFor {
|
||||
return cached.Counts, nil
|
||||
}
|
||||
r.refreshAdminChatCountsAsync(ctx, principal, filter, stage, keyword)
|
||||
stale := *cached.Counts
|
||||
stale.Refreshing = true
|
||||
return &stale, nil
|
||||
}
|
||||
r.storeAdminChatCountsCache(ctx, principal, filter, stage, keyword, result.DTO)
|
||||
return result.DTO, nil
|
||||
return r.refreshAdminChatCounts(ctx, principal, filter, stage, keyword)
|
||||
}
|
||||
|
||||
func (r *Repository) adminConversationTotal(ctx context.Context, principal Principal, filter string, stage string, keyword string) (int64, error) {
|
||||
@@ -397,7 +411,7 @@ func (r *Repository) adminConversationCounts(ctx context.Context, principal Prin
|
||||
StageAll, StagePending, StageUnjoined, StageHandoff, StageRenting, StageAfterSale, StageEnded int64
|
||||
}
|
||||
var result row
|
||||
if err := r.adminConversationBase(ctx, principal, keyword).Select(strings.Join(parts, ", "), args...).Scan(&result).Error; err != nil {
|
||||
if err := r.adminConversationCountsBase(ctx, principal).Select(strings.Join(parts, ", "), args...).Scan(&result).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &adminConversationCountsResult{
|
||||
@@ -409,6 +423,19 @@ func (r *Repository) adminConversationCounts(ctx context.Context, principal Prin
|
||||
}, nil
|
||||
}
|
||||
|
||||
// adminConversationCountsBase 只保留统计条件真正使用的关联。
|
||||
// 管理端的展示关联不参与计数,避免每次统计都额外访问状态表和商品表。
|
||||
func (r *Repository) adminConversationCountsBase(ctx context.Context, principal Principal) *gorm.DB {
|
||||
db := r.db.WithContext(ctx).Table("chat_conversations AS c").
|
||||
Joins("LEFT JOIN chat_participants AS cp_me ON cp_me.conversation_id = c.id AND cp_me.participant_type = ? AND cp_me.participant_id = ?", principal.Type, principal.ID).
|
||||
Joins("LEFT JOIN rental_orders AS explicit_lo ON explicit_lo.id = c.order_id")
|
||||
scoped, err := adminConversationScope(ctx, db, principal.ID)
|
||||
if err == nil {
|
||||
return scoped
|
||||
}
|
||||
return tenantConversationScope(ctx, db)
|
||||
}
|
||||
|
||||
func normalizeAdminChatFilter(filter string) string {
|
||||
switch filter {
|
||||
case adminChatFilterAll, adminChatFilterMine, adminChatFilterUnassigned:
|
||||
|
||||
Reference in New Issue
Block a user