rotor est un microframework d'acteur C ++ discret similaire à ses frères aînés - caf et sobjectizer . Dans la nouvelle version, le noyau interne a été complètement repensé à l'aide de mécanismes de plugins , ce qui affecte le cycle de vie des acteurs.
Lier les acteurs
, . . ( , ). , , (1); , A
B
, , B
M
.
v0.09
/, , . . , , , . v0.09
A
B
, (link), , .
:
namespace r = rotor;
void some_actor_t::on_start() noexcept override {
request<payload::link_request_t>(b_address).send(timeout);
}
void some_actor_t::on_link_response(r::message::link_response_t &response) noexcept {
auto& ec = message.payload.ec;
if (!ec) {
//
}
}
, … . A
, . . some_actor_t
. , :
namespace r = rotor;
void some_actor_t::configure(r::plugin::plugin_base_t &plugin) noexcept override {
plugin.with_casted<r::plugin::link_client_plugin_t>(
[&](auto &p) {
p.link(B1_address);
p.link(B2_address);
}
);
}
, link_client_plugin_t
actor_base_t
. , , , . . : 1) ( — )? 2) , ("") ? 3) , -?
.
(, state) : new
(ctor) -> initializing
-> initialized
-> operational
-> shutting down
-> shut down
.
, operational
, , .
(I-, . . initializing
-> initialized
), : , , . rotor', I- , . . , (2).
(S-, . . shutting down
-> shut down
) I-, . . , , .
, (composability) , - (. Trees of Supervisors). , , I-
S-
, . — (operational
), (shut down
).
( . , — ).
rotor . caf. , sobjectizer' shutdown helper, S-
; , , , "" () , Environment
' stop
. sobjectizer' ( ), I-
S-
rotor'a
, , -. rotor' .
rotor' , I-
(S-
) () , , , , . , . , , .
rotor'? — , . . init_shutdown
, () , . . , (); , , child_manager
, , , , . . , , , .
, , link_client_plugin_t
:
- : () ? :
initializing
(shutting down
). - : , ? : . . , -; , , , . . .
- : , , , -? : - , , - (3).
, (event loop), rotor', TCP-, . db_actor_t
, acceptor_t
. :
namespace r = rotor;
struct db_actor_t: r::actor_base_t {
struct resource {
static const constexpr r::plugin::resource_id_t db_connection = 0;
}
void configure(r::plugin::plugin_base_t &plugin) noexcept override {
plugin.with_casted<r::plugin::registry_plugin_t>([this](auto &p) {
p.register_name("service::database", this->get_address())
});
plugin.with_casted<r::plugin::resources_plugin_t>([this](auto &) {
resources->acquire(resource::db_connection);
//
});
}
void on_db_connection_success() {
resources->release(resource::db_connection);
...
}
void on_db_disconnected() {
resources->release(resource::db_connection);
}
void shutdown_start() noexcept override {
r::actor_base_t::shutdown_start();
resources->acquire(resource::db_connection);
//
}
};
resource
. , 0
. , , registry_plugin_t
, ( ). resources_plugin_t
"" , . , db_actor_t
. S-
: , ; (4).
, , :
namespace r = rotor;
struct acceptor_actor_t: r::actor_base_t {
r::address_ptr_t db_addr;
void configure(r::plugin::plugin_base_t &plugin) noexcept override {
plugin.with_casted<r::plugin::registry_plugin_t>([](auto &p) {
p.discover_name("service::database", db_addr, true).link();
});
}
void on_start() noexcept override {
r::actor_base_t::on_start();
// , :
// asio::ip::tcp::acceptor.async_accept(...);
}
void on_new_client(client_t& client) {
// send<message::log_client_t>(db_addr, client)
}
};
, configure
. registry_plugin_t
, service::database
, db_actor_t
db_addr
, . service::database
, acceptor_actor_t
(. . on_start
). , .
, , . . rotor'. (payload), , .
main.cpp
; , boost::asio
.
namespace asio = boost::asio;
namespace r = rotor;
...
asio::io_context io_context;
auto system_context = rotor::asio::system_context_asio_t(io_context);
auto strand = std::make_shared<asio::io_context::strand>(io_context);
auto timeout = r::pt::milliseconds(100);
auto sup = system_context->create_supervisor<r::asio::supervisor_asio_t>()
.timeout(timeout)
.strand(strand)
.create_registry()
.finish();
sup->create_actor<db_actor_t>().timeout(timeout).finish();
sup->create_actor<acceptor_actor_t>().timeout(timeout).finish();
sup->start();
io_context.run();
, rotor' builder
. sup
, 3 : (db_actor_t
acceptor_actor_t
) -. , , . . ( ).
" " , , . , v0.09
.
: , , , (5). , - , , . get_registry_address()
.
rotor' . : link_client_plugin_t
, , registry_plugin_t
, , resources_plugin_t
, () .
.
P.S. Crazy Panda , sobjectizer' .
(1) , , (UB).
(2) , , , . . operational
.
(3) , , ? , system_context_t::on_error(const std::error_code&)
, std::terminate()
. , , - .
(4) registry_plugin_t
, .
(5) , . API , , , . , API rotor'.