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 requestsSequence on backend failure:
- Failure detected → circuit opens
- Data is saved to local queue (no data loss)
- Every 30s: probe request to test backend recovery
- Backend available → circuit closes
- Queue is drained (all stored data is resent)
Benefits
| Benefit | Description |
|---|---|
| Resilience | Agent continues collecting data even when backend is down |
| No Data Loss | All data is stored locally and automatically resent |
| Fast Failure | No wasted resources on failed requests |
| Auto Recovery | Automatic 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=1000Monitoring
# 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.sizeTest: 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
- Architecture — Agent architecture and data flow
- Configuration — Environment variables for batch settings
- Troubleshooting — Circuit breaker diagnostics