|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Development Commands |
| 6 | + |
| 7 | +### Testing |
| 8 | +- Run all tests: `vendor/bin/phpunit` |
| 9 | +- Run tests with Docker: `docker-compose run --rm -w /tmp/ganesha -u ganesha client vendor/bin/phpunit` |
| 10 | +- Install dependencies with Docker: `docker-compose run --rm -w /tmp/ganesha -u ganesha client composer install` |
| 11 | + |
| 12 | +### Code Quality |
| 13 | +- Run static analysis: `vendor/bin/psalm` |
| 14 | +- PHP-CS-Fixer for code formatting: `vendor/bin/php-cs-fixer fix` |
| 15 | + |
| 16 | +### Local Development with Docker |
| 17 | +- Start data stores (Redis, Memcached, etc): `docker-compose up` |
| 18 | +- The Docker setup provides isolated environments for testing against different storage adapters |
| 19 | + |
| 20 | +## Architecture Overview |
| 21 | + |
| 22 | +### Core Circuit Breaker Pattern |
| 23 | +Ganesha is a PHP implementation of the Circuit Breaker pattern with a clean separation of concerns: |
| 24 | + |
| 25 | +- **Main Entry Point**: `Ganesha` class provides the core API (`isAvailable()`, `success()`, `failure()`) |
| 26 | +- **Builder Pattern**: Fluent builders for different strategies via `Builder::withRateStrategy()` and `Builder::withCountStrategy()` |
| 27 | +- **Strategy Pattern**: Two failure detection strategies (`Rate` and `Count`) implement `StrategyInterface` |
| 28 | +- **Storage Adapters**: Multiple adapters (Redis, Memcached, APCu, MongoDB) implement `AdapterInterface` |
| 29 | + |
| 30 | +### Two Circuit Breaker Strategies |
| 31 | + |
| 32 | +#### Rate Strategy |
| 33 | +Tracks failure rate percentage over a time window: |
| 34 | +- `timeWindow()` - Time period for evaluation |
| 35 | +- `failureRateThreshold()` - Percentage threshold (1-100) |
| 36 | +- `minimumRequests()` - Minimum requests before triggering |
| 37 | +- `intervalToHalfOpen()` - Time before retrying |
| 38 | + |
| 39 | +#### Count Strategy |
| 40 | +Tracks absolute failure count: |
| 41 | +- `failureCountThreshold()` - Number of failures before triggering |
| 42 | +- `intervalToHalfOpen()` - Time before retrying |
| 43 | + |
| 44 | +### Storage Architecture |
| 45 | +Two time window implementations based on storage capabilities: |
| 46 | +- **SlidingTimeWindow** (Redis, MongoDB) - Rolling window of exact time periods |
| 47 | +- **TumblingTimeWindow** (APCu, Memcached) - Fixed time segments |
| 48 | + |
| 49 | +### HTTP Client Integrations |
| 50 | +- **Guzzle Middleware**: `GuzzleMiddleware` for transparent Guzzle integration |
| 51 | +- **Symfony HttpClient**: `GaneshaHttpClient` wrapper for Symfony HttpClient |
| 52 | +- Service name extraction strategies and failure detection customization |
| 53 | + |
| 54 | +### Key Components |
| 55 | +- `Configuration` - Validates and stores strategy parameters |
| 56 | +- `Context` - Manages circuit state and transitions |
| 57 | +- `Storage` - Abstraction layer over different storage adapters |
| 58 | +- `StorageKeys` - Configurable key naming for storage operations |
| 59 | + |
| 60 | +### Events System |
| 61 | +The circuit breaker publishes events: |
| 62 | +- `EVENT_TRIPPED` - Circuit opened due to failures |
| 63 | +- `EVENT_CALMED_DOWN` - Circuit closed after recovery |
| 64 | +- `EVENT_STORAGE_ERROR` - Storage backend issues |
| 65 | + |
| 66 | +## Testing Patterns |
| 67 | + |
| 68 | +Tests are organized by component with extensive integration testing for storage adapters. VCR (Video Cassette Recorder) is used for HTTP interaction recording in Guzzle middleware tests. |
| 69 | + |
| 70 | +The test structure mirrors the source structure under `tests/Ackintosh/` with comprehensive coverage of both strategies and all storage adapters. |
0 commit comments