Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::circuit_breaker_policy Class Reference

Pool policy that implements circuit breaker pattern for failure protection. More...

#include <circuit_breaker_policy.h>

Inheritance diagram for kcenon::thread::circuit_breaker_policy:
Inheritance graph
Collaboration diagram for kcenon::thread::circuit_breaker_policy:
Collaboration graph

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_policyoperator= (const circuit_breaker_policy &)=delete
 
 circuit_breaker_policy (circuit_breaker_policy &&)=delete
 
circuit_breaker_policyoperator= (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}
 

Detailed Description

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.

Circuit Breaker Pattern

The circuit breaker monitors job failures and automatically opens when a threshold is exceeded, preventing cascading failures:

  • CLOSED: Normal operation, all jobs allowed
  • OPEN: Failure threshold exceeded, jobs rejected immediately
  • HALF_OPEN: Testing recovery, limited jobs allowed

Thread Safety

All methods are thread-safe and can be called from any thread.

Usage Example

circuit_breaker_config config;
config.failure_threshold = 5;
config.open_duration = std::chrono::seconds{30};
auto cb_policy = std::make_unique<circuit_breaker_policy>(config);
pool->add_policy(std::move(cb_policy));
// Now all jobs are protected by the circuit breaker
// Jobs will be rejected when circuit is open
See also
pool_policy
circuit_breaker
circuit_breaker_config

Definition at line 66 of file circuit_breaker_policy.h.

Constructor & Destructor Documentation

◆ circuit_breaker_policy() [1/4]

kcenon::thread::circuit_breaker_policy::circuit_breaker_policy ( const circuit_breaker_config & config = {})
explicit

Constructs a circuit breaker policy with the given configuration.

Parameters
configCircuit breaker configuration.

Definition at line 12 of file circuit_breaker_policy.cpp.

13 : circuit_breaker_(std::make_shared<circuit_breaker>(config))
14{
15}
std::shared_ptr< circuit_breaker > circuit_breaker_

◆ circuit_breaker_policy() [2/4]

kcenon::thread::circuit_breaker_policy::circuit_breaker_policy ( std::shared_ptr< circuit_breaker > cb)
explicit

Constructs a circuit breaker policy with an existing circuit breaker.

Parameters
cbShared 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.

18 : circuit_breaker_(std::move(cb))
19{
20 if (!circuit_breaker_) {
21 // Create default circuit breaker if nullptr passed
22 circuit_breaker_ = std::make_shared<circuit_breaker>();
23 }
24}

References circuit_breaker_.

◆ ~circuit_breaker_policy()

kcenon::thread::circuit_breaker_policy::~circuit_breaker_policy ( )
overridedefault

Destructor.

◆ circuit_breaker_policy() [3/4]

kcenon::thread::circuit_breaker_policy::circuit_breaker_policy ( const circuit_breaker_policy & )
delete

◆ circuit_breaker_policy() [4/4]

kcenon::thread::circuit_breaker_policy::circuit_breaker_policy ( circuit_breaker_policy && )
delete

Member Function Documentation

◆ get_circuit_breaker()

auto kcenon::thread::circuit_breaker_policy::get_circuit_breaker ( ) const -> std::shared_ptr<circuit_breaker>
nodiscard

Gets the underlying circuit breaker.

Returns
Shared pointer to the 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.

101{
102 return circuit_breaker_;
103}

References circuit_breaker_.

◆ get_name()

auto kcenon::thread::circuit_breaker_policy::get_name ( ) const -> std::string
nodiscardoverridevirtual

Gets the policy name.

Returns
"circuit_breaker_policy"

Implements kcenon::thread::pool_policy.

Definition at line 70 of file circuit_breaker_policy.cpp.

71{
72 return "circuit_breaker_policy";
73}

◆ get_state()

auto kcenon::thread::circuit_breaker_policy::get_state ( ) const -> circuit_state
nodiscard

Gets the current circuit state.

Returns
Current circuit_state.

Definition at line 95 of file circuit_breaker_policy.cpp.

96{
97 return circuit_breaker_->get_state();
98}

References circuit_breaker_.

◆ is_accepting_work()

auto kcenon::thread::circuit_breaker_policy::is_accepting_work ( ) const -> bool
nodiscard

Checks if the circuit is accepting work.

