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 : : #pragma once
16 : :
17 : : #include <hpactor/actor/external_msg_gateway.hpp>
18 : : #include <hpactor/mem/std_allocator.hpp>
19 : : #include <hpactor/net/http_gateway.hpp>
20 : : #include <hpactor/net/http_serializer.hpp>
21 : : #include <hpactor/net/http_types.hpp>
22 : :
23 : : #include <chrono>
24 : : #include <deque>
25 : : #include <memory>
26 : : #include <mutex>
27 : : #include <queue>
28 : : #include <unordered_map>
29 : :
30 : : namespace hpactor {
31 : : namespace net {
32 : :
33 : : // ---------------------------------------------------------------------------
34 : : // HTTPGatewayActor — ExternalMsgGatewayActor-based HTTP ingress gateway
35 : : // ---------------------------------------------------------------------------
36 : : class HTTPGatewayActor : public ExternalMsgGatewayActor {
37 : : public:
38 : : using MessageBuilder = RouteRegistry::MessageBuilder;
39 : :
40 : : HTTPGatewayActor(ActorContext* ctx, ActorSystem& sys,
41 : : const std::string& bind_host, uint16_t port);
42 : : ~HTTPGatewayActor() override;
43 : :
44 : : HTTPGatewayActor(const HTTPGatewayActor&) = delete;
45 : : HTTPGatewayActor& operator=(const HTTPGatewayActor&) = delete;
46 : :
47 : : // Route registration (HTTP-method-aware)
48 : : void route(HttpMethod method, std::string path_pattern,
49 : : MessageBuilder builder, int priority = 0);
50 : : void route(HttpMethod method, std::string path_pattern, ActorAddr target);
51 : :
52 : : // Configuration
53 : : void set_reply_timeout(std::chrono::milliseconds t) { reply_timeout_ = t; }
54 : : void set_max_connections(size_t max) { gateway_.set_max_connections(max); }
55 : : void set_max_request_size(size_t max) { gateway_.set_max_request_size(max); }
56 : :
57 : : uint16_t port() const { return gateway_.port(); }
58 : 1 : bool is_listening() const { return gateway_.is_listening(); }
59 : :
60 : : // DaemonActor overrides
61 : : bool run_once() override;
62 : :
63 : : protected:
64 : : void on_daemon_start() override;
65 : : void on_daemon_stop() override;
66 : : void on_deactivate() override;
67 : :
68 : : private:
69 : : void on_request(HTTPConnection* conn, HttpRequest&& req);
70 : : void on_reply(TypedMessage&& msg);
71 : : void on_error(HTTPConnection* conn, const error& err);
72 : : void on_timeout(uint64_t request_id);
73 : : void close_connection(HTTPConnection* conn);
74 : :
75 : : HTTPGateway gateway_;
76 : : RouteRegistry routes_;
77 : : std::unique_ptr<HttpSerializer> serializer_;
78 : :
79 : : struct PendingReply : mem::SlabAllocated<PendingReply> {
80 : : uint64_t request_id;
81 : : HTTPConnection* conn;
82 : : std::chrono::steady_clock::time_point enqueued_at;
83 : : };
84 : :
85 : : using PendingReplyMap =
86 : : std::unordered_map<uint64_t, std::unique_ptr<PendingReply>,
87 : : std::hash<uint64_t>, std::equal_to<>,
88 : : mem::MemStdAllocator<std::pair<const uint64_t,
89 : : std::unique_ptr<PendingReply>>>>;
90 : : PendingReplyMap pending_replies_{
91 : : mem::MemStdAllocator<std::pair<const uint64_t,
92 : : std::unique_ptr<PendingReply>>>(
93 : : id_ptr(), mem::RegionType::kActor)};
94 : : std::mutex reply_mutex_;
95 : :
96 : : using ReplyDeque =
97 : : std::deque<TypedMessage, mem::MemStdAllocator<TypedMessage>>;
98 : : std::queue<TypedMessage, ReplyDeque> reply_queue_{
99 : : ReplyDeque(mem::MemStdAllocator<TypedMessage>(
100 : : id_ptr(), mem::RegionType::kActor))};
101 : : std::mutex reply_queue_mutex_;
102 : :
103 : : Actor reply_adapter_{nullptr};
104 : :
105 : 1 : std::chrono::milliseconds reply_timeout_{5000};
106 : : size_t max_connections_{1000};
107 : : size_t max_request_size_{1048576};
108 : : uint64_t next_request_id_{1};
109 : : };
110 : :
111 : : } // namespace net
112 : : } // namespace hpactor
|