escalate-failure.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // Copyright (c) 2022 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  3. //
  4. // Distributed under the MIT Software License
  5. //
  6. /*
  7. *
  8. * This is an example of failure escalation feature, i.e. when an actor
  9. * terminates with failure, it will trigger its supervisor to shutdown
  10. * with failure too
  11. *
  12. */
  13. #include "rotor.hpp"
  14. #include "dummy_supervisor.h"
  15. #include <iostream>
  16. #include <random>
  17. namespace r = rotor;
  18. namespace payload {
  19. struct pong_t {};
  20. struct ping_t {
  21. using response_t = pong_t;
  22. };
  23. } // namespace payload
  24. namespace message {
  25. using ping_t = rotor::request_traits_t<payload::ping_t>::request::message_t;
  26. using pong_t = rotor::request_traits_t<payload::ping_t>::response::message_t;
  27. } // namespace message
  28. struct pinger_t : public rotor::actor_base_t {
  29. using rotor::actor_base_t::actor_base_t;
  30. void configure(rotor::plugin::plugin_base_t &plugin) noexcept override {
  31. rotor::actor_base_t::configure(plugin);
  32. plugin.with_casted<r::plugin::address_maker_plugin_t>([&](auto &p) { p.set_identity("pinger", true); });
  33. plugin.with_casted<rotor::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&pinger_t::on_pong); });
  34. plugin.with_casted<rotor::plugin::registry_plugin_t>(
  35. [&](auto &p) { p.discover_name("ponger", ponger, true).link(); });
  36. }
  37. void on_start() noexcept override {
  38. rotor::actor_base_t::on_start();
  39. request<payload::ping_t>(ponger).send(init_timeout);
  40. }
  41. void on_pong(message::pong_t &reply) noexcept {
  42. auto &ee = reply.payload.ee;
  43. if (ee) {
  44. std::cout << "err: " << ee->message() << "\n";
  45. return do_shutdown(ee);
  46. }
  47. std::cout << "pong received\n";
  48. do_shutdown();
  49. }
  50. rotor::address_ptr_t ponger;
  51. };
  52. struct ponger_t : public rotor::actor_base_t {
  53. using rotor::actor_base_t::actor_base_t;
  54. void configure(rotor::plugin::plugin_base_t &plugin) noexcept override {
  55. rotor::actor_base_t::configure(plugin);
  56. plugin.with_casted<r::plugin::address_maker_plugin_t>([&](auto &p) { p.set_identity("ponger", true); });
  57. plugin.with_casted<rotor::plugin::starter_plugin_t>([](auto &p) { p.subscribe_actor(&ponger_t::on_ping); });
  58. plugin.with_casted<rotor::plugin::registry_plugin_t>(
  59. [&](auto &p) { p.register_name("ponger", get_address()); });
  60. }
  61. void on_ping(message::ping_t &request) noexcept {
  62. using generator_t = std::mt19937;
  63. using distribution_t = std::uniform_real_distribution<double>;
  64. std::cout << "received ping\n";
  65. std::random_device rd;
  66. generator_t gen(rd());
  67. distribution_t dist;
  68. auto dice = dist(gen);
  69. std::cout << "pong, dice = " << dice << std::endl;
  70. if (dice > 0.5) {
  71. reply_to(request);
  72. } else {
  73. auto ec = r::make_error_code(r::error_code_t::request_timeout);
  74. auto ee = make_error(ec);
  75. reply_with_error(request, ee);
  76. }
  77. }
  78. };
  79. int main() {
  80. rotor::system_context_t ctx{};
  81. auto timeout = boost::posix_time::milliseconds{500}; /* does not matter */
  82. auto sup = ctx.create_supervisor<dummy_supervisor_t>().timeout(timeout).create_registry().finish();
  83. sup->create_actor<pinger_t>()
  84. .timeout(timeout)
  85. .escalate_failure() /* will shut down in error case */
  86. .autoshutdown_supervisor() /* will shut down in any case */
  87. .finish();
  88. sup->create_actor<ponger_t>().timeout(timeout).finish();
  89. sup->do_process();
  90. std::cout << "terminated, reason " << sup->get_shutdown_reason()->message() << "\n";
  91. return 0;
  92. }
  93. #if 0
  94. sample output 1
  95. received ping
  96. pong, dice = 0.53771
  97. pong received
  98. terminated, reason pinger 0x55a65bc99670 supervisor shutdown due to child shutdown policy
  99. <- supervisor 0x55a65bc91380 actor shutdown has been requested by supervisor
  100. <- pinger 0x55a65bc99670 normal shutdown
  101. sample output 2
  102. received ping
  103. pong, dice = 0.271187
  104. err: ponger 0x55a53fcf95f0 request timeout
  105. terminated, reason pinger 0x55a53fcf7670 failure escalation
  106. <- supervisor 0x55a53fcef380 actor shutdown has been requested by supervisor
  107. <- ponger 0x55a53fcf95f0 request timeout
  108. #endif