feat(app): 更新优雅停机逻辑与请求超时设置
- 修改 DrainTimeout 和 ShutdownTimeout 的注释,明确其含义与默认值 - 优化日志信息,提供更清晰的停机状态反馈 - 调整 ShutdownTimeout 默认值为 10s,以增强请求处理的灵活性 - 移除冗余的调试日志代码,提升代码整洁性
This commit is contained in:
+16
-16
@@ -42,8 +42,8 @@ type Application struct {
|
||||
shuttingDown atomic.Bool // 停机标志,置为 true 后新请求立即返回 503
|
||||
inFlight atomic.Int64 // 当前正在处理的请求数(不含已返回 503 的)
|
||||
shutdownOnce sync.Once // 保证多路触发(信号/关窗口)只执行一次
|
||||
DrainTimeout time.Duration // 返回 503 后等 nginx 切流的最长时长,默认 5s(每 1s 检测在途请求,为 0 则立即进入 Shutdown)
|
||||
ShutdownTimeout time.Duration // 等在途请求完成的最长时间,默认 30s
|
||||
DrainTimeout time.Duration // 已停止接收新请求后、等老请求自然结束的最长时长,默认 5s(每 1s 检测在途请求,为 0 则立即进入 Shutdown)
|
||||
ShutdownTimeout time.Duration // drain 超时后再给老请求的最长容忍时间,默认 10s(到期后强制关闭)
|
||||
}
|
||||
|
||||
func (that *Application) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
@@ -58,36 +58,36 @@ func (that *Application) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
// initiateGracefulShutdown 触发优雅停机,通过 sync.Once 保证多路触发只执行一次。
|
||||
// drain: 返回 503 后等待上游(nginx)完成流量切换的最长时长,每 1s 检测一次在途请求数,
|
||||
// 若为 0 则立即进入 Shutdown,避免无谓等待。
|
||||
// shutdown: 等待在途请求完成的最长时间。
|
||||
// drain: 已停止接收新请求后、等老请求自然结束的最长时长,每 1s 检测一次在途请求数,
|
||||
// 为 0 则立即进入 Shutdown,避免无谓等待。
|
||||
// shutdown: drain 超时后再给老请求的最长容忍时间,到期强制关闭。
|
||||
func (that *Application) initiateGracefulShutdown(drain, shutdown time.Duration) {
|
||||
that.shutdownOnce.Do(func() {
|
||||
that.shuttingDown.Store(true)
|
||||
that.Log.Infof("优雅停机:新请求已返回 503,最多等 %v 让 nginx 完成流量切换(每秒检测在途请求)...", drain)
|
||||
that.Log.Infof("优雅停机:已停止接收新请求(后续请求一律返回 503),等待老请求自然结束,最多再等 %v...", drain)
|
||||
|
||||
// 每秒检测一次,若无在途请求立即停机,否则最多等满 drain
|
||||
tick := time.NewTicker(1 * time.Second)
|
||||
defer tick.Stop()
|
||||
deadline := time.Now().Add(drain)
|
||||
for {
|
||||
if n := that.inFlight.Load(); n == 0 {
|
||||
that.Log.Infof("无在途请求,立即进入 Shutdown")
|
||||
n := that.inFlight.Load()
|
||||
if n == 0 {
|
||||
that.Log.Infof("已无在途请求,立即停机")
|
||||
break
|
||||
} else if time.Now().After(deadline) {
|
||||
that.Log.Infof("drain 等待到达上限 %v,仍有 %d 个在途请求,强制进入 Shutdown", drain, n)
|
||||
break
|
||||
} else {
|
||||
that.Log.Infof("drain 等待中,当前在途请求数=%d", n)
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
that.Log.Infof("已停止接收新请求,仍有 %d 个老请求未结束,将在最多 %v 后强制结束这些请求", n, shutdown)
|
||||
break
|
||||
}
|
||||
that.Log.Infof("已停止接收新请求,当前仍有 %d 个老请求在执行,继续等待...", n)
|
||||
<-tick.C
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), shutdown)
|
||||
defer cancel()
|
||||
that.Log.Infof("停止接受新连接,等进行中请求完成(最多 %v)...", shutdown)
|
||||
if err := that.Server.Shutdown(ctx); err != nil {
|
||||
that.Log.Errorf("优雅停机超时,强制关闭: %v", err)
|
||||
that.Log.Errorf("等待超时,强制关闭仍在执行的 %d 个请求: %v", that.inFlight.Load(), err)
|
||||
} else {
|
||||
that.Log.Infof("服务已安全关闭")
|
||||
}
|
||||
@@ -247,7 +247,7 @@ func (that *Application) Run(router Router) {
|
||||
}
|
||||
shutdown := that.ShutdownTimeout
|
||||
if shutdown == 0 {
|
||||
shutdown = 30 * time.Second
|
||||
shutdown = 10 * time.Second
|
||||
}
|
||||
that.initiateGracefulShutdown(drain, shutdown)
|
||||
}()
|
||||
|
||||
@@ -275,15 +275,6 @@ func (that *TestApp) GenerateSwagger(title, version, outputDir string) error {
|
||||
}
|
||||
isPartialRun := visitedPaths > 0 && visitedPaths < totalPaths
|
||||
|
||||
// #region agent log
|
||||
func() {
|
||||
if f, err := os.OpenFile("debug-e1b6ef.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
|
||||
fmt.Fprintf(f, `{"sessionId":"e1b6ef","hypothesisId":"D","message":"partial_run_check","data":{"proj":"%s","totalPaths":%d,"visitedPaths":%d,"isPartialRun":%v}}`+"\n", projName, totalPaths, visitedPaths, isPartialRun)
|
||||
f.Close()
|
||||
}
|
||||
}()
|
||||
// #endregion
|
||||
|
||||
// 仅部分运行时才加载已有 spec 用于合并,全量运行时清空重建
|
||||
var existingEndpoints map[string]map[string]interface{}
|
||||
if isPartialRun {
|
||||
@@ -311,40 +302,10 @@ func (that *TestApp) GenerateSwagger(title, version, outputDir string) error {
|
||||
// 部分运行且该路径未被访问 → 保留已有 spec 中的数据
|
||||
if isPartialRun && !visited[apiPath] {
|
||||
if existing, ok := existingEndpoints[apiPath]; ok {
|
||||
// #region agent log
|
||||
if strings.Contains(apiPath, "batch") {
|
||||
func() {
|
||||
if f, err := os.OpenFile("debug-e1b6ef.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
|
||||
fmt.Fprintf(f, `{"sessionId":"e1b6ef","hypothesisId":"D","message":"kept_existing","data":{"path":"%s"}}`+"\n", apiPath)
|
||||
f.Close()
|
||||
}
|
||||
}()
|
||||
}
|
||||
// #endregion
|
||||
endpoints = append(endpoints, existing)
|
||||
continue
|
||||
}
|
||||
// #region agent log
|
||||
if strings.Contains(apiPath, "batch") {
|
||||
func() {
|
||||
if f, err := os.OpenFile("debug-e1b6ef.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
|
||||
fmt.Fprintf(f, `{"sessionId":"e1b6ef","hypothesisId":"D","message":"NOT_in_existing_file","data":{"path":"%s"}}`+"\n", apiPath)
|
||||
f.Close()
|
||||
}
|
||||
}()
|
||||
}
|
||||
// #endregion
|
||||
}
|
||||
// #region agent log
|
||||
if strings.Contains(apiPath, "batch") && isPartialRun && visited[apiPath] {
|
||||
func() {
|
||||
if f, err := os.OpenFile("debug-e1b6ef.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
|
||||
fmt.Fprintf(f, `{"sessionId":"e1b6ef","hypothesisId":"D","message":"regenerated","data":{"path":"%s","numRecs":%d}}`+"\n", apiPath, len(recs))
|
||||
f.Close()
|
||||
}
|
||||
}()
|
||||
}
|
||||
// #endregion
|
||||
|
||||
httpMethod := "POST"
|
||||
if len(recs) > 0 && recs[0].Method != "" {
|
||||
|
||||
Reference in New Issue
Block a user