Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
circuit_breaker_policy.h
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
12#pragma once
13
14#include "pool_policy.h"
15#include <kcenon/common/resilience/circuit_breaker.h>
16#include <kcenon/common/resilience/circuit_breaker_config.h>
17#include <kcenon/common/resilience/circuit_state.h>
18
19#include <atomic>
20#include <memory>
21#include <string>
22
23namespace kcenon::thread
24{
25 // Import resilience types from common_system
26 using common::resilience::circuit_breaker;
27 using common::resilience::circuit_breaker_config;
28 using common::resilience::circuit_state;
29
67 {
68 public:
73 explicit circuit_breaker_policy(const circuit_breaker_config& config = {});
74
81 explicit circuit_breaker_policy(std::shared_ptr<circuit_breaker> cb);
82
86 ~circuit_breaker_policy() override = default;
87
88 // Non-copyable
91
92 // Non-movable (std::atomic is not movable)
95
96 // ============================================
97 // pool_policy Interface
98 // ============================================
99
108 auto on_enqueue(job& j) -> common::VoidResult override;
109
116 void on_job_start(job& j) override;
117
126 void on_job_complete(job& j, bool success, const std::exception* error = nullptr) override;
127
132 [[nodiscard]] auto get_name() const -> std::string override;
133
138 [[nodiscard]] auto is_enabled() const -> bool override;
139
144 void set_enabled(bool enabled) override;
145
146 // ============================================
147 // Circuit Breaker Specific Methods
148 // ============================================
149
154 [[nodiscard]] auto is_accepting_work() const -> bool;
155
160 [[nodiscard]] auto get_state() const -> circuit_state;
161
169 [[nodiscard]] auto get_circuit_breaker() const -> std::shared_ptr<circuit_breaker>;
170
171 private:
172 std::shared_ptr<circuit_breaker> circuit_breaker_;
173 std::atomic<bool> enabled_{true};
174 };
175
176} // namespace kcenon::thread
Pool policy that implements circuit breaker pattern for failure protection.
circuit_breaker_policy & operator=(const circuit_breaker_policy &)=delete
void on_job_complete(job &j, bool success, const std::exception *error=nullptr) override
Records job completion in the circuit breaker.
~circuit_breaker_policy() override=default
Destructor.
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.
circuit_breaker_policy & operator=(circuit_breaker_policy &&)=delete
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_
circuit_breaker_policy(circuit_breaker_policy &&)=delete
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.
circuit_breaker_policy(const circuit_breaker_policy &)=delete
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
Base interface for thread pool policies.
Definition pool_policy.h:81
Core threading foundation of the thread system library.
Definition thread_impl.h:17
STL namespace.
Base interface for extensible thread pool behavior policies.