|
QGroundControl
Ground Control Station for MAVLink Drones
|
#include <CircuitBreakerState.h>
Inheritance diagram for CircuitBreakerState:
Collaboration diagram for CircuitBreakerState:Public Types | |
| enum class | State { Closed , Open , HalfOpen } |
| using | Action = std::function< bool()> |
Public Types inherited from QGCAbstractState | |
| using | EntryCallback = std::function< void()> |
| using | ExitCallback = std::function< void()> |
| using | EventHandler = std::function< bool(QEvent *)> |
Signals | |
| void | tripped () |
| Emitted when the circuit trips (too many failures) | |
| void | circuitReset () |
| Emitted when the circuit resets (after successful half-open test) | |
| void | succeeded () |
| Emitted when action succeeds. | |
| void | failed () |
| Emitted when action fails (or circuit is open) | |
Signals inherited from QGCAbstractState | |
| void | advance () |
| void | error () |
Public Member Functions | |
| CircuitBreakerState (const QString &stateName, QState *parent, Action action, int failureThreshold=5, int resetTimeoutMsecs=30000) | |
| State | circuitState () const |
| Get the current circuit state. | |
| int | failureCount () const |
| Get the current failure count. | |
| int | failureThreshold () const |
| Get the failure threshold. | |
| void | reset () |
| Manually reset the circuit breaker. | |
| bool | isTripped () const |
| Check if the circuit is currently tripped (open) | |
Public Member Functions inherited from QGCState | |
| QGCState (const QString &stateName, QState *parentState) | |
| template<typename PointerToMemberFunction > | |
| QSignalTransition * | addThisTransition (PointerToMemberFunction signal, QAbstractState *target) |
| Simpler version of QState::addTransition which assumes the sender is this. | |
| void | setLocalErrorState (QAbstractState *errorState) |
| QAbstractState * | localErrorState () const |
| Get the per-state error state (nullptr if using global) | |
| void | setProperty (QObject *object, const char *name, const QVariant &value) |
| void | setEnabled (QObject *object, bool enabled) |
| Convenience overload for setting enabled state on widgets/controls. | |
| void | setVisible (QObject *object, bool visible) |
| Convenience overload for setting visible state on widgets/controls. | |
Public Member Functions inherited from QGCAbstractState | |
| QGCAbstractState (const QString &stateName, QState *parent) | |
| QGCStateMachine * | machine () const |
| Vehicle * | vehicle () const |
| QString | stateName () const |
| class StateContext * | context () const |
| void | setOnEntry (EntryCallback callback) |
| Set a callback to be invoked when the state is entered. | |
| void | setOnExit (ExitCallback callback) |
| Set a callback to be invoked when the state is exited. | |
| void | setCallbacks (EntryCallback onEntry, ExitCallback onExit=nullptr) |
| Set both entry and exit callbacks. | |
| void | setEventHandler (EventHandler handler) |
| Set a custom event handler for this state. | |
Protected Member Functions | |
| void | onEnter () override |
| Override to perform actions on state entry. | |
Protected Member Functions inherited from QGCAbstractState | |
| virtual void | onLeave () |
| Override to perform actions on state exit. | |
| void | onEntry (QEvent *event) override |
| void | onExit (QEvent *event) override |
| bool | event (QEvent *event) override |
A state that implements the circuit breaker pattern.
After a threshold of failures, the circuit "trips" and immediately fails without attempting the action for a reset timeout period. This prevents cascading failures when an external service is down.
States:
Example usage:
Definition at line 31 of file CircuitBreakerState.h.
| using CircuitBreakerState::Action = std::function<bool()> |
Definition at line 37 of file CircuitBreakerState.h.
|
strong |
| Enumerator | |
|---|---|
| Closed | Normal operation. |
| Open | Tripped, failing fast. |
| HalfOpen | Testing if service recovered. |
Definition at line 39 of file CircuitBreakerState.h.
| CircuitBreakerState::CircuitBreakerState | ( | const QString & | stateName, |
| QState * | parent, | ||
| Action | action, | ||
| int | failureThreshold = 5, |
||
| int | resetTimeoutMsecs = 30000 |
||
| ) |
| stateName | Name for logging |
| parent | Parent state |
| action | Action to execute (returns true on success) |
| failureThreshold | Number of failures before tripping |
| resetTimeoutMsecs | Time before attempting recovery |
Definition at line 4 of file CircuitBreakerState.cc.
|
signal |
Emitted when the circuit resets (after successful half-open test)
Referenced by reset().
|
inline |
Get the current circuit state.
Definition at line 56 of file CircuitBreakerState.h.
|
signal |
Emitted when action fails (or circuit is open)
Referenced by onEnter().
|
inline |
Get the current failure count.
Definition at line 59 of file CircuitBreakerState.h.
|
inline |
Get the failure threshold.
Definition at line 62 of file CircuitBreakerState.h.
|
inline |
Check if the circuit is currently tripped (open)
Definition at line 68 of file CircuitBreakerState.h.
References Open.
|
overrideprotectedvirtual |
Override to perform actions on state entry.
Reimplemented from QGCAbstractState.
Definition at line 22 of file CircuitBreakerState.cc.
References QGCAbstractState::advance(), QGCAbstractState::error(), failed(), HalfOpen, Open, reset(), QGCAbstractState::stateName(), succeeded(), and tripped().
| void CircuitBreakerState::reset | ( | ) |
Manually reset the circuit breaker.
Definition at line 14 of file CircuitBreakerState.cc.
References circuitReset(), Closed, and QGCAbstractState::stateName().
Referenced by onEnter().
|
signal |
Emitted when action succeeds.
Referenced by onEnter().
|
signal |
Emitted when the circuit trips (too many failures)
Referenced by onEnter().