Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #include <hpactor/config/toml_config_parser.hpp>
16 : : #include <hpactor/config/toml_parser_registry.hpp>
17 : : #include <hpactor/mailbox/mailbox_policy.hpp>
18 : :
19 : : #include <string>
20 : :
21 : : namespace hpactor::config {
22 : : namespace {
23 : :
24 : : // BackpressureMode string parsing (same logic as topology parser)
25 : : static hpactor::mailbox::BackpressureMode
26 : 4 : parse_backpressure_mode(const std::string& s) {
27 : 4 : if (s == "disabled")
28 : 0 : return hpactor::mailbox::BackpressureMode::Disabled;
29 : 4 : if (s == "local")
30 : 0 : return hpactor::mailbox::BackpressureMode::LocalSignal;
31 : 4 : if (s == "remote")
32 : 0 : return hpactor::mailbox::BackpressureMode::RemoteSignal;
33 : 4 : return hpactor::mailbox::BackpressureMode::LocalAndRemoteSignal;
34 : : }
35 : :
36 : : // OverflowPolicy string parsing
37 : : static hpactor::mailbox::OverflowPolicy
38 : 4 : parse_overflow_policy(const std::string& s) {
39 : 4 : if (s == "drop_newest")
40 : 0 : return hpactor::mailbox::OverflowPolicy::DropNewest;
41 : 4 : if (s == "drop_oldest")
42 : 0 : return hpactor::mailbox::OverflowPolicy::DropOldest;
43 : 4 : if (s == "drop_lowest_priority")
44 : 0 : return hpactor::mailbox::OverflowPolicy::DropLowestPriority;
45 : 4 : if (s == "dead_letter")
46 : 4 : return hpactor::mailbox::OverflowPolicy::DeadLetter;
47 : 0 : if (s == "spill_to_overflow_queue")
48 : 0 : return hpactor::mailbox::OverflowPolicy::SpillToOverflowQueue;
49 : 0 : if (s == "signal_only")
50 : 0 : return hpactor::mailbox::OverflowPolicy::SignalOnly;
51 : 0 : if (s == "block_when_allowed")
52 : 0 : return hpactor::mailbox::OverflowPolicy::BlockWhenAllowed;
53 : 0 : return hpactor::mailbox::OverflowPolicy::RejectNewest;
54 : : }
55 : :
56 : : class MailboxConfigParser final : public ITomlSystemConfigParser {
57 : : public:
58 : : static constexpr std::string_view kName = "system.mailbox";
59 : : static constexpr int kOrder = 90;
60 : :
61 : 0 : std::string_view name() const noexcept override {
62 : 0 : return kName;
63 : : }
64 : 0 : int order() const noexcept override {
65 : 0 : return kOrder;
66 : : }
67 : :
68 : 35 : result<void> parse(const TomlTableView& system, SystemDef& out,
69 : : TomlParseContext& /*ctx*/) const override {
70 : 35 : auto mt = system.table("mailbox");
71 : 35 : if (!mt.valid())
72 : 31 : return result<void>::make();
73 : :
74 : 4 : out.mailbox.default_capacity = mt.read_uint32("default_capacity", 1024);
75 : 4 : out.mailbox.default_byte_capacity =
76 : 4 : mt.read_uint32("default_byte_capacity", 0);
77 : 4 : out.mailbox.default_policy =
78 : 4 : parse_overflow_policy(mt.read_string("default_policy", "reject_"
79 : : "newest"));
80 : 4 : out.mailbox.high_watermark = mt.read_double("high_watermark", 0.80);
81 : 4 : out.mailbox.low_watermark = mt.read_double("low_watermark", 0.50);
82 : 4 : out.mailbox.protected_system_messages =
83 : 4 : mt.read_uint32("protected_system_messages", 32);
84 : 4 : out.mailbox.backpressure_mode =
85 : 4 : parse_backpressure_mode(mt.read_string("backpressure", "local_and_"
86 : : "remote"));
87 : :
88 : 4 : return result<void>::make();
89 : : }
90 : : };
91 : :
92 : : const TomlSystemParserRegistration<MailboxConfigParser> kRegisterMailboxConfigParser;
93 : :
94 : : } // anonymous namespace
95 : : } // namespace hpactor::config
|