feat(phase2e-2): 落地会议生命周期状态机 + Router 幂等双层防御(Task 8)
核心交付:
- 新建 MeetingLifecycleService(6 钩子 + sync.Map 本地 timer + Redis key 双保险 + RescheduleFromRedis)
- 新建 MeetingCleanupTask(启动重建 timer + 每 N 秒扫 host_grace/empty_ttl 兜底 + 4h stale active 回收)
- MediaOrchestrator 新增 ResolveRouterID;HTTPMediaOrchestrator.CreateRouter 入口 sync.Map 幂等防御
- 业务层 JoinRoom 移除 CreateRouter 调用改走 CancelEmptyTTL + ResolveRouterID;LeaveRoom 空房分支改调 OnAllMembersLeft 不再立即销毁
- MeetingSignalService 新增 OnWSDisconnect 实现 ws.MeetingDisconnectHook;OnRoomJoin 追加 host 重连钩子
- ws.handler 定义 MeetingDisconnectHook 接口 + SetMeetingDisconnectHook,解耦 ws→meeting 反向依赖
- config 新增 MeetingConfig{HostGrace=120, EmptyRoomTTL=300, CleanupInterval=30, StaleRoomHours=4}
关键设计决策:
- Redis key TTL = 业务时长 + max(CleanupIntervalSeconds*2, 30s) buffer:避免本地 timer 与
Redis 自动过期同步到期导致 DEL 返回 0 被误判为"已被其他路径处理"而跳过业务逻辑
- Router 幂等双层防御(决策 q2_router_dedup=a2_both):业务层不重复调 + HTTP 层 sync.Map 命中直接返回
- 普通成员 WS 断开仅清 media 资源不动 participant 表(决策 q1_nonhost_disconnect=a1_keep_current)
E2E 验证:docs/verify/meeting_t8_verify.mjs PASS=20 FAIL=0,覆盖 5 场景:
- S1 host 宽限期过期自动转让(meeting.host.changed + DB host_id 更新)
- S2 宽限期内重连保留身份
- S3 empty_ttl 期内新成员加入复活房间
- S4 empty_ttl 过期 → 房间 Ended + 新 join 被拒
- S5 CreateRoom +1 Router / JoinRoom 不再创建新 Router(通过 media-server /internal/info stats.routers 断言)
media-server:/internal/info 响应追加 stats.routers + routers[] 供 E2E 断言 Router 幂等
文档同步:
- docs/progress/CURRENT_STATUS.md 头部 + 新增 Task 8 交付条目
- docs/plans/2026-04-21-phase2e-2-implementation.plan.md Task 8 标记完成 + 实际产出/决策/验证
- docs/api/frontend/meeting.md 补充 host.changed.auto_reason / room.ended.reason=system_error / 空房 TTL 复活语义 + Task 8 验证记录
- docs/architecture/system-architecture.md meeting 模块职责补充"会议生命周期状态机"
- .cursor/rules/project-context.mdc 追加 Task 8 条目并更新 Phase 2e-2 进度(Task 0-8 ✅)
Made-with: Cursor
This commit is contained in:
@@ -65,11 +65,13 @@ type MeetingService struct {
|
||||
userResolver UserInfoResolver
|
||||
onlineChecker OnlineChecker
|
||||
mediaOrchestrator MediaOrchestrator
|
||||
lifecycleSvc *MeetingLifecycleService
|
||||
}
|
||||
|
||||
// NewMeetingService 创建 MeetingService 实例
|
||||
// 依赖通过构造函数注入;接口依赖由上游 Wire 绑定到具体实现
|
||||
// Task 7 (2026-04-21) 起 mediaOrchestrator 默认绑定 HTTPMediaOrchestrator
|
||||
// Task 8 (2026-04-21) 起新增 lifecycleSvc 依赖:JoinRoom/LeaveRoom 的空房/复活场景由 lifecycleSvc 托管
|
||||
func NewMeetingService(
|
||||
roomDAO *dao.MeetingRoomDAO,
|
||||
participantDAO *dao.MeetingParticipantDAO,
|
||||
@@ -81,6 +83,7 @@ func NewMeetingService(
|
||||
userResolver UserInfoResolver,
|
||||
onlineChecker OnlineChecker,
|
||||
mediaOrchestrator MediaOrchestrator,
|
||||
lifecycleSvc *MeetingLifecycleService,
|
||||
) *MeetingService {
|
||||
return &MeetingService{
|
||||
roomDAO: roomDAO,
|
||||
@@ -93,6 +96,7 @@ func NewMeetingService(
|
||||
userResolver: userResolver,
|
||||
onlineChecker: onlineChecker,
|
||||
mediaOrchestrator: mediaOrchestrator,
|
||||
lifecycleSvc: lifecycleSvc,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,9 +214,10 @@ func (s *MeetingService) CreateRoom(ctx context.Context, hostID int64, req *dto.
|
||||
return nil, nil, "", err
|
||||
}
|
||||
|
||||
// Task 7 起 CreateRouter 对接真实 mediasoup;Task 8 起仅在会议首次创建时调一次(房间级资源)
|
||||
routerID, mediaErr := s.mediaOrchestrator.CreateRouter(ctx, code)
|
||||
if mediaErr != nil {
|
||||
logs.Warn(ctx, funcName, "mediasoup Router 创建失败(Task 7 前为占位实现,不影响流程)",
|
||||
logs.Warn(ctx, funcName, "mediasoup Router 创建失败",
|
||||
zap.String("room_code", code), zap.Error(mediaErr))
|
||||
routerID = ""
|
||||
}
|
||||
@@ -309,10 +314,18 @@ func (s *MeetingService) JoinRoom(ctx context.Context, userID int64, code, passw
|
||||
return nil, nil, "", err
|
||||
}
|
||||
|
||||
routerID, mediaErr := s.mediaOrchestrator.CreateRouter(ctx, code)
|
||||
if mediaErr != nil {
|
||||
logs.Warn(ctx, funcName, "mediasoup Router 创建失败(占位实现)", zap.String("room_code", code), zap.Error(mediaErr))
|
||||
routerID = ""
|
||||
// Task 8:空房 TTL 复活
|
||||
// 若该房间正处于 empty_ttl 阶段(全员离开后的 5 分钟窗口),新成员加入立即取消销毁
|
||||
if s.lifecycleSvc != nil {
|
||||
s.lifecycleSvc.CancelEmptyTTL(ctx, code)
|
||||
}
|
||||
|
||||
// Task 8:JoinRoom 不再主动调 CreateRouter(Router 在 CreateRoom 时创建、由 HTTPMediaOrchestrator 本地缓存)
|
||||
// 从缓存读取 routerID;缺失时(极少见:服务重启后未重建缓存)保持为空,不阻塞加入流程
|
||||
routerID, _ := s.mediaOrchestrator.ResolveRouterID(code)
|
||||
if routerID == "" {
|
||||
logs.Debug(ctx, funcName, "RouterID 缓存缺失(非致命,可能服务重启)",
|
||||
zap.String("room_code", code))
|
||||
}
|
||||
|
||||
go s.broadcastToActiveParticipants(context.Background(), room.ID, constants.MeetingWSEventMemberJoined, map[string]interface{}{
|
||||
@@ -378,9 +391,10 @@ func (s *MeetingService) LeaveRoom(ctx context.Context, userID int64, code strin
|
||||
}
|
||||
}
|
||||
|
||||
if len(actives) == 0 {
|
||||
_, _ = s.roomDAO.MarkEnded(ctx, room.ID, constants.MeetingEndedReasonEmptyTTL, time.Now())
|
||||
_ = s.mediaOrchestrator.CloseRouter(ctx, code)
|
||||
// Task 8:全员离会改走生命周期状态机(空房 TTL),不再立即销毁房间
|
||||
// 在 TTL 窗口内如有新成员加入,房间会被 CancelEmptyTTL 复活;TTL 到期由 HandleEmptyRoomExpired 兜底销毁
|
||||
if len(actives) == 0 && s.lifecycleSvc != nil {
|
||||
s.lifecycleSvc.OnAllMembersLeft(ctx, code)
|
||||
}
|
||||
|
||||
go s.broadcastToActiveParticipants(context.Background(), room.ID, constants.MeetingWSEventMemberLeft, map[string]interface{}{
|
||||
|
||||
Reference in New Issue
Block a user