Skip to content

Commit

Permalink
Merge pull request #155 from foomo/fix/circuit-breaker-nil-pointer-ex…
Browse files Browse the repository at this point in the history
…ception

 fix: circuit breaker nil pointer exception
  • Loading branch information
franklinkim authored Jan 24, 2023
2 parents 4d9bb09 + 80ebf92 commit 8e7d0a2
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions net/http/roundtripware/circuitbreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,15 @@ func CircuitBreaker(set *CircuitBreakerSettings, opts ...CircuitBreakerOption) R

return func(l *zap.Logger, next Handler) Handler {
return func(r *http.Request) (*http.Response, error) {
if r == nil {
return nil, errors.New("request is nil")
}

// we need to detect the state change by ourselves, because the context does not allow us to hand in a context
fromState := circuitBreaker.State()

// clone the request and the body if wanted
var errCopy error
reqCopy, errCopy := copyRequest(r, o.CopyReqBody)
if errCopy != nil {
l.Error("unable to copy request", log.FError(errCopy))
Expand All @@ -142,6 +147,9 @@ func CircuitBreaker(set *CircuitBreakerSettings, opts ...CircuitBreakerOption) R
// call the next handler enclosed in the circuit breaker.
resp, err := circuitBreaker.Execute(func() (interface{}, error) {
resp, err := next(r)
if resp == nil {
return nil, o.IsSuccessful(err, reqCopy, nil)
}

// clone the response and the body if wanted
respCopy, errCopy := copyResponse(resp, o.CopyRespBody)
Expand Down

0 comments on commit 8e7d0a2

Please sign in to comment.