supervisor_config.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #pragma once
  2. //
  3. // Copyright (c) 2019-2021 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "policy.h"
  8. #include "actor_config.h"
  9. namespace rotor {
  10. /** \struct supervisor_config_t
  11. * \brief base supervisor config, which holds shutdowm timeout value
  12. */
  13. struct supervisor_config_t : actor_config_t {
  14. using actor_config_t::actor_config_t;
  15. /** \brief how to behave if child-actor fails */
  16. supervisor_policy_t policy = supervisor_policy_t::shutdown_self;
  17. /** \brief whether the registy actor should be instantiated by supervisor */
  18. bool create_registry = false;
  19. /** \brief whether supervisor should wait untill all actors confirmed
  20. * initialization, and only then send start signal to all of them */
  21. bool synchronize_start = false;
  22. /** \brief use the specified address of a registry
  23. *
  24. * Can be usesul if an registry was created on the different supervisors
  25. * hierarchy
  26. */
  27. address_ptr_t registry_address;
  28. /** \brief initial queue size for inbound messages. Makes sense only for
  29. * root/leader supervisor */
  30. size_t inbound_queue_size = 64;
  31. /**
  32. * \brief How much time it will spend in polling inbound queue before switching into
  33. * sleep mode (i.e. waiting external messages)
  34. */
  35. pt::time_duration poll_duration = pt::millisec{1};
  36. };
  37. /** \brief CRTP supervisor config builder */
  38. template <typename Supervisor> struct supervisor_config_builder_t : actor_config_builder_t<Supervisor> {
  39. /** \brief final builder class */
  40. using builder_t = typename Supervisor::template config_builder_t<Supervisor>;
  41. /** \brief parent config builder */
  42. using parent_t = actor_config_builder_t<Supervisor>;
  43. using parent_t::parent_t;
  44. /** \brief defines actor's startup policy */
  45. builder_t &&policy(supervisor_policy_t policy_) &&noexcept {
  46. parent_t::config.policy = policy_;
  47. return std::move(*static_cast<typename parent_t::builder_t *>(this));
  48. }
  49. /** \brief instructs supervisor to create an registry */
  50. builder_t &&create_registry(bool value = true) &&noexcept {
  51. parent_t::config.create_registry = value;
  52. return std::move(*static_cast<typename parent_t::builder_t *>(this));
  53. }
  54. /** \brief instructs supervisor to synchornize start on it's children actors */
  55. builder_t &&synchronize_start(bool value = true) &&noexcept {
  56. parent_t::config.synchronize_start = value;
  57. return std::move(*static_cast<typename parent_t::builder_t *>(this));
  58. }
  59. /** \brief injects external registry address */
  60. builder_t &&registry_address(const address_ptr_t &value) &&noexcept {
  61. parent_t::config.registry_address = value;
  62. return std::move(*static_cast<typename parent_t::builder_t *>(this));
  63. }
  64. /** \brief initial queue size for inbound messages. Makes sense only for
  65. * root/leader supervisor */
  66. builder_t &&inbound_queue_size(size_t value) && {
  67. parent_t::config.inbound_queue_size = value;
  68. return std::move(*static_cast<builder_t *>(this));
  69. }
  70. /** \brief how much time spend in active inbound queue polling */
  71. builder_t &&poll_duration(const pt::time_duration &value) && {
  72. parent_t::config.poll_duration = value;
  73. return std::move(*static_cast<builder_t *>(this));
  74. }
  75. virtual bool validate() noexcept {
  76. bool r = parent_t::validate();
  77. if (r) {
  78. r = !(parent_t::config.registry_address && parent_t::config.create_registry);
  79. }
  80. return r;
  81. }
  82. };
  83. } // namespace rotor