rotor
Event loop friendly C++ actor micro-framework
 
Loading...
Searching...
No Matches
child_info.h
1#pragma once
2
3//
4// Copyright (c) 2022 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
5//
6// Distributed under the MIT Software License
7//
8
9#include "rotor/forward.hpp"
10#include "rotor/policy.h"
11#include <variant>
12
13#if defined(_MSC_VER)
14#pragma warning(push)
15#pragma warning(disable : 4251)
16#endif
17
18namespace rotor::detail {
19
20enum class shutdown_state_t { none, sent, confirmed };
21
22namespace demand {
23struct no {};
24struct now {};
26} // namespace demand
27
28using spawn_demand_t = std::variant<demand::no, demand::now, demand::escalate_failure, pt::time_duration>;
29
36struct ROTOR_API child_info_t : boost::intrusive_ref_counter<child_info_t, boost::thread_unsafe_counter> {
38 using clock_t = pt::microsec_clock;
39
41 template <typename Factory>
42 child_info_t(address_ptr_t address_, Factory &&factory_, restart_policy_t policy_ = restart_policy_t::normal_only,
43 const pt::time_duration &restart_period_ = pt::seconds{15}, size_t max_attempts_ = 0,
44 bool escalate_failure_ = false) noexcept
45 : address{std::move(address_)}, factory{std::forward<Factory>(factory_)}, policy{policy_},
46 restart_period{restart_period_}, max_attempts{max_attempts_}, escalate_failure{escalate_failure_} {}
47
49 template <typename Factory, typename Actor>
50 child_info_t(address_ptr_t address_, Factory &&factory_, Actor &&actor_) noexcept
51 : child_info_t(std::move(address_), std::forward<Factory>(factory_)) {
52 actor = std::forward<Actor>(actor_);
53 if (actor) {
54 spawn_attempt();
55 }
56 }
57
64 void spawn_attempt() noexcept;
65
73 spawn_demand_t next_spawn(bool abnormal_shutdown) noexcept;
74
77
79 factory_t factory;
80
83
86
88 pt::time_duration restart_period;
89
91 shutdown_state_t shutdown = shutdown_state_t::none;
92
94 size_t max_attempts;
95
97 size_t attempts = 0;
98
100 bool active = true;
101
103 bool escalate_failure = false;
104
106 request_id_t timer_id{0};
107
110
112 bool initialized = false;
113
115 bool started = false;
116};
117
118using child_info_ptr_t = boost::intrusive_ptr<child_info_t>;
119
120} // namespace rotor::detail
121
122#if defined(_MSC_VER)
123#pragma warning(pop)
124#endif
intrusive_ptr_t< address_t > address_ptr_t
intrusive pointer for address
Definition address.hpp:57
restart_policy_t
spawner's actor restart policy
Definition policy.h:23
intrusive_ptr_t< actor_base_t > actor_ptr_t
intrusive pointer for actor
Definition forward.hpp:23
std::size_t request_id_t
timer identifier type in the scope of the actor
Definition forward.hpp:34
std::function< actor_ptr_t(supervisor_t &, const address_ptr_t &)> factory_t
factory which allows to create actors lazily or on demand
Definition forward.hpp:45
Child actor runtime housekeeping information.
Definition child_info.h:36
pt::microsec_clock clock_t
an alias for used microsec clock
Definition child_info.h:38
child_info_t(address_ptr_t address_, Factory &&factory_, restart_policy_t policy_=restart_policy_t::normal_only, const pt::time_duration &restart_period_=pt::seconds{15}, size_t max_attempts_=0, bool escalate_failure_=false) noexcept
CTOR for spawner-instantiated actor.
Definition child_info.h:42
pt::ptime last_instantiation
when an actor was successfully spawned
Definition child_info.h:109
void spawn_attempt() noexcept
recalculates the state upon spawn_attempt
child_info_t(address_ptr_t address_, Factory &&factory_, Actor &&actor_) noexcept
CTOR for manually instantiated actor.
Definition child_info.h:50
Definition child_info.h:25
Definition child_info.h:23
Definition child_info.h:24