MCPcopy Create free account
hub / github.com/aiprodcoder/MIXAPI / doRequest

Function doRequest

relay/channel/api_request.go:209–253  ·  view source on GitHub ↗
(c *gin.Context, req *http.Request, info *common.RelayInfo)

Source from the content-addressed store, hash-verified

207 return doRequest(c, req, info)
208}
209func doRequest(c *gin.Context, req *http.Request, info *common.RelayInfo) (*http.Response, error) {
210 var client *http.Client
211 var err error
212 if info.ChannelSetting.Proxy != "" {
213 client, err = service.NewProxyHttpClient(info.ChannelSetting.Proxy)
214 if err != nil {
215 return nil, fmt.Errorf("new proxy http client failed: %w", err)
216 }
217 } else {
218 client = service.GetHttpClient()
219 }
220
221 var stopPinger context.CancelFunc
222 if info.IsStream {
223 helper.SetEventStreamHeaders(c)
224 // 处理流式请求的 ping 保活
225 generalSettings := operation_setting.GetGeneralSetting()
226 if generalSettings.PingIntervalEnabled {
227 pingInterval := time.Duration(generalSettings.PingIntervalSeconds) * time.Second
228 stopPinger = startPingKeepAlive(c, pingInterval)
229 // 使用defer确保在任何情况下都能停止ping goroutine
230 defer func() {
231 if stopPinger != nil {
232 stopPinger()
233 if common2.DebugEnabled {
234 println("SSE ping goroutine stopped by defer")
235 }
236 }
237 }()
238 }
239 }
240
241 resp, err := client.Do(req)
242
243 if err != nil {
244 return nil, err
245 }
246 if resp == nil {
247 return nil, errors.New("resp is nil")
248 }
249
250 _ = req.Body.Close()
251 _ = c.Request.Body.Close()
252 return resp, nil
253}
254
255func DoTaskApiRequest(a TaskAdaptor, c *gin.Context, info *common.TaskRelayInfo, requestBody io.Reader) (*http.Response, error) {
256 fullRequestURL, err := a.BuildRequestURL(info)

Callers 4

DoApiRequestFunction · 0.70
DoFormRequestFunction · 0.70
DoRequestFunction · 0.70
DoTaskApiRequestFunction · 0.70

Calls 5

NewProxyHttpClientFunction · 0.92
GetHttpClientFunction · 0.92
SetEventStreamHeadersFunction · 0.92
GetGeneralSettingFunction · 0.92
startPingKeepAliveFunction · 0.85

Tested by

no test coverage detected