|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Pool policy that implements circuit breaker pattern for failure protection. More...
#include <circuit_breaker_policy.h>


Public Member Functions | |
| circuit_breaker_policy (const circuit_breaker_config &config={}) | |
| Constructs a circuit breaker policy with the given configuration. | |
| circuit_breaker_policy (std::shared_ptr< circuit_breaker > cb) | |
| Constructs a circuit breaker policy with an existing circuit breaker. | |
| ~circuit_breaker_policy () override=default | |
| Destructor. | |
| circuit_breaker_policy (const circuit_breaker_policy &)=delete | |
| circuit_breaker_policy & | operator= (const circuit_breaker_policy &)=delete |
| circuit_breaker_policy (circuit_breaker_policy &&)=delete | |
| circuit_breaker_policy & | operator= (circuit_breaker_policy &&)=delete |
| auto | on_enqueue (job &j) -> common::VoidResult override |
| Checks circuit state before allowing job enqueue. | |
| void | on_job_start (job &j) override |
| Called when job starts executing. | |
| void | on_job_complete (job &j, bool success, const std::exception *error=nullptr) override |
| Records job completion in the circuit breaker. | |
| auto | get_name () const -> std::string override |
| Gets the policy name. | |
| auto | is_enabled () const -> bool override |
| Checks if the policy is enabled. | |
| void | set_enabled (bool enabled) override |
| Enables or disables the policy. | |
| auto | is_accepting_work () const -> bool |
| Checks if the circuit is accepting work. | |
| auto | get_state () const -> circuit_state |
| Gets the current circuit state. | |
| auto | get_circuit_breaker () const -> std::shared_ptr< circuit_breaker > |
| Gets the underlying circuit breaker. | |
Public Member Functions inherited from kcenon::thread::pool_policy | |
| virtual | ~pool_policy ()=default |
| Virtual destructor for proper cleanup. | |
Private Attributes | |
| std::shared_ptr< circuit_breaker > | circuit_breaker_ |
| std::atomic< bool > | enabled_ {true} |
Pool policy that implements circuit breaker pattern for failure protection.
This policy wraps the circuit breaker functionality as a composable pool policy, enabling circuit breaker protection without modifying the thread_pool class.
The circuit breaker monitors job failures and automatically opens when a threshold is exceeded, preventing cascading failures:
All methods are thread-safe and can be called from any thread.
Definition at line 66 of file circuit_breaker_policy.h.
|
explicit |
Constructs a circuit breaker policy with the given configuration.
| config | Circuit breaker configuration. |
Definition at line 12 of file circuit_breaker_policy.cpp.
|
explicit |
Constructs a circuit breaker policy with an existing circuit breaker.
| cb | Shared pointer to an existing circuit breaker. |
This allows sharing a circuit breaker across multiple pools or components.
Definition at line 17 of file circuit_breaker_policy.cpp.
References circuit_breaker_.
|
overridedefault |
Destructor.
|
delete |
|
delete |
|
nodiscard |
Gets the underlying circuit breaker.
Useful for sharing the circuit breaker with other components or for advanced circuit breaker operations.
Definition at line 100 of file circuit_breaker_policy.cpp.
References circuit_breaker_.
|
nodiscardoverridevirtual |
Gets the policy name.
Implements kcenon::thread::pool_policy.
Definition at line 70 of file circuit_breaker_policy.cpp.
|
nodiscard |
Gets the current circuit state.
Definition at line 95 of file circuit_breaker_policy.cpp.
References circuit_breaker_.
|
nodiscard |
Checks if the circuit is accepting work.
Definition at line 85 of file circuit_breaker_policy.cpp.
References circuit_breaker_, and enabled_.
|
nodiscardoverridevirtual |
Checks if the policy is enabled.
Reimplemented from kcenon::thread::pool_policy.
Definition at line 75 of file circuit_breaker_policy.cpp.
References enabled_.
|
overridevirtual |
Checks circuit state before allowing job enqueue.
| j | Reference to the job being enqueued. |
When the circuit is open, jobs are rejected immediately with an error indicating the circuit breaker state.
Implements kcenon::thread::pool_policy.
Definition at line 26 of file circuit_breaker_policy.cpp.
References kcenon::thread::circuit_half_open, kcenon::thread::circuit_open, and kcenon::thread::make_error_result().

|
overridevirtual |
Records job completion in the circuit breaker.
| j | Reference to the completed job. |
| success | True if job succeeded. |
| error | Exception pointer if job failed. |
This updates the circuit breaker state based on success/failure.
Implements kcenon::thread::pool_policy.
Definition at line 55 of file circuit_breaker_policy.cpp.
References circuit_breaker_, enabled_, and kcenon::thread::success.
|
overridevirtual |
Called when job starts executing.
| j | Reference to the job. |
Records the start time for latency tracking.
Implements kcenon::thread::pool_policy.
Definition at line 49 of file circuit_breaker_policy.cpp.
|
delete |
|
delete |
|
overridevirtual |
Enables or disables the policy.
| enabled | Whether to enable. |
Reimplemented from kcenon::thread::pool_policy.
Definition at line 80 of file circuit_breaker_policy.cpp.
References enabled_.
|
private |
Definition at line 172 of file circuit_breaker_policy.h.
Referenced by circuit_breaker_policy(), get_circuit_breaker(), get_state(), is_accepting_work(), and on_job_complete().
|
private |
Definition at line 173 of file circuit_breaker_policy.h.
Referenced by is_accepting_work(), is_enabled(), on_job_complete(), and set_enabled().