Returns
True if circuit is closed or half-open with capacity.

Definition at line 85 of file circuit_breaker_policy.cpp.

86{
87 if (!enabled_.load(std::memory_order_acquire)) {
88 return true; // If disabled, always accept
89 }
90
91 auto state = circuit_breaker_->get_state();
92 return state != circuit_state::OPEN;
93}

References circuit_breaker_, and enabled_.

◆ is_enabled()

auto kcenon::thread::circuit_breaker_policy::is_enabled ( ) const -> bool
nodiscardoverridevirtual

Checks if the policy is enabled.

Returns
True if enabled.

Reimplemented from kcenon::thread::pool_policy.

Definition at line 75 of file circuit_breaker_policy.cpp.

76{
77 return enabled_.load(std::memory_order_acquire);
78}

References enabled_.

◆ on_enqueue()

auto kcenon::thread::circuit_breaker_policy::on_enqueue ( job & j) -> common::VoidResult
overridevirtual

Checks circuit state before allowing job enqueue.

Parameters
jReference to the job being enqueued.
Returns
common::ok() if allowed, error if circuit is open.

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.

27{
28 (void)j; // Job not used, policy applies to all jobs equally
29
30 if (!enabled_.load(std::memory_order_acquire)) {
31 return common::ok();
32 }
33
34 // Check if circuit breaker allows the request
35 if (!circuit_breaker_->allow_request()) {
36 auto state = circuit_breaker_->get_state();
37 if (state == circuit_state::OPEN) {
39 "Circuit breaker is open, job rejected");
40 }
41 // half_open state but at capacity
43 "Circuit breaker is half-open and at capacity");
44 }
45
46 return common::ok();
47}
common::VoidResult make_error_result(error_code code, const std::string &message="")
Create a common::VoidResult error from a thread::error_code.
@ circuit_half_open
Circuit breaker is in half-open state.
@ circuit_open
Circuit breaker is open, request rejected.

References kcenon::thread::circuit_half_open, kcenon::thread::circuit_open, and kcenon::thread::make_error_result().

Here is the call graph for this function:

◆ on_job_complete()

void kcenon::thread::circuit_breaker_policy::on_job_complete ( job & j,
bool success,
const std::exception * error = nullptr )
overridevirtual

Records job completion in the circuit breaker.

Parameters
jReference to the completed job.
successTrue if job succeeded.
errorException 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.

56{
57 (void)j; // Job not used, policy applies to all jobs equally
58
59 if (!enabled_.load(std::memory_order_acquire)) {
60 return;
61 }
62
63 if (success) {
64 circuit_breaker_->record_success();
65 } else {
66 circuit_breaker_->record_failure(error);
67 }
68}
@ error
Error events that might still allow continuation.

References circuit_breaker_, enabled_, and kcenon::thread::success.

◆ on_job_start()

void kcenon::thread::circuit_breaker_policy::on_job_start ( job & j)
overridevirtual

Called when job starts executing.

Parameters
jReference 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.

50{
51 (void)j; // Currently no action needed on job start
52 // Could be extended to track timing in the future
53}

◆ operator=() [1/2]

circuit_breaker_policy & kcenon::thread::circuit_breaker_policy::operator= ( circuit_breaker_policy && )
delete

◆ operator=() [2/2]

circuit_breaker_policy & kcenon::thread::circuit_breaker_policy::operator= ( const circuit_breaker_policy & )
delete

◆ set_enabled()

void kcenon::thread::circuit_breaker_policy::set_enabled ( bool enabled)
overridevirtual

Enables or disables the policy.

Parameters
enabledWhether to enable.

Reimplemented from kcenon::thread::pool_policy.

Definition at line 80 of file circuit_breaker_policy.cpp.

81{
82 enabled_.store(enabled, std::memory_order_release);
83}

References enabled_.

Member Data Documentation

◆ circuit_breaker_

std::shared_ptr<circuit_breaker> kcenon::thread::circuit_breaker_policy::circuit_breaker_
private

◆ enabled_

std::atomic<bool> kcenon::thread::circuit_breaker_policy::enabled_ {true}
private

Definition at line 173 of file circuit_breaker_policy.h.

173{true};

Referenced by is_accepting_work(), is_enabled(), on_job_complete(), and set_enabled().


The documentation for this class was generated from the following files: