|
| 1 | +# Hystrix Factory |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The Hystrix Factory provides integration with the [Hystrix-Go](https://github.com/afex/hystrix-go) library, implementing the circuit breaker pattern for Go applications within the Boost framework. This factory enables resilient and fault-tolerant systems by preventing cascading failures in distributed environments. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Command Configuration**: Easy configuration of Hystrix commands through Boost's configuration system |
| 10 | +- **Circuit Breaker Pattern**: Implementation of the circuit breaker pattern to handle failures gracefully |
| 11 | +- **Configurable Parameters**: Fine-grained control over timeout, concurrency, error thresholds, and recovery windows |
| 12 | +- **Logging Integration**: Seamless integration with Boost's logging system |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +### Basic Configuration |
| 17 | + |
| 18 | +```go |
| 19 | +package main |
| 20 | + |
| 21 | +import ( |
| 22 | + "github.com/xgodev/boost" |
| 23 | + "github.com/xgodev/boost/factory/contrib/afex/hystrix-go/v0" |
| 24 | +) |
| 25 | + |
| 26 | +func main() { |
| 27 | + // Initialize Boost |
| 28 | + boost.Start() |
| 29 | + |
| 30 | + // Configure a Hystrix command |
| 31 | + hystrix.ConfigureCommand("my_command") |
| 32 | + |
| 33 | + // Use the command in your application |
| 34 | + // ... |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### Configuring Multiple Commands |
| 39 | + |
| 40 | +```go |
| 41 | +// Configure multiple commands at once |
| 42 | +commands := []string{"database_query", "api_request", "authentication"} |
| 43 | +hystrix.ConfigureCommands(commands) |
| 44 | +``` |
| 45 | + |
| 46 | +### Custom Configuration |
| 47 | + |
| 48 | +You can customize Hystrix command parameters through Boost's configuration system: |
| 49 | + |
| 50 | +``` |
| 51 | +# In your configuration file or environment variables |
| 52 | +boost.factory.hystrix.commands.my_command.timeout=5000 |
| 53 | +boost.factory.hystrix.commands.my_command.maxConcurrentRequests=10 |
| 54 | +boost.factory.hystrix.commands.my_command.requestVolumeThreshold=20 |
| 55 | +boost.factory.hystrix.commands.my_command.errorPercentThreshold=25 |
| 56 | +boost.factory.hystrix.commands.my_command.sleepWindow=10000 |
| 57 | +``` |
| 58 | + |
| 59 | +## Configuration Parameters |
| 60 | + |
| 61 | +| Parameter | Description | Default | |
| 62 | +|-----------|-------------|---------| |
| 63 | +| `timeout` | How long to wait for command to complete (milliseconds) | 10000 | |
| 64 | +| `maxConcurrentRequests` | Maximum number of concurrent requests | 20 | |
| 65 | +| `requestVolumeThreshold` | Minimum requests needed before circuit can trip | 10 | |
| 66 | +| `errorPercentThreshold` | Error percentage to open the circuit | 5 | |
| 67 | +| `sleepWindow` | Time to wait after circuit opens before testing recovery (milliseconds) | 5000 | |
| 68 | + |
| 69 | +## Integration with Other Boost Components |
| 70 | + |
| 71 | +The Hystrix Factory integrates with: |
| 72 | + |
| 73 | +- **Config Wrapper**: For loading and managing configuration |
| 74 | +- **Log Wrapper**: For logging circuit breaker events and errors |
| 75 | + |
| 76 | +## Plugin System |
| 77 | + |
| 78 | +The factory supports plugins for extending functionality. Custom plugins can be added to the `plugins` directory and registered with the factory. |
| 79 | + |
| 80 | +## Example |
| 81 | + |
| 82 | +```go |
| 83 | +package main |
| 84 | + |
| 85 | +import ( |
| 86 | + "errors" |
| 87 | + "github.com/afex/hystrix-go/hystrix" |
| 88 | + hystrixfactory "github.com/xgodev/boost/factory/contrib/afex/hystrix-go/v0" |
| 89 | + "github.com/xgodev/boost/wrapper/log" |
| 90 | +) |
| 91 | + |
| 92 | +func main() { |
| 93 | + // Configure the command |
| 94 | + hystrixfactory.ConfigureCommand("get_user_profile") |
| 95 | + |
| 96 | + // Use the command |
| 97 | + err := hystrix.Do("get_user_profile", func() error { |
| 98 | + // This function will be executed if the circuit is closed |
| 99 | + return callExternalService() |
| 100 | + }, func(err error) error { |
| 101 | + // This function will be executed if the circuit is open |
| 102 | + log.Errorf("Circuit open, using fallback: %v", err) |
| 103 | + return getFallbackUserProfile() |
| 104 | + }) |
| 105 | + |
| 106 | + if err != nil { |
| 107 | + log.Errorf("Error: %v", err) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func callExternalService() error { |
| 112 | + // Actual implementation to call external service |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func getFallbackUserProfile() error { |
| 117 | + // Fallback implementation |
| 118 | + return errors.New("using cached profile") |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## Best Practices |
| 123 | + |
| 124 | +1. **Command Naming**: Use descriptive names for commands that reflect their purpose |
| 125 | +2. **Timeout Configuration**: Set appropriate timeouts based on expected response times |
| 126 | +3. **Error Thresholds**: Adjust error thresholds based on your application's tolerance for failures |
| 127 | +4. **Monitoring**: Monitor circuit breaker states to understand system health |
| 128 | + |
| 129 | +## References |
| 130 | + |
| 131 | +- [Hystrix-Go GitHub Repository](https://github.com/afex/hystrix-go) |
| 132 | +- [Circuit Breaker Pattern](https://martinfowler.com/bliki/CircuitBreaker.html) |
0 commit comments