优化客服云权限安全与访客IP解析
This commit is contained in:
@@ -3,11 +3,19 @@ package handler
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"kefu-cloud/server/internal/model"
|
||||
"kefu-cloud/server/internal/ws"
|
||||
)
|
||||
|
||||
var assignmentLocks sync.Map
|
||||
|
||||
func assignmentLock(tenantID uint) *sync.Mutex {
|
||||
lock, _ := assignmentLocks.LoadOrStore(tenantID, &sync.Mutex{})
|
||||
return lock.(*sync.Mutex)
|
||||
}
|
||||
|
||||
const (
|
||||
assignStrategyLeastLoad = "least_load"
|
||||
assignStrategyRoundRobin = "round_robin"
|
||||
@@ -138,6 +146,16 @@ func tryAutoAssign(session *model.Session) (*model.User, error) {
|
||||
if session.Status != "waiting" || session.AgentID != nil {
|
||||
return nil, nil
|
||||
}
|
||||
// 负载统计与会话更新必须串行,否则并发新会话会同时看到旧负载并突破最大并发。
|
||||
lock := assignmentLock(session.TenantID)
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
if err := model.DB.First(session, session.ID).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if session.Status != "waiting" || session.AgentID != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
setting, err := getOrCreateTenantSettings(session.TenantID)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user