推进P1批量发布Engine生命周期与账号任务收口
This commit is contained in:
1 parent
e53177ccd1
commit
417723d2c0
12 files changed
+918
-190
No files matched your search
@@ -660,7 +660,6 @@ func (a *Account) StopContext(ctx context.Context) error {
|
||||
if !shouldStop {
|
||||
return nil
|
||||
}
|
||||
defer a.lifecycle.finishStop()
|
||||
a.setRuntimeState(RuntimeStopped, "账号服务已停止")
|
||||
if cancel != nil {
|
||||
cancel()
|
||||
@@ -675,6 +674,13 @@ func (a *Account) StopContext(ctx context.Context) error {
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
if a.recorder != nil && !a.recorder.waitContext(ctx) {
|
||||
a.logger.Warn("等待账号 WS 记录 worker 退出超时")
|
||||
if ctx == nil || ctx.Err() == nil {
|
||||
return context.DeadlineExceeded
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1140,11 +1146,21 @@ func (a *Account) watchPendingAPIRenew(parent context.Context, result *renew.Res
|
||||
if result == nil || !result.HasPending() {
|
||||
return
|
||||
}
|
||||
// taskCtx 是迟到续期任务继承的账号生命周期上下文;Stop 会先取消它,再等待任务收束。
|
||||
taskCtx, accepted := a.beginTask()
|
||||
if !accepted {
|
||||
return
|
||||
}
|
||||
if parent != nil {
|
||||
// Run 传入的 parent 就是账号运行上下文;直接沿用它可同时响应调用方取消和 Stop 的取消。
|
||||
taskCtx = parent
|
||||
}
|
||||
a.pendingRenewWG.Add(1)
|
||||
go func() {
|
||||
defer a.lifecycle.finishTask()
|
||||
defer a.pendingRenewWG.Done()
|
||||
// ctx、cancel 保存ctx、cancel,供当前处理流程使用
|
||||
ctx, cancel := context.WithTimeout(parent, 35*time.Second)
|
||||
ctx, cancel := context.WithTimeout(taskCtx, 35*time.Second)
|
||||
defer cancel()
|
||||
// late、waitErr 保存late、waitErr,供当前处理流程使用
|
||||
late, waitErr := result.AwaitPending(ctx)
|
||||
|
||||
@@ -502,11 +502,18 @@ func TestTryAPIRenewPendingWatcherStopsWithAccountContext(t *testing.T) {
|
||||
acc.renewer = xrenew.Service{HTTPClient: srv.Client(), SilentHasLoginURL: srv.URL, RetryDelay: -1, PromiseTimeout: 5 * time.Millisecond}
|
||||
// ctx、cancel 保存ctx、cancel,供当前处理流程使用
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
acc.lifecycle.start(ctx, cancel)
|
||||
if acc.tryAPIRenew(ctx) {
|
||||
t.Fatal("Promise 超时不得伪装成同步续期成功")
|
||||
}
|
||||
cancel()
|
||||
acc.pendingRenewWG.Wait()
|
||||
// stopCtx 验证账号停止会等待迟到续期 worker,而不是只取消其父上下文。
|
||||
stopCtx, stopCancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer stopCancel()
|
||||
// stopErr 表示停止账号并等待迟到续期 worker 的结果。
|
||||
if stopErr := acc.StopContext(stopCtx); stopErr != nil {
|
||||
t.Fatalf("StopContext 未等待迟到续期 worker: %v", stopErr)
|
||||
}
|
||||
// detail、err 保存detail、err,供当前处理流程使用
|
||||
detail, err := store.Cookies.GetDetails(context.Background(), "cid")
|
||||
if err != nil {
|
||||
|
||||
@@ -23,6 +23,8 @@ type accountLifecycle struct {
|
||||
accepting bool
|
||||
// taskWG 等待所有已经通过 beginTask 的业务任务退出。
|
||||
taskWG sync.WaitGroup
|
||||
// activeTasks 记录尚未完成的任务数量,用于在超时 Stop 后由最后一个任务关闭 stopDone。
|
||||
activeTasks int
|
||||
// stopDone 在第一次 Stop 的清理完成后关闭,保证并发 Stop 调用也等待完整收束。
|
||||
stopDone chan struct{}
|
||||
}
|
||||
@@ -33,6 +35,8 @@ func (l *accountLifecycle) start(ctx context.Context, cancel context.CancelFunc)
|
||||
l.stopFn = cancel
|
||||
l.runtimeCtx = ctx
|
||||
l.accepting = true
|
||||
l.stopped = false
|
||||
l.stopDone = nil
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
@@ -58,22 +62,16 @@ func (l *accountLifecycle) stopContext(ctx context.Context) (context.CancelFunc,
|
||||
l.stopped = true
|
||||
l.accepting = false
|
||||
l.stopDone = make(chan struct{})
|
||||
if l.activeTasks == 0 {
|
||||
close(l.stopDone)
|
||||
l.stopDone = nil
|
||||
}
|
||||
// cancel 是当前账号 Run 上下文的取消函数。
|
||||
cancel := l.stopFn
|
||||
l.mu.Unlock()
|
||||
return cancel, true, nil
|
||||
}
|
||||
|
||||
// finishStop 标记第一次 Stop 的清理已完成,并唤醒并发 Stop 调用者。
|
||||
func (l *accountLifecycle) finishStop() {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.stopDone != nil {
|
||||
close(l.stopDone)
|
||||
l.stopDone = nil
|
||||
}
|
||||
}
|
||||
|
||||
// beginTask 在生命周期锁内登记业务任务,避免 Stop 与 WaitGroup.Add 竞争。
|
||||
func (l *accountLifecycle) beginTask() (context.Context, bool) {
|
||||
l.mu.Lock()
|
||||
@@ -90,12 +88,22 @@ func (l *accountLifecycle) beginTask() (context.Context, bool) {
|
||||
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 {
|
||||
close(l.stopDone)
|
||||
l.stopDone = nil
|
||||
}
|
||||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
// waitContext 等待已登记业务任务结束,并在 ctx 到期时及时返回。
|
||||
|
||||
@@ -83,5 +83,24 @@ func TestAccountStopContextBoundsTaskWait(t *testing.T) {
|
||||
if elapsed := time.Since(started); elapsed > time.Second {
|
||||
t.Fatalf("StopContext waited too long: %s", elapsed)
|
||||
}
|
||||
// retryDone 表示第一次超时后的第二次 Stop 是否仍等待原任务收束。
|
||||
retryDone := make(chan error, 1)
|
||||
go func() {
|
||||
retryDone <- account.StopContext(context.Background())
|
||||
}()
|
||||
select {
|
||||
case <-retryDone:
|
||||
t.Fatal("第一次 Stop 超时后,重试不应在任务完成前返回")
|
||||
case <-time.After(20 * time.Millisecond):
|
||||
}
|
||||
account.lifecycle.finishTask()
|
||||
select {
|
||||
// retryErr 表示第一次超时后再次停止账号的等待结果。
|
||||
case retryErr := <-retryDone:
|
||||
if retryErr != nil {
|
||||
t.Fatalf("重试 StopContext error=%v", retryErr)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("第一次 Stop 超时后,重试未在任务完成后返回")
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
@@ -23,6 +24,10 @@ type wsRecorder struct {
|
||||
once sync.Once
|
||||
// wg 等待 recorder worker 退出。
|
||||
wg sync.WaitGroup
|
||||
// done 在 recorder worker 退出时关闭,供 Context-aware 等待直接选择而不创建旁路 goroutine。
|
||||
done chan struct{}
|
||||
// started 表示 recorder worker 是否实际启动;未启动的 recorder 无需等待。
|
||||
started atomic.Bool
|
||||
// queue 是有界的报文内存队列,满时丢弃诊断记录而不阻塞 WS。
|
||||
queue chan db.WSMessage
|
||||
}
|
||||
@@ -40,6 +45,7 @@ func newWSRecorder(store *db.Store, cookieID string, logger *slog.Logger) *wsRec
|
||||
cookieID: cookieID,
|
||||
logger: logger,
|
||||
queue: make(chan db.WSMessage, 256),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,9 +79,11 @@ func (r *wsRecorder) start(ctx context.Context) {
|
||||
return
|
||||
}
|
||||
r.once.Do(func() {
|
||||
r.started.Store(true)
|
||||
r.wg.Add(1)
|
||||
go func() {
|
||||
defer r.wg.Done()
|
||||
defer close(r.done)
|
||||
// cleanupCtx 是清理历史报文时的有限时长上下文。
|
||||
cleanupCtx, cleanupCancel := context.WithTimeout(ctx, WSRecordWriteTimeout)
|
||||
// deleted 是本次清理删除的历史报文数量。
|
||||
@@ -131,3 +139,27 @@ func (r *wsRecorder) wait() {
|
||||
r.wg.Wait()
|
||||
}
|
||||
}
|
||||
|
||||
// waitContext 在 ctx 约束内等待 recorder worker 退出;数据库写入异常阻塞时及时把停止超时交给调用方。
|
||||
func (r *wsRecorder) waitContext(ctx context.Context) bool {
|
||||
if r == nil {
|
||||
return true
|
||||
}
|
||||
if !r.started.Load() {
|
||||
return true
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
// done 表示 recorder worker 已经退出,供当前等待调用选择上下文超时或正常完成。
|
||||
done := r.done
|
||||
if done == nil {
|
||||
return true
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
return true
|
||||
case <-ctx.Done():
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user