收口生命周期停止上下文边界
This commit is contained in:
1 parent
e953464df9
commit
12a79d4f62
8 files changed
+201
-38
No files matched your search
@@ -644,12 +644,20 @@ func (a *Account) clearConnectionToken(ctx context.Context) {
|
||||
a.clearTokenCache(ctx)
|
||||
}
|
||||
|
||||
// Stop 优雅停止。
|
||||
// Stop 优雅停止并兼容不需要错误返回值的旧调用方。
|
||||
func (a *Account) Stop() {
|
||||
_ = a.StopContext(context.Background())
|
||||
}
|
||||
|
||||
// StopContext 在 ctx 约束内停止账号,并等待已登记任务完成。
|
||||
func (a *Account) StopContext(ctx context.Context) error {
|
||||
// cancel 是当前 Run 上下文的取消函数;shouldStop 表示当前调用是否负责首次清理。
|
||||
cancel, shouldStop := a.lifecycle.stop()
|
||||
cancel, shouldStop, err := a.lifecycle.stopContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !shouldStop {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
defer a.lifecycle.finishStop()
|
||||
a.setRuntimeState(RuntimeStopped, "账号服务已停止")
|
||||
@@ -659,9 +667,14 @@ func (a *Account) Stop() {
|
||||
// 取消所有防抖定时器;回调任务仍由 lifecycle 统一等待。
|
||||
a.stop()
|
||||
|
||||
if !a.lifecycle.wait(10 * time.Second) {
|
||||
if !a.lifecycle.waitContext(ctx) {
|
||||
a.logger.Warn("等待账号业务任务退出超时")
|
||||
if ctx == nil || ctx.Err() == nil {
|
||||
return context.DeadlineExceeded
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// beginTask 负责begin任务相关处理。
|
||||
|
||||
@@ -39,15 +39,29 @@ func (l *accountLifecycle) start(ctx context.Context, cancel context.CancelFunc)
|
||||
|
||||
// stop 原子地禁止新任务并取出运行上下文取消函数。
|
||||
func (l *accountLifecycle) stop() (context.CancelFunc, bool) {
|
||||
// cancel、first 分别表示运行上下文取消函数和是否由本次调用负责首次停止。
|
||||
cancel, first, _ := l.stopContext(context.Background())
|
||||
return cancel, first
|
||||
}
|
||||
|
||||
// stopContext 原子地禁止新任务并取出取消函数;并发停止调用会受 ctx 限制地等待首次清理完成。
|
||||
func (l *accountLifecycle) stopContext(ctx context.Context) (context.CancelFunc, bool, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
l.mu.Lock()
|
||||
if l.stopped {
|
||||
// done 是第一次 Stop 完成全部清理后关闭的信号。
|
||||
done := l.stopDone
|
||||
l.mu.Unlock()
|
||||
if done != nil {
|
||||
<-done
|
||||
select {
|
||||
case <-done:
|
||||
case <-ctx.Done():
|
||||
return nil, false, ctx.Err()
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
return nil, false, nil
|
||||
}
|
||||
l.stopped = true
|
||||
l.accepting = false
|
||||
@@ -55,7 +69,7 @@ func (l *accountLifecycle) stop() (context.CancelFunc, bool) {
|
||||
// cancel 是当前账号 Run 上下文的取消函数。
|
||||
cancel := l.stopFn
|
||||
l.mu.Unlock()
|
||||
return cancel, true
|
||||
return cancel, true, nil
|
||||
}
|
||||
|
||||
// finishStop 标记第一次 Stop 的清理已完成,并唤醒并发 Stop 调用者。
|
||||
@@ -94,23 +108,30 @@ func (l *accountLifecycle) finishTask() {
|
||||
|
||||
// wait 等待所有已登记业务任务结束;timeout 小于等于零表示无限等待。
|
||||
func (l *accountLifecycle) wait(timeout time.Duration) bool {
|
||||
if timeout <= 0 {
|
||||
return l.waitContext(context.Background())
|
||||
}
|
||||
// ctx、cancel 分别表示有限等待上下文和释放定时器的函数。
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
return l.waitContext(ctx)
|
||||
}
|
||||
|
||||
// waitContext 等待已登记业务任务结束,并在 ctx 到期时及时返回。
|
||||
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)
|
||||
}()
|
||||
if timeout <= 0 {
|
||||
<-done
|
||||
return true
|
||||
}
|
||||
// timer 是有限等待模式使用的超时定时器。
|
||||
timer := time.NewTimer(timeout)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-done:
|
||||
return true
|
||||
case <-timer.C:
|
||||
case <-ctx.Done():
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -55,3 +56,32 @@ func TestAccountStopWaitsForTaskAndConcurrentStop(t *testing.T) {
|
||||
t.Fatal("Stop 后不应再接受业务任务")
|
||||
}
|
||||
}
|
||||
|
||||
// TestAccountStopContextBoundsTaskWait 验证停止上下文到期时不会无限等待业务任务。
|
||||
func TestAccountStopContextBoundsTaskWait(t *testing.T) {
|
||||
// account 是用于验证停止超时的测试账号 facade。
|
||||
account := New(Config{CookieID: "lifecycle-timeout", CookieStr: "unb=1"})
|
||||
// runCtx 是测试账号运行上下文;cancel 负责释放运行上下文。
|
||||
runCtx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
account.lifecycle.start(runCtx, cancel)
|
||||
// ok 表示测试业务任务是否成功登记。
|
||||
if _, ok := account.beginTask(); !ok {
|
||||
t.Fatal("业务任务应成功登记")
|
||||
}
|
||||
// stopCtx 是刻意很短的停止上下文,用于验证有界等待。
|
||||
stopCtx, stopCancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
||||
defer stopCancel()
|
||||
// started 记录停止开始时间,用于验证上下文确实限制等待时长。
|
||||
started := time.Now()
|
||||
// err 表示停止上下文到期后的返回错误。
|
||||
err := account.StopContext(stopCtx)
|
||||
if err == nil || !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("StopContext error=%v, want deadline exceeded", err)
|
||||
}
|
||||
// elapsed 表示停止调用实际耗时。
|
||||
if elapsed := time.Since(started); elapsed > time.Second {
|
||||
t.Fatalf("StopContext waited too long: %s", elapsed)
|
||||
}
|
||||
account.lifecycle.finishTask()
|
||||
}
|
||||
Reference in new issue
Block a user