-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming_http_hpx.cpp
More file actions
362 lines (316 loc) · 9.96 KB
/
streaming_http_hpx.cpp
File metadata and controls
362 lines (316 loc) · 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright (c) 2023 Jakob Hördt
// based on work an example by
// Copyright (c) 2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/asio.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <hpx/hpx_start.hpp>
#include <hpx/include/actions.hpp>
#include <hpx/include/lcos.hpp>
#include <hpx/include/parallel_executors.hpp>
#include <hpx/include/post.hpp>
#include <hpx/include/runtime.hpp>
#include <hpx/iostream.hpp>
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
#include <memory>
#include <optional>
#include <ranges>
#include <span>
#include <string>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
std::vector<hpx::id_type> localities;
auto get_round_robin_locality() {
thread_local std::ptrdiff_t i = 0;
i = (i + 1) % std::ssize(localities);
return localities[i];
}
// boost span range constructor seems broken
template <typename T, std::size_t E>
auto std2boost(std::span<T, E> s) {
return boost::span<T, E>{s.data(), s.size()};
}
std::string get_file_contents(const char* filename) {
std::ifstream in(filename, std::ios::in);
if (!in) {
throw std::runtime_error{std::strerror(errno)};
}
in.seekg(0, std::ios::end);
std::string contents(in.tellg(), 0);
in.seekg(0);
in.read(contents.data(), std::ranges::ssize(contents));
return contents;
}
HPX_REGISTER_CHANNEL(uint8_t)
void execute_function(
std::string function_path, hpx::lcos::channel<uint8_t> input,
hpx::lcos::send_channel<uint8_t> output
) {
// hpx::cout << "hello from " << hpx::get_locality_id() << std::endl;
auto input_it = input.begin();
auto input_end = input.end();
auto more = [&] -> int32_t {
auto result = input_it != input_end;
return result;
};
auto put_byte = [&](uint32_t byte) { output.set(byte); };
auto get_byte = [&] -> int32_t {
if (input_it != input_end) {
return *input_it++;
}
return 0;
};
/////"wasm" function dispatch//
if (function_path == "/echo") {
while (more()) {
put_byte(get_byte());
}
} else if (function_path == "/noop") {
while (more()) {
get_byte();
}
} else {
output.close();
throw std::runtime_error{"function not found"};
}
//////////////////////////////
output.close();
}
HPX_PLAIN_ACTION(execute_function, execute_function_action)
class http_connection : public std::enable_shared_from_this<http_connection> {
public:
http_connection(tcp::socket socket) : socket_(std::move(socket)) {
request_parser_.body_limit(boost::none);
}
// Initiate the asynchronous operations associated with the connection.
void start() {
read_request();
check_deadline();
}
private:
// The socket for the currently connected client.
tcp::socket socket_;
// The buffer for performing reads.
beast::flat_buffer buffer_{8192};
std::array<uint8_t, 1024> write_buffer_;
std::array<uint8_t, 1024> read_buffer_;
http::response<http::empty_body> response_;
http::response<http::string_body> string_response_;
http::request_parser<http::buffer_body> request_parser_;
// sessions are created on the main node, so creating sessions "here" is
// correct
hpx::lcos::channel<uint8_t> input_{hpx::find_here()},
output_{hpx::find_here()};
// The timer for putting a deadline on connection processing.
net::steady_timer deadline_{
socket_.get_executor(), std::chrono::seconds(60)};
void read_request() {
http::async_read_header(
socket_, buffer_, request_parser_,
[self = shared_from_this(
)](beast::error_code ec, std::size_t bytes_transferred) {
boost::ignore_unused(bytes_transferred);
if (!ec) {
self->header_read();
} else {
std::cerr << "error: " << ec.message() << "\n";
}
}
);
}
void header_read() {
if (request_parser_.get().method() != http::verb::post) {
string_response_.result(http::status::bad_request);
string_response_.set(http::field::content_type, "text/plain");
string_response_.body() = "Invalid request-method.";
write_response(&http_connection::string_response_);
return;
}
// write response header upfront
response_.set(http::field::content_type, "application/octet-stream");
response_.keep_alive(false);
http::async_write(
socket_, response_,
[self = shared_from_this()](beast::error_code ec, std::size_t) {
// now we can start writing body data
// launch output task, this is the same post as in
// write_partial, but we need to start with reading from the
// channel here
hpx::post([self] {
int bytes_read = 0;
auto it = self->write_buffer_.begin();
// this "blocks" until the worker has given us enough
// output to fill a buffer
for (auto byte : self->output_) {
*it++ = byte;
++bytes_read;
if (bytes_read == std::ssize(self->write_buffer_)) {
break;
}
}
self->write_partial(bytes_read);
});
}
);
// launch input task that reads from socket and puts into
// function channel
read_partial();
hpx::post([self = shared_from_this()] {
execute_function_action f;
try {
f(get_round_robin_locality(), self->request_parser_.get().target(),
self->input_, self->output_);
} catch (const std::exception& e) {
std::cerr << "action threw: " << e.what() << '\n';
self->string_response_.result(http::status::not_found);
self->string_response_.set(
http::field::content_type, "text/plain"
);
self->string_response_.body() = "function not found\r\n";
self->write_response(&http_connection::string_response_);
}
});
}
void write_partial(int bytes_read) {
if (bytes_read == 0) {
// there is nothing left to be written here
socket_.shutdown(tcp::socket::shutdown_send);
deadline_.cancel();
return;
}
net::async_write(
socket_, net::buffer(write_buffer_, bytes_read),
[self = shared_from_this(
)](boost::system::error_code ec, std::size_t bytes_transferred) {
if (ec) {
std::cerr << "error " << ec;
return;
}
hpx::post([self] {
int bytes_read = 0;
auto it = self->write_buffer_.begin();
// this "blocks" until the worker has given us enough output
// to fill a buffer
for (auto byte : self->output_) {
*it++ = byte;
++bytes_read;
if (bytes_read == std::ssize(self->write_buffer_)) {
break;
}
}
self->write_partial(bytes_read);
});
}
);
}
// see
// https://www.boost.org/doc/libs/1_83_0/libs/beast/doc/html/beast/using_http/parser_stream_operations/incremental_read.html
// for an idea of how incremental reads are supposed to be done with beast
void read_partial() {
request_parser_.get().body().data = read_buffer_.data();
request_parser_.get().body().size = read_buffer_.size();
http::async_read_some(
socket_, buffer_, request_parser_,
[self = shared_from_this(
)](boost::system::error_code ec, std::size_t bytes_transferred) {
if (ec == http::error::need_buffer) {
ec = {};
}
if (ec) {
std::cerr << "error " << ec;
return;
}
hpx::post([self] {
// send read bytes through channel to function
for (auto byte : std::span{
self->read_buffer_.data(),
self->read_buffer_.size() -
self->request_parser_.get().body().size}) {
self->input_.set(byte);
}
if (self->request_parser_.is_done()) {
// indicate that this is all the input
self->input_.close();
return;
}
self->read_partial();
});
}
);
}
void write_response(auto http_connection::*response) {
// may be called in hpx thread, which is fine
(this->*response).content_length((this->*response).body().size());
(this->*response).keep_alive(false);
// call is by itself thread safe and handler will sync before execution
// handler cant overlap with deadline as we are using one asio thread
http::async_write(
socket_, this->*response,
[self = shared_from_this()](beast::error_code ec, std::size_t) {
self->socket_.shutdown(tcp::socket::shutdown_send, ec);
self->deadline_.cancel();
}
);
}
// Check whether we have spent enough time on this connection.
void check_deadline() {
deadline_.async_wait([self = shared_from_this(
)](boost::system::error_code ec) {
if (!ec) {
std::cerr << "taking too long :(\n";
// Close socket to cancel any outstanding operation.
self->socket_.close(ec);
} else if (boost::asio::error::operation_aborted) {
// got canceled
} else {
std::cerr << "unknown error when waiting for deadline\n";
}
});
}
};
// "Loop" forever accepting new connections.
void http_server(tcp::acceptor& acceptor, tcp::socket& socket) {
acceptor.async_accept(socket, [&](beast::error_code ec) {
if (!ec)
std::make_shared<http_connection>(std::move(socket))->start();
http_server(acceptor, socket);
});
}
int main(int argc, char* argv[]) {
try {
// Initialize HPX, don't run hpx_main
hpx::start(nullptr, argc, argv);
// initialize localities used for load balancing
localities = hpx::find_all_localities();
// we don't want to run asio on a hpx thread, but on the main thread, so
// we cant use hpx_main
if (hpx::find_here() == hpx::find_root_locality()) {
auto const address = net::ip::make_address("127.0.0.1");
unsigned short port = 32425;
net::io_context ioc{1};
tcp::acceptor acceptor{ioc, {address, port}};
tcp::socket socket{ioc};
http_server(acceptor, socket);
ioc.run();
// this shutdown is not clean at all, but it does not matter for our
// purposes
hpx::post([]() { hpx::finalize(); });
}
// blocks until finalize is called
return hpx::stop();
} catch (std::exception const& e) {
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
}