From 361b0abec9f37b7019991536478fd1dcf9d075d9 Mon Sep 17 00:00:00 2001 From: creamlike1024 Date: Wed, 28 May 2025 21:34:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20pingerCtx=20=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- relay/channel/api_request.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/relay/channel/api_request.go b/relay/channel/api_request.go index 03eff9cf..da8d4e14 100644 --- a/relay/channel/api_request.go +++ b/relay/channel/api_request.go @@ -122,11 +122,13 @@ func doRequest(c *gin.Context, req *http.Request, info *common.RelayInfo) (*http var pingerWg sync.WaitGroup if info.IsStream { helper.SetEventStreamHeaders(c) - pingInterval := time.Duration(generalSettings.PingIntervalSeconds) * time.Second - var pingerCtx context.Context - pingerCtx, stopPinger = context.WithCancel(c.Request.Context()) if pingEnabled { + pingInterval := time.Duration(generalSettings.PingIntervalSeconds) * time.Second + var pingerCtx context.Context + pingerCtx, stopPinger = context.WithCancel(c.Request.Context()) + // 退出时清理 pingerCtx 防止泄露 + defer stopPinger() pingerWg.Add(1) gopool.Go(func() { defer pingerWg.Done() @@ -166,9 +168,8 @@ func doRequest(c *gin.Context, req *http.Request, info *common.RelayInfo) (*http } resp, err := client.Do(req) - // request结束后停止ping + // request结束后等待 ping goroutine 完成 if info.IsStream && pingEnabled { - stopPinger() pingerWg.Wait() } if err != nil {