Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
protected_job.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
7
8#include <stdexcept>
9
10namespace kcenon::thread
11{
13 std::unique_ptr<job> inner,
14 std::shared_ptr<circuit_breaker> cb)
15 : job("protected_" + (inner ? inner->get_name() : "unknown"))
16 , inner_(std::move(inner))
17 , cb_(std::move(cb))
18 {
19 }
20
22
23 auto protected_job::do_work() -> common::VoidResult
24 {
25 if (!cb_)
26 {
27 return make_error_result(
29 "Circuit breaker is null");
30 }
31
32 if (!inner_)
33 {
34 return make_error_result(
36 "Inner job is null");
37 }
38
39 // Check if circuit breaker allows the request
40 if (!cb_->allow_request())
41 {
42 return make_error_result(
44 "Circuit breaker is open");
45 }
46
47 // Create guard for automatic failure recording
48 auto guard = cb_->make_guard();
49
50 try
51 {
52 auto result = inner_->do_work();
53 if (result.is_ok())
54 {
55 guard.record_success();
56 }
57 // If result is error, let guard destructor record failure
58 return result;
59 }
60 catch (...)
61 {
62 // Guard destructor will automatically record failure
63 throw;
64 }
65 }
66
67 auto protected_job::get_name() const -> std::string
68 {
69 return job::get_name();
70 }
71
72} // namespace kcenon::thread
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
auto get_name(void) const -> std::string
Retrieves the name of this job.
Definition job.cpp:112
~protected_job() override
Destructor.
auto do_work() -> common::VoidResult override
Executes the wrapped job with circuit breaker protection.
auto get_name() const -> std::string
Gets the name of this job.
protected_job(std::unique_ptr< job > inner, std::shared_ptr< circuit_breaker > cb)
Constructs a protected job wrapper.
A template class representing either a value or an error.
bool is_ok() const noexcept
Checks if the result is successful.
Error codes and utilities for 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.
STL namespace.
Job wrapper integrating circuit breaker protection.