Loading documentation...
Loading documentation...
Loading documentation...
Complete API documentation for the circuit package.
Import Path: github.com/kolosys/ion/circuit
Package circuit provides circuit breaker functionality for resilient microservices. Circuit breakers prevent cascading failures by temporarily blocking requests to failing services, allowing them time to recover while providing fast-fail behavior to callers.
The circuit breaker implements a three-state machine:
Usage:
cb := circuit.New("payment-service",
circuit.WithFailureThreshold(5),
circuit.WithRecoveryTimeout(30*time.Second),
)
result, err := cb.Execute(ctx, func(ctx context.Context) (any, error) {
return paymentService.ProcessPayment(ctx, payment)
})The circuit breaker integrates with ION's observability system and supports context cancellation, timeouts, and comprehensive metrics collection.
CircuitBreaker represents a circuit breaker that controls access to a potentially failing operation. It provides fast-fail behavior when the operation is failing and automatic recovery testing when appropriate.
// Example implementation of CircuitBreaker
type MyCircuitBreaker struct {
// Add your fields here
}
func (m MyCircuitBreaker) Execute(param1 context.Context, param2 func(context.Context) (any, error)) any {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Call(param1 context.Context, param2 func(context.Context) error) error {
// Implement your logic here
return
}
func (m MyCircuitBreaker) State() State {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Metrics() CircuitMetrics {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Reset() {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Close() error {
// Implement your logic here
return
}
type CircuitBreaker interface {
Execute(ctx context.Context, fn func(context.Context) (any, error)) (any, error)
Call(ctx context.Context, fn func(context.Context) error) error
State() State
Metrics() CircuitMetrics
Reset()
Close() error
}| Method | Description |
|---|
New creates a new circuit breaker with the given name and options.
func New(name string, options ...Option) CircuitBreakerParameters:
name (string)options (...Option)Returns:
CircuitError represents circuit breaker specific errors with context
// Create a new CircuitError
circuiterror := CircuitError{
Op: "example",
CircuitName: "example",
State: "example",
Err: error{},
}type CircuitError struct {
Op string
CircuitName string
State string
Err error
}| Field | Type | Description |
|---|---|---|
| Op | string | operation that failed |
| CircuitName | string | name of the circuit breaker |
| State | string | current state of the circuit |
| Err | error | underlying error |
func (*CircuitError) Error() stringParameters: None
Returns:
IsCircuitOpen returns true if the error is due to an open circuit.
func (*CircuitError) IsCircuitOpen() boolParameters: None
Returns:
func (*CircuitError) Unwrap() errorParameters: None
Returns:
CircuitMetrics holds metrics for a circuit breaker instance.
// Create a new CircuitMetrics
circuitmetrics := CircuitMetrics{
Name: "example",
State: State{},
TotalRequests: 42,
TotalFailures: 42,
TotalSuccesses: 42,
ConsecutiveFails: 42,
StateChanges: 42,
LastFailure: /* value */,
LastSuccess: /* value */,
LastStateChange: /* value */,
}type CircuitMetrics struct {
Name string
State State
TotalRequests int64
TotalFailures int64
TotalSuccesses int64
ConsecutiveFails int64
StateChanges int64
LastFailure time.Time
LastSuccess time.Time
LastStateChange time.Time
}| Field | Type | Description |
|---|---|---|
| Name | string | Name is the name of the circuit breaker |
| State | State | State is the current state of the circuit |
| TotalRequests | int64 | TotalRequests is the total number of requests processed |
| TotalFailures | int64 | TotalFailures is the total number of failed requests |
| TotalSuccesses | int64 | TotalSuccesses is the total number of successful requests |
| ConsecutiveFails | int64 | ConsecutiveFails is the current count of consecutive failures |
| StateChanges | int64 | StateChanges is the total number of state transitions |
| LastFailure | time.Time | LastFailure is the timestamp of the last failure |
| LastSuccess | time.Time | LastSuccess is the timestamp of the last success |
| LastStateChange | time.Time | LastStateChange is the timestamp of the last state change |
FailureRate returns the failure rate as a percentage (0.0 to 1.0).
func (CircuitMetrics) FailureRate() float64Parameters: None
Returns:
IsHealthy returns true if the circuit appears to be healthy based on recent activity.
func (CircuitMetrics) IsHealthy() boolParameters: None
Returns:
SuccessRate returns the success rate as a percentage (0.0 to 1.0).
func (CircuitMetrics) SuccessRate() float64Parameters: None
Returns:
Config holds configuration for a circuit breaker.
// Create a new Config
config := Config{
FailureThreshold: 42,
RecoveryTimeout: /* value */,
HalfOpenMaxRequests: 42,
HalfOpenSuccessThreshold: 42,
IsFailure: /* value */,
OnStateChange: /* value */,
}type Config struct {
FailureThreshold int64
RecoveryTimeout time.Duration
HalfOpenMaxRequests int64
HalfOpenSuccessThreshold int64
IsFailure func(error) bool
OnStateChange func(from, to State)
}| Field | Type | Description |
|---|---|---|
| FailureThreshold | int64 | FailureThreshold is the number of consecutive failures required to trip the circuit. Default: 5 |
| RecoveryTimeout | time.Duration | RecoveryTimeout is the duration to wait in the open state before transitioning to half-open for recovery testing. Default: 30 seconds |
| HalfOpenMaxRequests | int64 | HalfOpenMaxRequests is the maximum number of requests allowed in half-open state. Default: 3 |
| HalfOpenSuccessThreshold | int64 | HalfOpenSuccessThreshold is the number of successful requests required in half-open state to transition back to closed. Default: 2 |
| IsFailure | func(error) bool | IsFailure is a predicate function that determines if an error should be counted as a failure for circuit breaker purposes. If nil, all non-nil errors are considered failures. |
| OnStateChange | func(from, to State) | OnStateChange is called whenever the circuit breaker changes state. This is useful for logging or metrics collection. |
DefaultConfig returns a Config with sensible defaults.
func DefaultConfig() *ConfigParameters: None
Returns:
Validate checks if the configuration is valid and returns an error if not.
func (*Config) Validate() errorParameters: None
Returns:
Option is a function that configures a circuit breaker.
// Example usage of Option
var value Option
// Initialize with appropriate valuetype Option func(*Config, *observe.Observability)Aggressive returns options for a circuit breaker that trips quickly and takes time to recover. Suitable for protecting against cascading failures.
func Aggressive() []OptionParameters: None
Returns:
Conservative returns options for a circuit breaker that is slow to trip and slow to recover. Suitable for critical operations.
func Conservative() []OptionParameters: None
Returns:
QuickFailover returns options for a circuit breaker that fails over quickly but also recovers quickly. Suitable for non-critical operations.
func QuickFailover() []OptionParameters: None
Returns:
WithFailurePredicate sets a custom predicate to determine what constitutes a failure. If not set, all non-nil errors are considered failures.
func WithFailurePredicate(isFailure func(error) bool) OptionParameters:
isFailure (func(error) bool)Returns:
WithFailureThreshold sets the number of consecutive failures required to trip the circuit.
func WithFailureThreshold(threshold int64) OptionParameters:
threshold (int64)Returns:
WithHalfOpenMaxRequests sets the maximum number of requests allowed in half-open state.
func WithHalfOpenMaxRequests(maxRequests int64) OptionParameters:
maxRequests (int64)Returns:
WithHalfOpenSuccessThreshold sets the number of successful requests required in half-open state to transition back to closed.
func WithHalfOpenSuccessThreshold(threshold int64) OptionParameters:
threshold (int64)Returns:
WithLogger sets the logger for the circuit breaker.
func WithLogger(logger observe.Logger) OptionParameters:
logger (observe.Logger)Returns:
WithMetrics sets the metrics recorder for the circuit breaker.
func WithMetrics(metrics observe.Metrics) OptionParameters:
metrics (observe.Metrics)Returns:
WithName is a convenience option that adds the circuit breaker name to log and metric tags. This is automatically handled by the New function, but can be useful for testing.
func WithName(name string) OptionParameters:
name (string)Returns:
WithObservability sets the observability hooks for logging, metrics, and tracing.
func WithObservability(observability *observe.Observability) OptionParameters:
observability (*observe.Observability)Returns:
WithRecoveryTimeout sets the duration to wait in open state before attempting recovery.
func WithRecoveryTimeout(timeout time.Duration) OptionParameters:
timeout (time.Duration)Returns:
WithStateChangeCallback sets a callback to be invoked on state changes.
func WithStateChangeCallback(callback func(from, to State)) OptionParameters:
callback (func(from, to State))Returns:
WithTracer sets the tracer for the circuit breaker.
func WithTracer(tracer observe.Tracer) OptionParameters:
tracer (observe.Tracer)Returns:
State represents the current state of a circuit breaker.
// Example usage of State
var value State
// Initialize with appropriate valuetype State int32String returns the string representation of the circuit state.
func (State) String() stringParameters: None
Returns:
NewCircuitOpenError creates an error indicating the circuit is open
func NewCircuitOpenError(circuitName string) errorParameters:
| Parameter | Type | Description |
|---|---|---|
circuitName | string |
Returns:
| Type | Description |
|---|---|
error |
Example:
// Example usage of NewCircuitOpenError
result := NewCircuitOpenError(/* parameters */)NewCircuitTimeoutError creates an error indicating a circuit operation timed out
func NewCircuitTimeoutError(circuitName string) errorParameters:
| Parameter | Type | Description |
|---|---|---|
circuitName | string |
Returns:
| Type | Description |
|---|---|
error |
Example:
// Example usage of NewCircuitTimeoutError
result := NewCircuitTimeoutError(/* parameters */)Complete API documentation for the circuit package.
Import Path: github.com/kolosys/ion/circuit
Package circuit provides circuit breaker functionality for resilient microservices. Circuit breakers prevent cascading failures by temporarily blocking requests to failing services, allowing them time to recover while providing fast-fail behavior to callers.
The circuit breaker implements a three-state machine:
Usage:
cb := circuit.New("payment-service",
circuit.WithFailureThreshold(5),
circuit.WithRecoveryTimeout(30*time.Second),
)
result, err := cb.Execute(ctx, func(ctx context.Context) (any, error) {
return paymentService.ProcessPayment(ctx, payment)
})The circuit breaker integrates with ION's observability system and supports context cancellation, timeouts, and comprehensive metrics collection.
CircuitBreaker represents a circuit breaker that controls access to a potentially failing operation. It provides fast-fail behavior when the operation is failing and automatic recovery testing when appropriate.
// Example implementation of CircuitBreaker
type MyCircuitBreaker struct {
// Add your fields here
}
func (m MyCircuitBreaker) Execute(param1 context.Context, param2 func(context.Context) (any, error)) any {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Call(param1 context.Context, param2 func(context.Context) error) error {
// Implement your logic here
return
}
func (m MyCircuitBreaker) State() State {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Metrics() CircuitMetrics {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Reset() {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Close() error {
// Implement your logic here
return
}
type CircuitBreaker interface {
Execute(ctx context.Context, fn func(context.Context) (any, error)) (any, error)
Call(ctx context.Context, fn func(context.Context) error) error
State() State
Metrics() CircuitMetrics
Reset()
Close() error
}| Method | Description |
|---|
New creates a new circuit breaker with the given name and options.
func New(name string, options ...Option) CircuitBreakerParameters:
name (string)options (...Option)Returns:
CircuitError represents circuit breaker specific errors with context
// Create a new CircuitError
circuiterror := CircuitError{
Op: "example",
CircuitName: "example",
State: "example",
Err: error{},
}type CircuitError struct {
Op string
CircuitName string
State string
Err error
}| Field | Type | Description |
|---|---|---|
| Op | string | operation that failed |
| CircuitName | string | name of the circuit breaker |
| State | string | current state of the circuit |
| Err | error | underlying error |
func (*CircuitError) Error() stringParameters: None
Returns:
IsCircuitOpen returns true if the error is due to an open circuit.
func (*CircuitError) IsCircuitOpen() boolParameters: None
Returns:
func (*CircuitError) Unwrap() errorParameters: None
Returns:
CircuitMetrics holds metrics for a circuit breaker instance.
// Create a new CircuitMetrics
circuitmetrics := CircuitMetrics{
Name: "example",
State: State{},
TotalRequests: 42,
TotalFailures: 42,
TotalSuccesses: 42,
ConsecutiveFails: 42,
StateChanges: 42,
LastFailure: /* value */,
LastSuccess: /* value */,
LastStateChange: /* value */,
}type CircuitMetrics struct {
Name string
State State
TotalRequests int64
TotalFailures int64
TotalSuccesses int64
ConsecutiveFails int64
StateChanges int64
LastFailure time.Time
LastSuccess time.Time
LastStateChange time.Time
}| Field | Type | Description |
|---|---|---|
| Name | string | Name is the name of the circuit breaker |
| State | State | State is the current state of the circuit |
| TotalRequests | int64 | TotalRequests is the total number of requests processed |
| TotalFailures | int64 | TotalFailures is the total number of failed requests |
| TotalSuccesses | int64 | TotalSuccesses is the total number of successful requests |
| ConsecutiveFails | int64 | ConsecutiveFails is the current count of consecutive failures |
| StateChanges | int64 | StateChanges is the total number of state transitions |
| LastFailure | time.Time | LastFailure is the timestamp of the last failure |
| LastSuccess | time.Time | LastSuccess is the timestamp of the last success |
| LastStateChange | time.Time | LastStateChange is the timestamp of the last state change |
FailureRate returns the failure rate as a percentage (0.0 to 1.0).
func (CircuitMetrics) FailureRate() float64Parameters: None
Returns:
IsHealthy returns true if the circuit appears to be healthy based on recent activity.
func (CircuitMetrics) IsHealthy() boolParameters: None
Returns:
SuccessRate returns the success rate as a percentage (0.0 to 1.0).
func (CircuitMetrics) SuccessRate() float64Parameters: None
Returns:
Config holds configuration for a circuit breaker.
// Create a new Config
config := Config{
FailureThreshold: 42,
RecoveryTimeout: /* value */,
HalfOpenMaxRequests: 42,
HalfOpenSuccessThreshold: 42,
IsFailure: /* value */,
OnStateChange: /* value */,
}type Config struct {
FailureThreshold int64
RecoveryTimeout time.Duration
HalfOpenMaxRequests int64
HalfOpenSuccessThreshold int64
IsFailure func(error) bool
OnStateChange func(from, to State)
}| Field | Type | Description |
|---|---|---|
| FailureThreshold | int64 | FailureThreshold is the number of consecutive failures required to trip the circuit. Default: 5 |
| RecoveryTimeout | time.Duration | RecoveryTimeout is the duration to wait in the open state before transitioning to half-open for recovery testing. Default: 30 seconds |
| HalfOpenMaxRequests | int64 | HalfOpenMaxRequests is the maximum number of requests allowed in half-open state. Default: 3 |
| HalfOpenSuccessThreshold | int64 | HalfOpenSuccessThreshold is the number of successful requests required in half-open state to transition back to closed. Default: 2 |
| IsFailure | func(error) bool | IsFailure is a predicate function that determines if an error should be counted as a failure for circuit breaker purposes. If nil, all non-nil errors are considered failures. |
| OnStateChange | func(from, to State) | OnStateChange is called whenever the circuit breaker changes state. This is useful for logging or metrics collection. |
DefaultConfig returns a Config with sensible defaults.
func DefaultConfig() *ConfigParameters: None
Returns:
Validate checks if the configuration is valid and returns an error if not.
func (*Config) Validate() errorParameters: None
Returns:
Option is a function that configures a circuit breaker.
// Example usage of Option
var value Option
// Initialize with appropriate valuetype Option func(*Config, *observe.Observability)Aggressive returns options for a circuit breaker that trips quickly and takes time to recover. Suitable for protecting against cascading failures.
func Aggressive() []OptionParameters: None
Returns:
Conservative returns options for a circuit breaker that is slow to trip and slow to recover. Suitable for critical operations.
func Conservative() []OptionParameters: None
Returns:
QuickFailover returns options for a circuit breaker that fails over quickly but also recovers quickly. Suitable for non-critical operations.
func QuickFailover() []OptionParameters: None
Returns:
WithFailurePredicate sets a custom predicate to determine what constitutes a failure. If not set, all non-nil errors are considered failures.
func WithFailurePredicate(isFailure func(error) bool) OptionParameters:
isFailure (func(error) bool)Returns:
WithFailureThreshold sets the number of consecutive failures required to trip the circuit.
func WithFailureThreshold(threshold int64) OptionParameters:
threshold (int64)Returns:
WithHalfOpenMaxRequests sets the maximum number of requests allowed in half-open state.
func WithHalfOpenMaxRequests(maxRequests int64) OptionParameters:
maxRequests (int64)Returns:
WithHalfOpenSuccessThreshold sets the number of successful requests required in half-open state to transition back to closed.
func WithHalfOpenSuccessThreshold(threshold int64) OptionParameters:
threshold (int64)Returns:
WithLogger sets the logger for the circuit breaker.
func WithLogger(logger observe.Logger) OptionParameters:
logger (observe.Logger)Returns:
WithMetrics sets the metrics recorder for the circuit breaker.
func WithMetrics(metrics observe.Metrics) OptionParameters:
metrics (observe.Metrics)Returns:
WithName is a convenience option that adds the circuit breaker name to log and metric tags. This is automatically handled by the New function, but can be useful for testing.
func WithName(name string) OptionParameters:
name (string)Returns:
WithObservability sets the observability hooks for logging, metrics, and tracing.
func WithObservability(observability *observe.Observability) OptionParameters:
observability (*observe.Observability)Returns:
WithRecoveryTimeout sets the duration to wait in open state before attempting recovery.
func WithRecoveryTimeout(timeout time.Duration) OptionParameters:
timeout (time.Duration)Returns:
WithStateChangeCallback sets a callback to be invoked on state changes.
func WithStateChangeCallback(callback func(from, to State)) OptionParameters:
callback (func(from, to State))Returns:
WithTracer sets the tracer for the circuit breaker.
func WithTracer(tracer observe.Tracer) OptionParameters:
tracer (observe.Tracer)Returns:
State represents the current state of a circuit breaker.
// Example usage of State
var value State
// Initialize with appropriate valuetype State int32String returns the string representation of the circuit state.
func (State) String() stringParameters: None
Returns:
NewCircuitOpenError creates an error indicating the circuit is open
func NewCircuitOpenError(circuitName string) errorParameters:
| Parameter | Type | Description |
|---|---|---|
circuitName | string |
Returns:
| Type | Description |
|---|---|
error |
Example:
// Example usage of NewCircuitOpenError
result := NewCircuitOpenError(/* parameters */)NewCircuitTimeoutError creates an error indicating a circuit operation timed out
func NewCircuitTimeoutError(circuitName string) errorParameters:
| Parameter | Type | Description |
|---|---|---|
circuitName | string |
Returns:
| Type | Description |
|---|---|
error |
Example:
// Example usage of NewCircuitTimeoutError
result := NewCircuitTimeoutError(/* parameters */)cb := circuit.New("payment-service",
circuit.WithFailureThreshold(5),
circuit.WithRecoveryTimeout(30*time.Second),
)
result, err := cb.Execute(ctx, func(ctx context.Context) (any, error) {
return paymentService.ProcessPayment(ctx, payment)
})// Example implementation of CircuitBreaker
type MyCircuitBreaker struct {
// Add your fields here
}
func (m MyCircuitBreaker) Execute(param1 context.Context, param2 func(context.Context) (any, error)) any {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Call(param1 context.Context, param2 func(context.Context) error) error {
// Implement your logic here
return
}
func (m MyCircuitBreaker) State() State {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Metrics() CircuitMetrics {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Reset() {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Close() error {
// Implement your logic here
return
}
type CircuitBreaker interface {
Execute(ctx context.Context, fn func(context.Context) (any, error)) (any, error)
Call(ctx context.Context, fn func(context.Context) error) error
State() State
Metrics() CircuitMetrics
Reset()
Close() error
}func New(name string, options ...Option) CircuitBreaker// Create a new CircuitError
circuiterror := CircuitError{
Op: "example",
CircuitName: "example",
State: "example",
Err: error{},
}type CircuitError struct {
Op string
CircuitName string
State string
Err error
}func (*CircuitError) Error() stringfunc (*CircuitError) IsCircuitOpen() boolfunc (*CircuitError) Unwrap() error// Create a new CircuitMetrics
circuitmetrics := CircuitMetrics{
Name: "example",
State: State{},
TotalRequests: 42,
TotalFailures: 42,
TotalSuccesses: 42,
ConsecutiveFails: 42,
StateChanges: 42,
LastFailure: /* value */,
LastSuccess: /* value */,
LastStateChange: /* value */,
}type CircuitMetrics struct {
Name string
State State
TotalRequests int64
TotalFailures int64
TotalSuccesses int64
ConsecutiveFails int64
StateChanges int64
LastFailure time.Time
LastSuccess time.Time
LastStateChange time.Time
}func (CircuitMetrics) FailureRate() float64func (CircuitMetrics) IsHealthy() boolfunc (CircuitMetrics) SuccessRate() float64// Create a new Config
config := Config{
FailureThreshold: 42,
RecoveryTimeout: /* value */,
HalfOpenMaxRequests: 42,
HalfOpenSuccessThreshold: 42,
IsFailure: /* value */,
OnStateChange: /* value */,
}type Config struct {
FailureThreshold int64
RecoveryTimeout time.Duration
HalfOpenMaxRequests int64
HalfOpenSuccessThreshold int64
IsFailure func(error) bool
OnStateChange func(from, to State)
}func DefaultConfig() *Configfunc (*Config) Validate() error// Example usage of Option
var value Option
// Initialize with appropriate valuetype Option func(*Config, *observe.Observability)func Aggressive() []Optionfunc Conservative() []Optionfunc QuickFailover() []Optionfunc WithFailurePredicate(isFailure func(error) bool) Optionfunc WithFailureThreshold(threshold int64) Optionfunc WithHalfOpenMaxRequests(maxRequests int64) Optionfunc WithHalfOpenSuccessThreshold(threshold int64) Optionfunc WithLogger(logger observe.Logger) Optionfunc WithMetrics(metrics observe.Metrics) Optionfunc WithName(name string) Optionfunc WithObservability(observability *observe.Observability) Optionfunc WithRecoveryTimeout(timeout time.Duration) Optionfunc WithStateChangeCallback(callback func(from, to State)) Optionfunc WithTracer(tracer observe.Tracer) Option// Example usage of State
var value State
// Initialize with appropriate valuetype State int32func (State) String() stringfunc NewCircuitOpenError(circuitName string) error// Example usage of NewCircuitOpenError
result := NewCircuitOpenError(/* parameters */)func NewCircuitTimeoutError(circuitName string) error// Example usage of NewCircuitTimeoutError
result := NewCircuitTimeoutError(/* parameters */)cb := circuit.New("payment-service",
circuit.WithFailureThreshold(5),
circuit.WithRecoveryTimeout(30*time.Second),
)
result, err := cb.Execute(ctx, func(ctx context.Context) (any, error) {
return paymentService.ProcessPayment(ctx, payment)
})// Example implementation of CircuitBreaker
type MyCircuitBreaker struct {
// Add your fields here
}
func (m MyCircuitBreaker) Execute(param1 context.Context, param2 func(context.Context) (any, error)) any {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Call(param1 context.Context, param2 func(context.Context) error) error {
// Implement your logic here
return
}
func (m MyCircuitBreaker) State() State {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Metrics() CircuitMetrics {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Reset() {
// Implement your logic here
return
}
func (m MyCircuitBreaker) Close() error {
// Implement your logic here
return
}
type CircuitBreaker interface {
Execute(ctx context.Context, fn func(context.Context) (any, error)) (any, error)
Call(ctx context.Context, fn func(context.Context) error) error
State() State
Metrics() CircuitMetrics
Reset()
Close() error
}func New(name string, options ...Option) CircuitBreaker// Create a new CircuitError
circuiterror := CircuitError{
Op: "example",
CircuitName: "example",
State: "example",
Err: error{},
}type CircuitError struct {
Op string
CircuitName string
State string
Err error
}func (*CircuitError) Error() stringfunc (*CircuitError) IsCircuitOpen() boolfunc (*CircuitError) Unwrap() error// Create a new CircuitMetrics
circuitmetrics := CircuitMetrics{
Name: "example",
State: State{},
TotalRequests: 42,
TotalFailures: 42,
TotalSuccesses: 42,
ConsecutiveFails: 42,
StateChanges: 42,
LastFailure: /* value */,
LastSuccess: /* value */,
LastStateChange: /* value */,
}type CircuitMetrics struct {
Name string
State State
TotalRequests int64
TotalFailures int64
TotalSuccesses int64
ConsecutiveFails int64
StateChanges int64
LastFailure time.Time
LastSuccess time.Time
LastStateChange time.Time
}func (CircuitMetrics) FailureRate() float64func (CircuitMetrics) IsHealthy() boolfunc (CircuitMetrics) SuccessRate() float64// Create a new Config
config := Config{
FailureThreshold: 42,
RecoveryTimeout: /* value */,
HalfOpenMaxRequests: 42,
HalfOpenSuccessThreshold: 42,
IsFailure: /* value */,
OnStateChange: /* value */,
}type Config struct {
FailureThreshold int64
RecoveryTimeout time.Duration
HalfOpenMaxRequests int64
HalfOpenSuccessThreshold int64
IsFailure func(error) bool
OnStateChange func(from, to State)
}func DefaultConfig() *Configfunc (*Config) Validate() error// Example usage of Option
var value Option
// Initialize with appropriate valuetype Option func(*Config, *observe.Observability)func Aggressive() []Optionfunc Conservative() []Optionfunc QuickFailover() []Optionfunc WithFailurePredicate(isFailure func(error) bool) Optionfunc WithFailureThreshold(threshold int64) Optionfunc WithHalfOpenMaxRequests(maxRequests int64) Optionfunc WithHalfOpenSuccessThreshold(threshold int64) Optionfunc WithLogger(logger observe.Logger) Optionfunc WithMetrics(metrics observe.Metrics) Optionfunc WithName(name string) Optionfunc WithObservability(observability *observe.Observability) Optionfunc WithRecoveryTimeout(timeout time.Duration) Optionfunc WithStateChangeCallback(callback func(from, to State)) Optionfunc WithTracer(tracer observe.Tracer) Option// Example usage of State
var value State
// Initialize with appropriate valuetype State int32func (State) String() stringfunc NewCircuitOpenError(circuitName string) error// Example usage of NewCircuitOpenError
result := NewCircuitOpenError(/* parameters */)func NewCircuitTimeoutError(circuitName string) error// Example usage of NewCircuitTimeoutError
result := NewCircuitTimeoutError(/* parameters */)