Skip to content

Commit

Permalink
Merge pull request #26 from StackVista/stac-21470-fix-panic
Browse files Browse the repository at this point in the history
STAC-21470: Forward request even on panic
  • Loading branch information
rb3ckers authored Aug 20, 2024
2 parents 33e2df3 + c7c9706 commit e13fc1a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/mirror/reflector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/mirror/sendqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions internal/proxy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit e13fc1a

Please sign in to comment.