IMVEEDev Portal

Circuit Breaker

The Circuit Breaker prevents cascade failures in the Satellite Agent when the SOCSuite backend is unreachable.


Principle

Requests → Circuit Breaker → Backend
              │
              ├── Closed (normal): requests pass through
              ├── Open (failure): requests rejected immediately
              └── Half-Open (test): probe requests

Sequence on backend failure:

  1. Failure detected → circuit opens
  2. Data is saved to local queue (no data loss)
  3. Every 30s: probe request to test backend recovery
  4. Backend available → circuit closes
  5. Queue is drained (all stored data is resent)

Benefits

BenefitDescription
ResilienceAgent continues collecting data even when backend is down
No Data LossAll data is stored locally and automatically resent
Fast FailureNo wasted resources on failed requests
Auto RecoveryAutomatic recovery without manual intervention

Configuration

# Circuit breaker settings
export IMVEE_CB_MAX_REQUESTS=3       # Half-Open: max requests
export IMVEE_CB_INTERVAL=60s         # Error counting window
export IMVEE_CB_TIMEOUT=30s          # Wait before recovery test
export IMVEE_CB_FAILURE_RATIO=0.6    # 60% error rate → Open

# Queue settings
export IMVEE_QUEUE_DIR=/var/lib/imvee-satellite/queue
export IMVEE_QUEUE_MAX_SIZE=104857600  # 100 MB
export IMVEE_QUEUE_MAX_FILES=1000

Monitoring

# Circuit breaker status (via agent stats endpoint)
curl http://localhost:8888/stats | jq .circuit_breaker

# Watch queue size
watch -n 1 'curl -s http://localhost:8888/stats | jq .queue.size'

Test: Open the Circuit Breaker

# Stop backend
docker-compose stop api

# Agent logs: circuit opens
tail -f /var/log/imvee-satellite/agent.log
# → [CircuitBreaker] Backend unavailable, switching to offline mode

# Queue grows
curl http://localhost:8888/stats | jq .queue.size

Test: Close the Circuit Breaker

# Start backend
docker-compose start api

# After ~30s: circuit closes
tail -f /var/log/imvee-satellite/agent.log
# → [CircuitBreaker] Backend recovered, switching to online mode

# Queue is drained
watch -n 1 'curl -s http://localhost:8888/stats | jq .queue.size'

Implementation

The Circuit Breaker uses sony/gobreaker (Go library):

_, err := backendCircuitBreaker.Execute(func() (interface{}, error) {
    resp, err := client.Do(req)
    if resp.StatusCode >= 500 {
        // 5xx → circuit failure (can open the circuit)
        return nil, fmt.Errorf("backend error: %d", resp.StatusCode)
    }
    // 4xx → client error, does NOT open the circuit
    return resp, nil
})

if err != nil {
    // Circuit open or backend unavailable → save to queue
    queueManager.Enqueue(data, endpoint)
}

:::info [Source code] Implementation: satellite/internal/breaker/ :::


See Also

On this page