@@ -259,24 +259,33 @@ func (p *ProxyServer) getClientForHost(hostBackend string, timeout float32) (*ht
259
259
260
260
// proxyRequest forwards the request to the backend with retries based on the configuration
261
261
func (p * ProxyServer ) proxyRequest (r * ForwardedRequest ) {
262
+ if r == nil || r .Req == nil {
263
+ klog .Error ("Received a nil request, skipping processing" )
264
+ return
265
+ }
262
266
263
- host := r .Req .Host // The front-facing host
264
- host , _ , _ = strings .Cut (host , ":" ) //strip port
267
+ host := r .Req .Host
268
+ host , _ , _ = strings .Cut (host , ":" ) // strip port
265
269
backends , found := p .getBackendsForHost (host )
266
270
267
271
p .totalRequests .WithLabelValues (host ).Inc ()
268
272
269
273
if ! found || len (backends ) == 0 {
270
274
p .totalFailed .WithLabelValues (host ).Inc () // Increment failed request counter
271
- klog .V (1 ).Infof ("Error host: '%v' not found in config file, droping request" , host )
275
+ klog .V (1 ).Infof ("Error host: '%v' not found in config file, dropping request" , host )
272
276
return
273
277
}
274
278
275
279
var lastErr error
276
280
for _ , backend := range backends {
277
- client := p .getClientForHost (host + backend .Backend , backend .Timeout )
281
+ client := p .getClientForHost (host + backend .Backend , backend .Timeout )
282
+ if client == nil {
283
+ klog .V (1 ).Infof ("Client for backend %s is nil, skipping" , backend .Backend )
284
+ continue
285
+ }
286
+
278
287
for i := 0 ; i <= backend .Retries ; i ++ {
279
- req , err := http .NewRequest (r .Req .Method , backend .Backend + r .Req .URL .Path , bytes .NewReader (r .Body ));
288
+ req , err := http .NewRequest (r .Req .Method , backend .Backend + r .Req .URL .Path , bytes .NewReader (r .Body ))
280
289
if err != nil {
281
290
klog .V (4 ).Infof ("Message failed for host %s with error: %v" , backend .Backend , err )
282
291
lastErr = err
@@ -285,26 +294,32 @@ func (p *ProxyServer) proxyRequest(r *ForwardedRequest) {
285
294
286
295
req .Header = r .Req .Header
287
296
resp , err := client .Do (req )
288
- // Read the response body to ensure the connection can be reused
289
- if _ , err := io .Copy (io .Discard , resp .Body ); err != nil {
290
- klog .V (1 ).Infof ("Failed to read %v response body: %v" , backend .Backend + r .Req .URL .Path , err )
297
+ if err != nil {
298
+ klog .V (1 ).Infof ("Failed to process request to %s with error: %v" , backend .Backend , err )
299
+ lastErr = err
300
+ p .totalRetries .WithLabelValues (host , backend .Backend ).Inc ()
301
+ time .Sleep (time .Duration (backend .Delay ) * time .Second )
302
+ continue
291
303
}
292
304
305
+ // Read and discard response body
306
+ if _ , err := io .Copy (io .Discard , resp .Body ); err != nil {
307
+ klog .V (1 ).Infof ("Failed to read response body from %v: %v" , backend .Backend + r .Req .URL .Path , err )
308
+ }
293
309
resp .Body .Close ()
294
- if err == nil && resp . StatusCode < 400 {
295
- // Successfully forwarded request
310
+
311
+ if resp . StatusCode < 400 {
296
312
p .totalForwarded .WithLabelValues (host , backend .Backend ).Inc ()
313
+ klog .V (4 ).Infof ("Request success to %s" , backend .Backend )
297
314
return
298
315
} else {
299
- klog .V (4 ).Infof ("Message failed for host %s with resp code %v error: %v" , backend .Backend , resp .StatusCode , err )
316
+ klog .V (4 ).Infof ("Request to %s failed on not acceptable status code %v" , backend .Backend , resp .StatusCode )
317
+ time .Sleep (time .Duration (backend .Delay ) * time .Second )
318
+ continue
300
319
}
301
- lastErr = err
302
- p .totalRetries .WithLabelValues (host , backend .Backend ).Inc ()
303
- time .Sleep (time .Duration (backend .Delay ) * time .Second ) // Small delay before retrying
304
320
}
305
321
}
306
322
307
- // If we get here, all backends failed
308
323
p .totalFailed .WithLabelValues (host ).Inc ()
309
324
klog .V (1 ).Infof ("All backends failed for host %s: %v" , host , lastErr )
310
325
}
0 commit comments