From c7c9706d7cf094e827b411f6d7b26eb4f6c14e8a Mon Sep 17 00:00:00 2001 From: Bram Schuur Date: Tue, 20 Aug 2024 16:45:15 +0200 Subject: [PATCH] STAC-21470: Forward request even on panic --- internal/mirror/reflector.go | 2 +- internal/mirror/sendqueue.go | 6 +++--- internal/proxy/handler.go | 12 ++++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/internal/mirror/reflector.go b/internal/mirror/reflector.go index 68e854a..832f49d 100644 --- a/internal/mirror/reflector.go +++ b/internal/mirror/reflector.go @@ -68,7 +68,7 @@ func (r *Reflector) sendToMirrors(req *Request) { defer r.RUnlock() for _, mirror := range r.mirrors { - go mirror.Reflect(req) + mirror.Reflect(req) } } diff --git a/internal/mirror/sendqueue.go b/internal/mirror/sendqueue.go index 2935747..4f00ab3 100644 --- a/internal/mirror/sendqueue.go +++ b/internal/mirror/sendqueue.go @@ -66,12 +66,12 @@ func (s *SendQueue) AddToQueue(req *Request, targetURL string) { } func (s *SendQueue) NextExecuteItems() []*Request { - var result []*Request - var newQueue []*Request - s.Lock() defer s.Unlock() + var result []*Request + var newQueue []*Request = make([]*Request, 0, len(s.requestsQueued)) + // Try to find the next request that can execute, assumes for _, request := range s.requestsQueued { // Try to figure out whether the completed epoch have advanced enough for the request to be picked up. diff --git a/internal/proxy/handler.go b/internal/proxy/handler.go index 8abfa6d..1308911 100644 --- a/internal/proxy/handler.go +++ b/internal/proxy/handler.go @@ -33,6 +33,18 @@ func ReverseProxyHandler(reflector *mirror.Reflector, url *url.URL) func(res htt requestEpoch := epoch.Add(1) activeRequests.Set(requestEpoch, nil) + // Catch panic in serving HTTP + defer func() { + if p := recover(); p != nil { + // At this point the request has been served to the main target, so we remove this as active request + activeRequests.Remove(requestEpoch) + + reflector.IncomingCh <- mirror.NewRequest(req, body, requestEpoch, activeSnapshot) + + panic(p) + } + }() + // Server the request to main target proxyTo.ServeHTTP(res, req)