Phase 8.1: Implement Polly Circuit Breaker Pattern#153
Merged
Conversation
- Implement configuration class with failure thresholds - Add advanced circuit breaker settings (failure rate, sampling duration) - Configure break duration and minimum throughput - Provide TimeSpan properties for Polly integration Related to #108
…licies - Implement HTTP circuit breaker with status code handling - Implement database circuit breaker for MongoDB operations - Implement broker circuit breaker for RabbitMQ operations - Add state change callbacks (onBreak, onReset, onHalfOpen) - Use advanced circuit breaker with failure rate threshold - Comprehensive logging for circuit state changes Related to #108
- Implement wrapped policies for HTTP, database, and broker - Circuit breaker (outer) wraps retry (inner) for proper order - Reuse existing RetryPolicyFactory and CircuitBreakerFactory - Enable fail-fast when circuit is open (no retry attempts) Related to #108
- Implement thread-safe state tracking using ConcurrentDictionary - Add methods to record and query circuit states - Provide aggregated view of all circuit states - Enable detection of open circuits for monitoring Related to #108
- Implement health check that monitors circuit breaker states - Return Healthy when all circuits are closed - Return Degraded when circuits are half-open (testing recovery) - Return Unhealthy when any circuit is open - Include circuit state details in health check data Related to #108
…er support - Register CircuitBreakerConfiguration from configuration - Create wrapped resilience policies (retry + circuit breaker) - Register database and broker wrapped policies as singletons - Update HTTP client factory to support wrapped policies - Maintain backward compatibility with existing retry policies Related to #108
- Add CircuitBreaker section under Resilience - Configure failure thresholds and rates - Set break duration and sampling duration - Use production-ready conservative values Related to #108
- Add CircuitBreakerStateService as singleton - Register CircuitBreakerHealthCheck for monitoring - Maintain existing health checks and configuration - Enable circuit breaker state tracking and health monitoring Related to #108
- Test circuit opening after threshold exceeded - Test circuit reset after break duration - Test state transitions (Closed -> Open -> Half-Open -> Closed) - Test CircuitBreakerStateService tracking - Test CircuitBreakerHealthCheck with various states - Verify fail-fast behavior when circuit is open - Test recovery mechanism in half-open state Related to #108
- Test healthy status when all circuits are closed - Test degraded status when circuits are half-open - Test unhealthy status when circuits are open - Test with no circuits configured - Verify health check data includes circuit states Related to #108
- Document circuit breaker pattern and benefits - Explain advanced vs simple circuit breaker - Detail configuration options and recommendations - Provide usage examples for all service types - Document state transitions and monitoring - Include testing and troubleshooting guides - Add performance considerations Related to #108
…rhead - Change threshold from 100ms to 500ms for fail-fast test - Account for test framework overhead, GC, and OS scheduling - Still validates fast failure vs retry delays (which would be seconds) - More reliable test execution across different environments Related to #108
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📋 Description
Implements the Circuit Breaker pattern using Polly to prevent cascading failures when external services (MongoDB, RabbitMQ, HTTP APIs) are unavailable or degraded. This PR wraps retry policies with circuit breakers following the proper pattern: Circuit Breaker (outer) → Retry (inner).
🔗 Related Issue
Closes #108
🎯 Objectives Completed
📦 Changes Made
New Files
Infrastructure Layer
src/StarGate.Infrastructure/Resilience/CircuitBreakerConfiguration.cs- Configuration for circuit breaker policiessrc/StarGate.Infrastructure/Resilience/CircuitBreakerFactory.cs- Factory for creating HTTP, database, and broker circuit breakerssrc/StarGate.Infrastructure/Resilience/ResiliencePolicyWrapper.cs- Wraps retry and circuit breaker policiessrc/StarGate.Infrastructure/Resilience/CircuitBreakerStateService.cs- Tracks circuit breaker statesServer Layer
src/StarGate.Server/HealthChecks/CircuitBreakerHealthCheck.cs- Health check for monitoring circuit statesTests
tests/StarGate.Infrastructure.Tests/Resilience/CircuitBreakerTests.cs- 9 unit tests for circuit breaker functionalitytests/StarGate.Server.Tests/HealthChecks/CircuitBreakerHealthCheckTests.cs- 8 unit tests for health checkDocumentation
docs/CIRCUIT-BREAKER.md- Comprehensive documentation (states, configuration, usage, monitoring)Modified Files
src/StarGate.Infrastructure/Extensions/ResilienceServiceCollectionExtensions.cs- Register wrapped policies and circuit breaker configurationsrc/StarGate.Server/appsettings.json- Add CircuitBreaker configuration sectionsrc/StarGate.Server/Program.cs- Register CircuitBreakerStateService and health check🏗️ Architecture
Circuit Breaker States
Policy Wrapping Order
Why this order?
🧪 Testing
Unit Tests (17 total)
CircuitBreakerTests.cs (9 tests):
CircuitBreakerHealthCheckTests.cs (8 tests):
Test Results
All tests passing ✅
⚙️ Configuration
Production Settings (Conservative)
{ "CircuitBreaker": { "FailureThreshold": 5, "FailureRateThreshold": 0.5, "MinimumThroughput": 10, "BreakDurationSeconds": 30.0, "SamplingDurationSeconds": 60.0 } }Key Parameters
📊 Monitoring
Health Check Endpoint
Response includes circuit states:
{ "circuit-breakers": { "status": "Healthy", "description": "All circuit breakers closed", "data": { "database": "Closed", "broker": "Closed" } } }Logging
Automatic logging for all state changes:
🎨 Code Quality
🔒 Benefits
1. Prevents Cascading Failures
2. Fast Failure
3. Automatic Recovery
4. Observable
📚 Documentation
Comprehensive documentation added in
docs/CIRCUIT-BREAKER.md:🔗 Dependencies
⏱️ Estimated Effort
Actual: 8-10 hours (as estimated in #108)
📋 Checklist
🚀 Next Steps
After merge:
📝 Notes
Ready for Review ✨