feat: refact id column define
This commit is contained in:
@@ -15,11 +15,11 @@ import (
|
||||
|
||||
// AgentWSManager WebSocket 连接管理器
|
||||
type AgentWSManager struct {
|
||||
connections map[uint]*AgentConnection // Agent ID -> 连接对象
|
||||
connections map[string]*AgentConnection // Agent ID -> 连接对象
|
||||
ipConnections map[string]int // IP -> 连接数
|
||||
ipLastAttempt map[string]time.Time // IP -> 最后连接尝试时间
|
||||
ipFailCount map[string]int // IP -> 连续失败次数
|
||||
remoteWaiters map[uint]chan *models.AgentTaskResult // 日志 ID -> 结果通道
|
||||
remoteWaiters map[string]chan *models.AgentTaskResult // 日志 ID -> 结果通道
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ const (
|
||||
|
||||
// AgentConnection Agent WebSocket 连接
|
||||
type AgentConnection struct {
|
||||
AgentID uint
|
||||
AgentID string
|
||||
IP string
|
||||
Conn *websocket.Conn
|
||||
Send chan []byte
|
||||
@@ -72,11 +72,11 @@ var agentWSOnce sync.Once
|
||||
func GetAgentWSManager() *AgentWSManager {
|
||||
agentWSOnce.Do(func() {
|
||||
agentWSManager = &AgentWSManager{
|
||||
connections: make(map[uint]*AgentConnection),
|
||||
connections: make(map[string]*AgentConnection),
|
||||
ipConnections: make(map[string]int),
|
||||
ipLastAttempt: make(map[string]time.Time),
|
||||
ipFailCount: make(map[string]int),
|
||||
remoteWaiters: make(map[uint]chan *models.AgentTaskResult),
|
||||
remoteWaiters: make(map[string]chan *models.AgentTaskResult),
|
||||
}
|
||||
go agentWSManager.cleanupLoop()
|
||||
})
|
||||
@@ -137,7 +137,7 @@ func (m *AgentWSManager) RecordConnectSuccess(ip string) {
|
||||
}
|
||||
|
||||
// Register 注册连接
|
||||
func (m *AgentWSManager) Register(agentID uint, conn *websocket.Conn, ip string) *AgentConnection {
|
||||
func (m *AgentWSManager) Register(agentID string, conn *websocket.Conn, ip string) *AgentConnection {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
@@ -164,12 +164,12 @@ func (m *AgentWSManager) Register(agentID uint, conn *websocket.Conn, ip string)
|
||||
// 增加 IP 连接计数
|
||||
m.ipConnections[ip]++
|
||||
|
||||
logger.Infof("[AgentWS] Agent #%d 已连接 (%s)", agentID, ip)
|
||||
logger.Infof("[AgentWS] Agent #%s 已连接 (%s)", agentID, ip)
|
||||
return ac
|
||||
}
|
||||
|
||||
// Unregister 注销连接(只注销指定的连接实例)
|
||||
func (m *AgentWSManager) Unregister(agentID uint, ac *AgentConnection) {
|
||||
func (m *AgentWSManager) Unregister(agentID string, ac *AgentConnection) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
@@ -183,19 +183,19 @@ func (m *AgentWSManager) Unregister(agentID uint, ac *AgentConnection) {
|
||||
}
|
||||
conn.Close()
|
||||
delete(m.connections, agentID)
|
||||
logger.Infof("[AgentWS] Agent #%d 已断开", agentID)
|
||||
logger.Infof("[AgentWS] Agent #%s 已断开", agentID)
|
||||
}
|
||||
}
|
||||
|
||||
// GetConnection 获取连接
|
||||
func (m *AgentWSManager) GetConnection(agentID uint) *AgentConnection {
|
||||
func (m *AgentWSManager) GetConnection(agentID string) *AgentConnection {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
return m.connections[agentID]
|
||||
}
|
||||
|
||||
// SendToAgent 发送消息给指定 Agent
|
||||
func (m *AgentWSManager) SendToAgent(agentID uint, msgType string, data interface{}) error {
|
||||
func (m *AgentWSManager) SendToAgent(agentID string, msgType string, data interface{}) error {
|
||||
conn := m.GetConnection(agentID)
|
||||
if conn == nil {
|
||||
return nil // Agent 不在线
|
||||
@@ -214,7 +214,7 @@ func (m *AgentWSManager) SendToAgent(agentID uint, msgType string, data interfac
|
||||
}
|
||||
|
||||
// BroadcastTasks 广播任务更新给指定 Agent
|
||||
func (m *AgentWSManager) BroadcastTasks(agentID uint) {
|
||||
func (m *AgentWSManager) BroadcastTasks(agentID string) {
|
||||
agentService := NewAgentService()
|
||||
tasks := agentService.GetTasks(agentID)
|
||||
m.SendToAgent(agentID, WSTypeTasks, map[string]interface{}{
|
||||
@@ -223,7 +223,7 @@ func (m *AgentWSManager) BroadcastTasks(agentID uint) {
|
||||
}
|
||||
|
||||
// RegisterRemoteWaiter 注册远程任务结果等待者
|
||||
func (m *AgentWSManager) RegisterRemoteWaiter(logID uint) chan *models.AgentTaskResult {
|
||||
func (m *AgentWSManager) RegisterRemoteWaiter(logID string) chan *models.AgentTaskResult {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
ch := make(chan *models.AgentTaskResult, 1)
|
||||
@@ -232,7 +232,7 @@ func (m *AgentWSManager) RegisterRemoteWaiter(logID uint) chan *models.AgentTask
|
||||
}
|
||||
|
||||
// UnregisterRemoteWaiter 注销远程任务结果等待者
|
||||
func (m *AgentWSManager) UnregisterRemoteWaiter(logID uint) {
|
||||
func (m *AgentWSManager) UnregisterRemoteWaiter(logID string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
delete(m.remoteWaiters, logID)
|
||||
@@ -293,7 +293,7 @@ func (m *AgentWSManager) cleanupLoop() {
|
||||
delete(m.connections, agentID)
|
||||
// 更新数据库状态
|
||||
database.DB.Model(&models.Agent{}).Where("id = ?", agentID).Update("status", constant.AgentStatusOffline)
|
||||
logger.Infof("[AgentWS] Agent #%d 心跳超时,已断开", agentID)
|
||||
logger.Infof("[AgentWS] Agent #%s 心跳超时,已断开", agentID)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user