收口生命周期停止上下文边界
This commit is contained in:
1 parent
e953464df9
commit
12a79d4f62
8 files changed
+201
-38
No files matched your search
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"testing"
|
||||
@@ -176,3 +177,36 @@ func TestServerStartStopIsIdempotentAndWaitsForWorkers(t *testing.T) {
|
||||
t.Fatalf("重复 Stop: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestServerStopContextBoundsWorkerWait 验证关闭上下文到期时不会无限等待后台 worker。
|
||||
func TestServerStopContextBoundsWorkerWait(t *testing.T) {
|
||||
// srv 是用于验证关闭超时的 HTTP 服务;cleanup 负责释放测试数据库。
|
||||
srv, _, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
srv.Addr = freeAddr(t)
|
||||
// err 表示 HTTP 服务启动失败。
|
||||
if err := srv.Start(context.Background()); err != nil {
|
||||
t.Fatalf("Start: %v", err)
|
||||
}
|
||||
// workerDone 保持 worker 未完成,模拟不响应关闭的后台任务。
|
||||
workerDone := srv.beginWorker()
|
||||
// stopCtx 是刻意很短的关闭上下文,用于验证 Stop 的等待边界。
|
||||
stopCtx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
||||
defer cancel()
|
||||
// started 记录停止开始时间,用于验证上下文确实限制等待时长。
|
||||
started := time.Now()
|
||||
// err 表示 worker 未完成时停止上下文到期返回的错误。
|
||||
err := srv.Stop(stopCtx)
|
||||
if err == nil || !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("Stop error=%v, want deadline exceeded", err)
|
||||
}
|
||||
// elapsed 表示停止调用实际耗时。
|
||||
if elapsed := time.Since(started); elapsed > time.Second {
|
||||
t.Fatalf("Stop waited too long: %s", elapsed)
|
||||
}
|
||||
workerDone()
|
||||
// err 表示释放 worker 后第二次幂等停止的返回错误。
|
||||
if err := srv.Stop(context.Background()); err != nil {
|
||||
t.Fatalf("second Stop: %v", err)
|
||||
}
|
||||
}
|
||||
+49
-16
@@ -545,11 +545,12 @@ func (s *Server) Stop(ctx context.Context) error {
|
||||
// httpDone 是已进入停止流程的 HTTP 监听完成信号。
|
||||
httpDone := s.httpDone
|
||||
s.lifecycleMu.Unlock()
|
||||
if httpDone != nil {
|
||||
<-httpDone
|
||||
if httpDone != nil && !waitForSignal(ctx, httpDone) {
|
||||
return ctx.Err()
|
||||
}
|
||||
if !waitForWaitGroup(ctx, &s.backgroundWG) || !s.waitForWorkersContext(ctx) {
|
||||
return ctx.Err()
|
||||
}
|
||||
s.backgroundWG.Wait()
|
||||
s.waitForWorkers(0)
|
||||
return nil
|
||||
}
|
||||
s.stopped = true
|
||||
@@ -572,11 +573,12 @@ func (s *Server) Stop(ctx context.Context) error {
|
||||
if httpServer != nil {
|
||||
shutdownErr = httpServer.Shutdown(ctx)
|
||||
}
|
||||
if httpDone != nil {
|
||||
<-httpDone
|
||||
if httpDone != nil && !waitForSignal(ctx, httpDone) {
|
||||
return ctx.Err()
|
||||
}
|
||||
if !waitForWaitGroup(ctx, &s.backgroundWG) || !s.waitForWorkersContext(ctx) {
|
||||
return ctx.Err()
|
||||
}
|
||||
s.backgroundWG.Wait()
|
||||
s.waitForWorkers(0)
|
||||
return shutdownErr
|
||||
}
|
||||
|
||||
@@ -651,20 +653,51 @@ func (s *Server) beginWorker() func() {
|
||||
|
||||
// waitForWorkers 负责waitForWorkers相关处理。
|
||||
func (s *Server) waitForWorkers(timeout time.Duration) {
|
||||
if timeout <= 0 {
|
||||
_ = s.waitForWorkersContext(context.Background())
|
||||
return
|
||||
}
|
||||
// ctx、cancel 分别表示有限等待上下文和释放定时器的函数。
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
_ = s.waitForWorkersContext(ctx)
|
||||
}
|
||||
|
||||
// waitForWorkersContext 等待批量 worker,并将等待限制在调用方的关闭上下文内。
|
||||
func (s *Server) waitForWorkersContext(ctx context.Context) bool {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
s.workerMu.Lock()
|
||||
// done 保存done,供当前处理流程使用
|
||||
done := s.workersDone
|
||||
s.workerMu.Unlock()
|
||||
if timeout <= 0 {
|
||||
<-done
|
||||
return
|
||||
}
|
||||
// timer 保存定时器,供当前处理流程使用
|
||||
timer := time.NewTimer(timeout)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-done:
|
||||
case <-timer.C:
|
||||
return true
|
||||
case <-ctx.Done():
|
||||
s.Logger.Warn("等待后台 worker 退出超时")
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// waitForSignal 在关闭上下文取消或目标信号到达时返回,避免无界阻塞。
|
||||
func waitForSignal(ctx context.Context, signal <-chan struct{}) bool {
|
||||
select {
|
||||
case <-signal:
|
||||
return true
|
||||
case <-ctx.Done():
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// waitForWaitGroup 在关闭上下文取消或 WaitGroup 清空时返回,避免无界阻塞。
|
||||
func waitForWaitGroup(ctx context.Context, group *sync.WaitGroup) bool {
|
||||
// done 是 WaitGroup 清空后的完成信号。
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
group.Wait()
|
||||
close(done)
|
||||
}()
|
||||
return waitForSignal(ctx, done)
|
||||
}
|
||||
Reference in new issue
Block a user