Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
circuit_breaker_policy.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
8
9namespace kcenon::thread
10{
11
12circuit_breaker_policy::circuit_breaker_policy(const circuit_breaker_config& config)
13 : circuit_breaker_(std::make_shared<circuit_breaker>(config))
14{
15}
16
17circuit_breaker_policy::circuit_breaker_policy(std::shared_ptr<circuit_breaker> cb)
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}
25
26auto circuit_breaker_policy::on_enqueue(job& j) -> common::VoidResult
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}
48
50{
51 (void)j; // Currently no action needed on job start
52 // Could be extended to track timing in the future
53}
54
55void circuit_breaker_policy::on_job_complete(job& j, bool success, const std::exception* error)
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}
69
71{
72 return "circuit_breaker_policy";
73}
74
76{
77 return enabled_.load(std::memory_order_acquire);
78}
79
81{
82 enabled_.store(enabled, std::memory_order_release);
83}
84
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}
94
95auto circuit_breaker_policy::get_state() const -> circuit_state
96{
97 return circuit_breaker_->get_state();
98}
99
100auto circuit_breaker_policy::get_circuit_breaker() const -> std::shared_ptr<circuit_breaker>
101{
102 return circuit_breaker_;
103}
104
105} // namespace kcenon::thread
Pool policy implementing circuit breaker pattern for failure protection.
void on_job_complete(job &j, bool success, const std::exception *error=nullptr) override
Records job completion in the circuit breaker.
auto is_enabled() const -> bool override
Checks if the policy is enabled.
auto get_circuit_breaker() const -> std::shared_ptr< circuit_breaker >
Gets the underlying circuit breaker.
auto is_accepting_work() const -> bool
Checks if the circuit is accepting work.
auto on_enqueue(job &j) -> common::VoidResult override
Checks circuit state before allowing job enqueue.
void set_enabled(bool enabled) override
Enables or disables the policy.
std::shared_ptr< circuit_breaker > circuit_breaker_
auto get_name() const -> std::string override
Gets the policy name.
circuit_breaker_policy(const circuit_breaker_config &config={})
Constructs a circuit breaker policy with the given configuration.
auto get_state() const -> circuit_state
Gets the current circuit state.
void on_job_start(job &j) override
Called when job starts executing.
Represents an error in the thread system.
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
Error codes and utilities for the thread system.
Base job class for schedulable work units in the thread system.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
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.
STL namespace.