feat(ws): A3 WebSocket 统一连接管理 — 心跳/注册表/监控/优雅关闭
- 新增 pkg/wshub/manager.go 统一 WebSocket 连接管理器 (WSManager) - 6 个 WebSocket handler 全部迁移: WebShell/kubectl/PodExec/Audit/Metrics/Pipeline - 心跳机制: ping(30s) + pong handler + read deadline(60s) 自动检测死连接 - 连接注册表: 元数据(类型/用户/远程地址/连接时间/最后活跃) - 每用户连接限制 MaxConnsPerUser=15 防资源耗尽 - 监控端点: GET /api/v1/ws/stats + GET /api/v1/ws/connections - 优雅关闭: CloseAll() 发送 CloseGoingAway 帧并关闭所有连接 - 消除 3 个重复 websocket.Upgrader 变量统一为 WSManager 内部 upgrader - 6/6 A3 测试通过, 17/17 回归测试通过
This commit is contained in:
1 parent
00a98e210d
commit
e4f1018ca1
13 files changed
+508
-58
No files matched your search
@@ -12,8 +12,10 @@ import (
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"ezops/middleware"
|
||||
"ezops/model"
|
||||
"ezops/pkg/toolinstaller"
|
||||
"ezops/pkg/wshub"
|
||||
|
||||
"github.com/creack/pty"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -23,13 +25,14 @@ import (
|
||||
|
||||
// KubectlShellHandler kubectl WebSocket 终端处理(PTY 模式)
|
||||
type KubectlShellHandler struct {
|
||||
db *gorm.DB
|
||||
toolInstaller *toolinstaller.ToolInstaller
|
||||
db *gorm.DB
|
||||
toolInstaller *toolinstaller.ToolInstaller
|
||||
wsMgr *wshub.WSManager
|
||||
}
|
||||
|
||||
// NewKubectlShellHandler creates handler
|
||||
func NewKubectlShellHandler(db *gorm.DB, ti *toolinstaller.ToolInstaller) *KubectlShellHandler {
|
||||
return &KubectlShellHandler{db: db, toolInstaller: ti}
|
||||
func NewKubectlShellHandler(db *gorm.DB, ti *toolinstaller.ToolInstaller, wsMgr *wshub.WSManager) *KubectlShellHandler {
|
||||
return &KubectlShellHandler{db: db, toolInstaller: ti, wsMgr: wsMgr}
|
||||
}
|
||||
|
||||
// wsWriter wraps a websocket.Conn as an io.Writer (each write = one WSMessage)
|
||||
@@ -68,12 +71,16 @@ func (h *KubectlShellHandler) Terminal(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||
userID := middleware.GetUserID(c)
|
||||
username := middleware.GetUsername(c)
|
||||
|
||||
conn, info, err := h.wsMgr.Upgrade(c, wshub.TypeKubectl, userID, username)
|
||||
if err != nil {
|
||||
log.Printf("[KubectlShell] WebSocket upgrade failed: %v", err)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
defer h.wsMgr.Unregister(info)
|
||||
h.wsMgr.StartHeartbeat(conn, info)
|
||||
|
||||
// Write kubeconfig to a temp file (isolated per session)
|
||||
tmpFile, err := os.CreateTemp("", "kubeconfig-*.yaml")
|
||||
|
||||
Reference in new issue
Block a user