MCPcopy Index your code
hub / github.com/tbphp/gpt-load / executeRequestWithRetry

Method executeRequestWithRetry

internal/proxy/server.go:117–291  ·  view source on GitHub ↗

executeRequestWithRetry is the core recursive function for handling requests and retries.

(
	c *gin.Context,
	channelHandler channel.ChannelProxy,
	originalGroup *models.Group,
	group *models.Group,
	bodyBytes []byte,
	isStream bool,
	startTime time.Time,
	retryCount int,
)

Source from the content-addressed store, hash-verified

115
116// executeRequestWithRetry is the core recursive function for handling requests and retries.
117func (ps *ProxyServer) executeRequestWithRetry(
118 c *gin.Context,
119 channelHandler channel.ChannelProxy,
120 originalGroup *models.Group,
121 group *models.Group,
122 bodyBytes []byte,
123 isStream bool,
124 startTime time.Time,
125 retryCount int,
126) {
127 cfg := group.EffectiveConfig
128
129 apiKey, err := ps.keyProvider.SelectKey(group.ID)
130 if err != nil {
131 logrus.Errorf("Failed to select a key for group %s on attempt %d: %v", group.Name, retryCount+1, err)
132 response.Error(c, app_errors.NewAPIError(app_errors.ErrNoKeysAvailable, err.Error()))
133 ps.logRequest(c, originalGroup, group, nil, startTime, http.StatusServiceUnavailable, err, isStream, "", channelHandler, bodyBytes, models.RequestTypeFinal)
134 return
135 }
136
137 upstreamURL, err := channelHandler.BuildUpstreamURL(c.Request.URL, originalGroup.Name)
138 if err != nil {
139 response.Error(c, app_errors.NewAPIError(app_errors.ErrInternalServer, fmt.Sprintf("Failed to build upstream URL: %v", err)))
140 return
141 }
142
143 var ctx context.Context
144 var cancel context.CancelFunc
145 if isStream {
146 ctx, cancel = context.WithCancel(c.Request.Context())
147 } else {
148 timeout := time.Duration(cfg.RequestTimeout) * time.Second
149 ctx, cancel = context.WithTimeout(c.Request.Context(), timeout)
150 }
151 defer cancel()
152
153 req, err := http.NewRequestWithContext(ctx, c.Request.Method, upstreamURL, bytes.NewReader(bodyBytes))
154 if err != nil {
155 logrus.Errorf("Failed to create upstream request: %v", err)
156 response.Error(c, app_errors.ErrInternalServer)
157 return
158 }
159 req.ContentLength = int64(len(bodyBytes))
160
161 req.Header = c.Request.Header.Clone()
162
163 // Clean up client auth key
164 req.Header.Del("Authorization")
165 req.Header.Del("X-Api-Key")
166 req.Header.Del("X-Goog-Api-Key")
167
168 // Apply model redirection
169 finalBodyBytes, err := channelHandler.ApplyModelRedirect(req, bodyBytes, group)
170 if err != nil {
171 response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, err.Error()))
172 ps.logRequest(c, originalGroup, group, apiKey, startTime, http.StatusBadRequest, err, isStream, upstreamURL, channelHandler, bodyBytes, models.RequestTypeFinal)
173 return
174 }

Callers 1

HandleProxyMethod · 0.95

Calls 15

logRequestMethod · 0.95
handleNormalResponseMethod · 0.95
ErrorFunction · 0.92
ApplyHeaderRulesFunction · 0.92
MaskAPIKeyFunction · 0.92
RedactSecretFunction · 0.92
handleGzipCompressionFunction · 0.85
shouldInterceptModelListFunction · 0.85

Tested by

no test coverage detected