improved handling of disallowed reads from body

This commit is contained in:
Michael Hegel 2023-08-31 14:54:29 +02:00
parent ba1a11b1ff
commit f12379252f
2 changed files with 9 additions and 2 deletions

View File

@ -21,8 +21,13 @@ var (
// needed
ErrCircuitBreaker = errors.New("circuit breaker triggered")
// ErrIgnoreSuccessfulness
// ErrIgnoreSuccessfulness can be returned by the IsSuccessful callback in order for the RoundTripware to ignore the
// result of the function
ErrIgnoreSuccessfulness = errors.New("ignored successfulness")
// ErrReadFromActualBody when it is attempted to read from a body in the IsSuccessful callback that has not
// previously been copied.
ErrReadFromActualBody = errors.New("read from actual body")
)
// CircuitBreakerSettings is a copy of the gobreaker.Settings, except that the IsSuccessful function is omitted since we
@ -184,7 +189,7 @@ func CircuitBreaker(set *CircuitBreakerSettings, opts ...CircuitBreakerOption) R
// we actually want to return an error instead of the original request and error since the user
// should be made aware that there is a misconfiguration
resp = nil
err = errSuccess
err = ErrReadFromActualBody
} else if !errors.Is(errSuccess, ErrIgnoreSuccessfulness) {
done(errSuccess == nil)
}

View File

@ -230,6 +230,7 @@ func TestCircuitBreakerReadFromNotCopiedBodies(t *testing.T) {
defer resp.Body.Close()
}
require.Error(t, err)
require.ErrorIs(t, err, roundtripware.ErrReadFromActualBody)
// same thing for the response
client = keelhttp.NewHTTPClient(
@ -258,6 +259,7 @@ func TestCircuitBreakerReadFromNotCopiedBodies(t *testing.T) {
defer resp.Body.Close()
}
require.Error(t, err)
require.ErrorIs(t, err, roundtripware.ErrReadFromActualBody)
}
func TestCircuitBreakerInterval(t *testing.T) {