Skip to content

Conversation

@coolwednesday
Copy link
Member

@coolwednesday coolwednesday commented Jan 7, 2026

Fix Circuit Breaker Concurrency, Error Handling, and Metrics

Description

This PR addresses critical issues in the Circuit Breaker implementation regarding concurrency safety, error handling logic, and metric reporting.

Key Changes

1. Concurrency Safety & Race Condition Fixes

  • pkg/gofr/service/circuit_breaker.go: Added necessary locking in startHealthChecks and tryCircuitRecovery. Previously, resetCircuit was being called without holding the lock, leading to potential race conditions on state and failureCount.
  • pkg/gofr/service/metrics_helper.go: Implemented a global lock (metricsMu) for metric registration to prevent "concurrent map writes" when multiple services initialize metrics simultaneously.

2. Robust Edge Case Handling

  • Thundering Herd Fix: In tryCircuitRecovery, added a check for cb.state == ClosedState immediately after acquiring the lock. This prevents queued requests from re-running the health check if another request has already recovered the circuit.
  • Busy Loop Prevention: Updated lastChecked timestamp before attempting a health check in tryCircuitRecovery. This ensures that if a health check fails, subsequent requests respect the interval wait time instead of immediately retrying in a tight loop.
Screenshot 2026-01-09 at 6 25 59 PM

3. Functional Correctness

  • HTTP 500 Handling: Updated executeWithCircuitBreaker to treat HTTP 500+ status codes as failures. Previously, only network errors (err != nil) triggered the circuit breaker, causing it to remain closed during server-side outages.

Checklist:

  • I have formatted my code using goimport and golangci-lint.
  • All new code is covered by unit tests.
  • This PR does not decrease the overall code coverage.
  • I have reviewed the code comments and documentation for clarity.

@coolwednesday coolwednesday changed the title Metrics in Http Options Metrics in HttpService Options Jan 7, 2026
Umang01-hash
Umang01-hash previously approved these changes Jan 9, 2026
@coolwednesday coolwednesday merged commit 93f8de0 into development Jan 10, 2026
20 checks passed
@coolwednesday coolwednesday deleted the metrics-http-options branch January 10, 2026 07:02
coolwednesday added a commit that referenced this pull request Jan 17, 2026
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.

metrics support for the circuit breaker

4 participants