This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
- Run all tests:
vendor/bin/phpunit - Run tests with Docker:
docker-compose run --rm -w /tmp/ganesha -u ganesha client vendor/bin/phpunit - Install dependencies with Docker:
docker-compose run --rm -w /tmp/ganesha -u ganesha client composer install
- Run static analysis:
vendor/bin/psalm - PHP-CS-Fixer for code formatting:
vendor/bin/php-cs-fixer fix
- Start data stores (Redis, Memcached, etc):
docker-compose up - The Docker setup provides isolated environments for testing against different storage adapters
Ganesha is a PHP implementation of the Circuit Breaker pattern with a clean separation of concerns:
- Main Entry Point:
Ganeshaclass provides the core API (isAvailable(),success(),failure()) - Builder Pattern: Fluent builders for different strategies via
Builder::withRateStrategy()andBuilder::withCountStrategy() - Strategy Pattern: Two failure detection strategies (
RateandCount) implementStrategyInterface - Storage Adapters: Multiple adapters (Redis, Memcached, APCu, MongoDB) implement
AdapterInterface
Tracks failure rate percentage over a time window:
timeWindow()- Time period for evaluationfailureRateThreshold()- Percentage threshold (1-100)minimumRequests()- Minimum requests before triggeringintervalToHalfOpen()- Time before retrying
Tracks absolute failure count:
failureCountThreshold()- Number of failures before triggeringintervalToHalfOpen()- Time before retrying
Two time window implementations based on storage capabilities:
- SlidingTimeWindow (Redis, MongoDB) - Rolling window of exact time periods
- TumblingTimeWindow (APCu, Memcached) - Fixed time segments
- Guzzle Middleware:
GuzzleMiddlewarefor transparent Guzzle integration - Symfony HttpClient:
GaneshaHttpClientwrapper for Symfony HttpClient - Service name extraction strategies and failure detection customization
Configuration- Validates and stores strategy parametersContext- Manages circuit state and transitionsStorage- Abstraction layer over different storage adaptersStorageKeys- Configurable key naming for storage operations
The circuit breaker publishes events:
EVENT_TRIPPED- Circuit opened due to failuresEVENT_CALMED_DOWN- Circuit closed after recoveryEVENT_STORAGE_ERROR- Storage backend issues
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.
The test structure mirrors the source structure under tests/Ackintosh/ with comprehensive coverage of both strategies and all storage adapters.