阶段七:完成 React Feature 化与前置重构收口
This commit is contained in:
1 parent
375e84d663
commit
1a228dc35c
305 files changed
+14179
-5431
No files matched your search
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
// accountLifecycle 管理单账号运行上下文和业务任务生命周期。
|
||||
// mu 保护 stopFn、stopped、runtimeCtx、accepting 以及 taskWG.Add 与停止切换的顺序;
|
||||
// mu 保护 stopFn、stopped、runtimeCtx、accepting、任务计数和停止完成信号;
|
||||
// 持有 mu 时只做内存操作,不执行 I/O。Stop 先禁止新任务、再取消上下文,最后等待已有任务。
|
||||
|
||||
// accountLifecycle 是账号业务任务生命周期组件。
|
||||
@@ -21,23 +21,30 @@ type accountLifecycle struct {
|
||||
runtimeCtx context.Context
|
||||
// accepting 表示是否仍允许新的消息或自动化任务进入。
|
||||
accepting bool
|
||||
// taskWG 等待所有已经通过 beginTask 的业务任务退出。
|
||||
taskWG sync.WaitGroup
|
||||
// activeTasks 记录尚未完成的任务数量,用于在超时 Stop 后由最后一个任务关闭 stopDone。
|
||||
activeTasks int
|
||||
// stopDone 在第一次 Stop 的清理完成后关闭,保证并发 Stop 调用也等待完整收束。
|
||||
// tasksFinished 表示 stopDone 已关闭,避免重复 finishTask 破坏共享停止信号。
|
||||
tasksFinished bool
|
||||
// stopDone 在 Stop 前已登记的业务任务全部退出后关闭;它由 lifecycle 保存,
|
||||
// 所有等待者直接订阅该信号,不为每次带超时的等待创建无法 Join 的协程。
|
||||
stopDone chan struct{}
|
||||
}
|
||||
|
||||
// start 注册一次 Run 的上下文和取消函数,并重新开放业务任务入口。
|
||||
func (l *accountLifecycle) start(ctx context.Context, cancel context.CancelFunc) {
|
||||
// 若 Stop 已经先于 Run 建立了不可逆的停止 fencing,则返回 false,拒绝迟到的启动。
|
||||
func (l *accountLifecycle) start(ctx context.Context, cancel context.CancelFunc) bool {
|
||||
l.mu.Lock()
|
||||
if l.stopped {
|
||||
l.mu.Unlock()
|
||||
return false
|
||||
}
|
||||
l.stopFn = cancel
|
||||
l.runtimeCtx = ctx
|
||||
l.accepting = true
|
||||
l.stopped = false
|
||||
l.stopDone = nil
|
||||
l.tasksFinished = false
|
||||
l.mu.Unlock()
|
||||
return true
|
||||
}
|
||||
|
||||
// stopContext 原子地禁止新任务并取出取消函数;并发停止调用会受 ctx 限制地等待首次清理完成。
|
||||
@@ -64,7 +71,7 @@ func (l *accountLifecycle) stopContext(ctx context.Context) (context.CancelFunc,
|
||||
l.stopDone = make(chan struct{})
|
||||
if l.activeTasks == 0 {
|
||||
close(l.stopDone)
|
||||
l.stopDone = nil
|
||||
l.tasksFinished = true
|
||||
}
|
||||
// cancel 是当前账号 Run 上下文的取消函数。
|
||||
cancel := l.stopFn
|
||||
@@ -87,36 +94,36 @@ func (l *accountLifecycle) beginTask() (context.Context, bool) {
|
||||
if ctx.Err() != nil {
|
||||
return nil, false
|
||||
}
|
||||
l.taskWG.Add(1)
|
||||
l.activeTasks++
|
||||
return ctx, true
|
||||
}
|
||||
|
||||
// finishTask 标记一个已登记的业务任务退出。
|
||||
func (l *accountLifecycle) finishTask() {
|
||||
l.taskWG.Done()
|
||||
l.mu.Lock()
|
||||
if l.activeTasks > 0 {
|
||||
l.activeTasks--
|
||||
}
|
||||
if l.stopped && l.activeTasks == 0 && l.stopDone != nil {
|
||||
if l.stopped && l.activeTasks == 0 && !l.tasksFinished && l.stopDone != nil {
|
||||
close(l.stopDone)
|
||||
l.stopDone = nil
|
||||
l.tasksFinished = true
|
||||
}
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
// waitContext 等待已登记业务任务结束,并在 ctx 到期时及时返回。
|
||||
// waitContext 等待 Stop 前已登记的业务任务结束,并在 ctx 到期时及时返回。
|
||||
// stopDone 由最后一个任务关闭,因而超时返回不遗留等待协程;调用方可用新的上下文再次等待同一信号。
|
||||
func (l *accountLifecycle) waitContext(ctx context.Context) bool {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
// done 是所有已登记任务完成后关闭的等待信号。
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
l.taskWG.Wait()
|
||||
close(done)
|
||||
}()
|
||||
l.mu.Lock()
|
||||
// done 是 Stop 为当前收束周期创建、由最后一个已登记任务关闭的共享信号。
|
||||
done := l.stopDone
|
||||
l.mu.Unlock()
|
||||
if done == nil {
|
||||
return true
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
return true
|
||||
|
||||
Reference in new issue
Block a user