Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions relay/channel/api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func DoApiRequest(a Adaptor, c *gin.Context, info *common.RelayInfo, requestBody
if common2.DebugEnabled {
println("fullRequestURL:", fullRequestURL)
}
req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody)
req, err := http.NewRequestWithContext(c.Request.Context(), c.Request.Method, fullRequestURL, requestBody)
if err != nil {
return nil, fmt.Errorf("new request failed: %w", err)
}
Expand Down Expand Up @@ -326,7 +326,7 @@ func DoFormRequest(a Adaptor, c *gin.Context, info *common.RelayInfo, requestBod
if common2.DebugEnabled {
println("fullRequestURL:", fullRequestURL)
}
req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody)
req, err := http.NewRequestWithContext(c.Request.Context(), c.Request.Method, fullRequestURL, requestBody)
if err != nil {
return nil, fmt.Errorf("new request failed: %w", err)
}
Expand Down Expand Up @@ -382,7 +382,7 @@ func DoWssRequest(a Adaptor, c *gin.Context, info *common.RelayInfo, requestBody
}

func startPingKeepAlive(c *gin.Context, pingInterval time.Duration) context.CancelFunc {
pingerCtx, stopPinger := context.WithCancel(context.Background())
pingerCtx, stopPinger := context.WithCancel(c.Request.Context())

gopool.Go(func() {
defer func() {
Expand Down Expand Up @@ -433,9 +433,6 @@ func startPingKeepAlive(c *gin.Context, pingInterval time.Duration) context.Canc
// 收到退出信号
case <-pingerCtx.Done():
return
// request 结束
case <-c.Request.Context().Done():
return
// 超时保护,防止goroutine无限运行
case <-pingTimeout.C:
if common2.DebugEnabled {
Expand Down Expand Up @@ -517,6 +514,14 @@ func doRequest(c *gin.Context, req *http.Request, info *common.RelayInfo) (*http

resp, err := client.Do(req)
if err != nil {
// 区分客户端主动断开和真正的上游错误
// 客户端断开时不记录错误日志、不重试、不影响渠道状态
if c.Request.Context().Err() != nil {
logger.LogInfo(c, "request cancelled by client: "+err.Error())
return nil, types.NewError(err, types.ErrorCodeDoRequestFailed,
types.ErrOptionWithSkipRetry(),
types.ErrOptionWithHideErrMsg("client disconnected"))
}
logger.LogError(c, "do request failed: "+err.Error())
return nil, types.NewError(err, types.ErrorCodeDoRequestFailed, types.ErrOptionWithHideErrMsg("upstream error: do request failed"))
}
Expand All @@ -534,7 +539,7 @@ func DoTaskApiRequest(a TaskAdaptor, c *gin.Context, info *common.RelayInfo, req
if err != nil {
return nil, err
}
req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody)
req, err := http.NewRequestWithContext(c.Request.Context(), c.Request.Method, fullRequestURL, requestBody)
if err != nil {
return nil, fmt.Errorf("new request failed: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions relay/channel/coze/relay-coze.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func checkIfChatComplete(a *Adaptor, c *gin.Context, info *relaycommon.RelayInfo

requestURL = requestURL + "?conversation_id=" + c.GetString("coze_conversation_id") + "&chat_id=" + c.GetString("coze_chat_id")
// 将 conversationId和chatId作为参数发送get请求
req, err := http.NewRequest("GET", requestURL, nil)
req, err := http.NewRequestWithContext(c.Request.Context(), "GET", requestURL, nil)
if err != nil {
return err, false
}
Expand Down Expand Up @@ -263,7 +263,7 @@ func getChatDetail(a *Adaptor, c *gin.Context, info *relaycommon.RelayInfo) (*ht
requestURL := fmt.Sprintf("%s/v3/chat/message/list", info.ChannelBaseUrl)

requestURL = requestURL + "?conversation_id=" + c.GetString("coze_conversation_id") + "&chat_id=" + c.GetString("coze_chat_id")
req, err := http.NewRequest("GET", requestURL, nil)
req, err := http.NewRequestWithContext(c.Request.Context(), "GET", requestURL, nil)
if err != nil {
return nil, fmt.Errorf("new request failed: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion relay/channel/dify/relay-dify.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func uploadDifyFile(c *gin.Context, info *relaycommon.RelayInfo, user string, me
writer.Close()

// Create HTTP request
req, err := http.NewRequest("POST", uploadUrl, body)
req, err := http.NewRequestWithContext(c.Request.Context(), "POST", uploadUrl, body)
if err != nil {
common.SysLog("failed to create request: " + err.Error())
return nil
Expand Down
2 changes: 1 addition & 1 deletion relay/channel/jimeng/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
if err != nil {
return nil, fmt.Errorf("get request url failed: %w", err)
}
req, err := http.NewRequest(c.Request.Method, fullRequestURL, requestBody)
req, err := http.NewRequestWithContext(c.Request.Context(), c.Request.Method, fullRequestURL, requestBody)
if err != nil {
return nil, fmt.Errorf("new request failed: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion relay/channel/replicate/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func uploadFileFromForm(c *gin.Context, info *relaycommon.RelayInfo, fieldCandid
}
uploadURL := relaycommon.GetFullRequestURL(baseURL, "/v1/files", info.ChannelType)

req, err := http.NewRequest(http.MethodPost, uploadURL, &body)
req, err := http.NewRequestWithContext(c.Request.Context(), http.MethodPost, uploadURL, &body)
if err != nil {
return "", fmt.Errorf("replicate adaptor: create upload request failed: %w", err)
}
Expand Down
11 changes: 3 additions & 8 deletions relay/helper/stream_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func StreamScannerHandler(c *gin.Context, resp *http.Response, info *relaycommon
scanner.Split(bufio.ScanLines)
SetEventStreamHeaders(c)

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(c.Request.Context())
defer cancel()

ctx = context.WithValue(ctx, "stop_chan", stopChan)
Expand Down Expand Up @@ -165,9 +165,6 @@ func StreamScannerHandler(c *gin.Context, resp *http.Response, info *relaycommon
return
case <-stopChan:
return
case <-c.Request.Context().Done():
// 监听客户端断开连接
return
case <-pingTimeout.C:
logger.LogError(c, "ping goroutine max duration reached")
return
Expand Down Expand Up @@ -219,8 +216,6 @@ func StreamScannerHandler(c *gin.Context, resp *http.Response, info *relaycommon
return
case <-ctx.Done():
return
case <-c.Request.Context().Done():
return
default:
}

Expand Down Expand Up @@ -276,8 +271,8 @@ func StreamScannerHandler(c *gin.Context, resp *http.Response, info *relaycommon
case <-stopChan:
// 正常结束
logger.LogInfo(c, "streaming finished")
case <-c.Request.Context().Done():
// 客户端断开连接
case <-ctx.Done():
// 客户端断开连接或 context 被取消
logger.LogInfo(c, "client disconnected")
}
}