perf: speed up graceful shutdown
移除关闭时等待信号量释放的逻辑,直接标记任务为暂停状态。 超时时间从 10 秒减少到 3 秒,加快进程退出速度。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+3
-14
@@ -85,22 +85,11 @@ func mustRunServer() {
|
|||||||
|
|
||||||
// gracefulShutdown 优雅退出:保存任务状态并关闭服务
|
// gracefulShutdown 优雅退出:保存任务状态并关闭服务
|
||||||
func gracefulShutdown() {
|
func gracefulShutdown() {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// 等待活跃任务完成或超时
|
// 标记所有运行中的任务为暂停状态(立即中断,不等待)
|
||||||
task.GlobalTaskMux.Lock()
|
task.MarkAllTasksPaused()
|
||||||
activeCount := len(task.GlobalTaskList)
|
|
||||||
task.GlobalTaskMux.Unlock()
|
|
||||||
|
|
||||||
if activeCount > 0 {
|
|
||||||
logger.Infof("等待 %d 个活跃任务完成(最多10秒)...", activeCount)
|
|
||||||
// 标记所有运行中的任务为暂停状态
|
|
||||||
task.MarkAllTasksPaused()
|
|
||||||
// 等待下载信号量释放
|
|
||||||
task.GlobalDownloadSem.Wait()
|
|
||||||
task.GlobalMergeSem.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关闭 HTTP 服务器
|
// 关闭 HTTP 服务器
|
||||||
if err := server.Shutdown(ctx); err != nil {
|
if err := server.Shutdown(ctx); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user