Skip to content

[Feature 09] Circuit Breaker #28

@onlyhyde

Description

@onlyhyde

Overview

Implement Circuit Breaker pattern for RPC resilience - fast fail and automatic recovery.

Implementation Details

Files:

  • internal/circuitbreaker/breaker.go - Circuit breaker implementation
  • internal/circuitbreaker/client.go - Wrapped ethclient

Circuit Breaker States

    ┌─────────┐
    │ CLOSED  │◄──────────────────────┐
    └────┬────┘                       │
         │                            │
   failures >= threshold         successes >= threshold
         │                            │
         ▼                            │
    ┌─────────┐    timeout       ┌────┴────┐
    │  OPEN   │─────────────────►│HALF-OPEN│
    └─────────┘                  └────┬────┘
         ▲                            │
         │         failure            │
         └────────────────────────────┘

Configuration

type Config struct {
    FailureThreshold    int           // Default: 5
    SuccessThreshold    int           // Default: 3
    Timeout             time.Duration // Default: 30s
    HalfOpenMaxRequests int           // Default: 1
    OnStateChange       func(from, to State)
}

Wrapped Client

type Client struct {
    client  *ethclient.Client
    breaker *CircuitBreaker
}

func (c *Client) ChainID(ctx) (*big.Int, error)
func (c *Client) BalanceAt(ctx, account, blockNumber) (*big.Int, error)
func (c *Client) State() State

Execute Pattern

func (cb *CircuitBreaker) Execute(ctx, fn func() error) error {
    if !cb.allowRequest() {
        return ErrCircuitOpen
    }
    err := fn()
    cb.recordResult(err)
    return err
}

Acceptance Criteria

  • Circuit breaker with three states (Closed, Open, Half-Open)
  • Configurable thresholds and timeout
  • State transition callbacks
  • Wrapped ethclient methods
  • Thread-safe operations
  • Unit tests for state transitions
  • Integration tests

Dependencies

Branch

feature/circuit-breaker

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions