Skip to content

Commit 218cb35

Browse files
committed
better error handling
1 parent a7ff825 commit 218cb35

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

main.go

+30-15
Original file line numberDiff line numberDiff line change
@@ -259,24 +259,33 @@ func (p *ProxyServer) getClientForHost(hostBackend string, timeout float32) (*ht
259259

260260
// proxyRequest forwards the request to the backend with retries based on the configuration
261261
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+
}
262266

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
265269
backends, found := p.getBackendsForHost(host)
266270

267271
p.totalRequests.WithLabelValues(host).Inc()
268272

269273
if !found || len(backends) == 0 {
270274
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)
272276
return
273277
}
274278

275279
var lastErr error
276280
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+
278287
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))
280289
if err != nil {
281290
klog.V(4).Infof("Message failed for host %s with error: %v", backend.Backend, err)
282291
lastErr = err
@@ -285,26 +294,32 @@ func (p *ProxyServer) proxyRequest(r *ForwardedRequest) {
285294

286295
req.Header = r.Req.Header
287296
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
291303
}
292304

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+
}
293309
resp.Body.Close()
294-
if err == nil && resp.StatusCode < 400 {
295-
// Successfully forwarded request
310+
311+
if resp.StatusCode < 400 {
296312
p.totalForwarded.WithLabelValues(host, backend.Backend).Inc()
313+
klog.V(4).Infof("Request success to %s", backend.Backend)
297314
return
298315
} 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
300319
}
301-
lastErr = err
302-
p.totalRetries.WithLabelValues(host, backend.Backend).Inc()
303-
time.Sleep(time.Duration(backend.Delay) * time.Second) // Small delay before retrying
304320
}
305321
}
306322

307-
// If we get here, all backends failed
308323
p.totalFailed.WithLabelValues(host).Inc()
309324
klog.V(1).Infof("All backends failed for host %s: %v", host, lastErr)
310325
}

0 commit comments

Comments
 (0)