Skip to content

Handle HTTP status codes in the HTTP breaker example - #121

Open
ZayanKhan-12 wants to merge 1 commit into
sony:masterfrom
ZayanKhan-12:example-handle-http-status-codes
Open

ZayanKhan-12 wants to merge 1 commit into
sony:masterfrom
ZayanKhan-12:example-handle-http-status-codes

Conversation

@ZayanKhan-12

Copy link
Copy Markdown

Fixes #46

The example/http_breaker.go example (and its v2 counterpart) treated any completed request as a success:

  • a server returning 500s never tripped the circuit breaker, defeating the pattern the example demonstrates;
  • error pages such as 404 were returned to the caller as the response body, with no indication anything went wrong.

This PR updates both examples to:

  • return a StatusError for non-2xx responses, so callers always get operational context instead of error-page HTML;
  • configure Settings.IsSuccessful so that only transport errors and 5xx responses count as circuit breaker failures.

The IsSuccessful split follows the discussion in #46: client errors such as 404 are responses the server produced deliberately (the service is healthy), so they are surfaced to the caller but do not trip the breaker, while server errors indicate an unhealthy service and do.

Both examples are kept minimal and identical apart from the v1/v2 API differences.

Verification

  • gofmt, go vet ./... clean in both modules
  • Full go test ./... passes in both the root and v2 modules
  • Verified behaviorally with a temporary httptest harness (not committed): repeated 500s open the breaker (ErrOpenState), repeated 404s never do, and 2xx returns the body unchanged

🤖 Generated with Claude Code

The example treated any completed request as a success, so a server
returning 500s never tripped the circuit breaker, and error pages such
as 404 were returned to the caller as the response body with no
indication that anything went wrong.

Return a StatusError for non-2xx responses so callers get the status
context, and use Settings.IsSuccessful so that only transport errors
and 5xx responses count as circuit breaker failures; client errors
such as 404 are handled responses and do not trip the breaker.

Fixes sony#46

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

example: HTTP circuit breaker doesn't consider HTTP status codes which are more indicative of retryable errors

1 participant