13 : circuit_breaker_(
std::make_shared<circuit_breaker>(config))
18 : circuit_breaker_(
std::move(cb))
30 if (!enabled_.load(std::memory_order_acquire)) {
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");
43 "Circuit breaker is half-open and at capacity");
59 if (!
enabled_.load(std::memory_order_acquire)) {
72 return "circuit_breaker_policy";
77 return enabled_.load(std::memory_order_acquire);
82 enabled_.store(enabled, std::memory_order_release);
87 if (!
enabled_.load(std::memory_order_acquire)) {
92 return state != circuit_state::OPEN;
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.
std::atomic< bool > enabled_
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.
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.
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.