Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
- Enable tcp_nodelay for all sessions
- Turn off stats collector for all sessions
- Update the session_stats struct to have start and end timepoints rather than duration
- Clean up includes, be more specific for beast
  • Loading branch information
luketokheim committed Sep 30, 2022
1 parent f1f352e commit 5622a8a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN apk update && apk add --no-cache \
g++ \
liburing-dev \
linux-headers \
make \
ninja \
py-pip

# Install conan package manager
Expand All @@ -27,7 +27,7 @@ RUN conan install .. --build=missing

# Configure, build with static musl/libc and libstdc++ so we can run on the
# scratch empty base image
RUN cmake -B . -S .. \
RUN cmake .. -GNinja \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake \
-DENABLE_STANDALONE=ON \
Expand Down
2 changes: 1 addition & 1 deletion include/httpmicroservice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ int run(int port, request_handler handler);

int getenv_port();

response make_response(request req);
response make_response(const request& req);

} // namespace httpmicroservice
13 changes: 9 additions & 4 deletions include/httpmicroservice/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace asio = boost::asio;
template <typename Acceptor, typename Handler>
asio::awaitable<void> accept(Acceptor acceptor, Handler handler)
{
using tcp = asio::ip::tcp;

for (;;) {
boost::system::error_code ec;

Expand All @@ -28,12 +30,15 @@ asio::awaitable<void> accept(Acceptor acceptor, Handler handler)
break;
}

stream.set_option(tcp::no_delay{true}, ec);

if (ec) {
continue;
}

// Run coroutine to handle one http connection
co_spawn(
acceptor.get_executor(),
session(
std::move(stream), handler,
std::make_optional<session_stats>()),
acceptor.get_executor(), session(std::move(stream), handler, {}),
[](auto ptr, auto stats) {
// Propagate exception from the coroutine
if (ptr) {
Expand Down
21 changes: 10 additions & 11 deletions include/httpmicroservice/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include <boost/asio/awaitable.hpp>
#include <boost/asio/redirect_error.hpp>
#include <boost/asio/use_awaitable.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/http/read.hpp>
#include <boost/beast/http/write.hpp>

#include <chrono>
#include <functional>
Expand All @@ -21,28 +22,26 @@ namespace httpmicroservice {

namespace asio = boost::asio;

constexpr auto kRequestSizeLimit = 1 << 20;
// 1 MB request limit
constexpr auto kRequestSizeLimit = 1000 * 1000;

template <typename AsyncStream, typename Handler>
asio::awaitable<std::optional<session_stats>>
session(AsyncStream stream, Handler handler, std::optional<session_stats> stats)
{
std::chrono::steady_clock::time_point start_time;
if (stats) {
stats->fd = stream.native_handle();
start_time = std::chrono::steady_clock::now();
stats->start_time = std::chrono::steady_clock::now();
}

boost::beast::flat_buffer buffer(kRequestSizeLimit);
boost::system::error_code ec;

// stream.set_option(asio::ip::tcp::no_delay{true}, ec);

for (;;) {
// req = read(...)
request req;
{
auto bytes_read = co_await http::async_read(
const auto bytes_read = co_await http::async_read(
stream, buffer, req,
asio::redirect_error(asio::use_awaitable, ec));

Expand All @@ -60,15 +59,15 @@ session(AsyncStream stream, Handler handler, std::optional<session_stats> stats)
}
}

auto keep_alive = req.keep_alive();
const auto keep_alive = req.keep_alive();

// res = handler(req)
response res = co_await std::invoke(handler, std::move(req));
res.prepare_payload();
res.keep_alive(keep_alive);

// write(res)
auto bytes_write = co_await http::async_write(
const auto bytes_write = co_await http::async_write(
stream, res, asio::redirect_error(asio::use_awaitable, ec));

if (ec) {
Expand All @@ -87,7 +86,7 @@ session(AsyncStream stream, Handler handler, std::optional<session_stats> stats)
}

if (stats) {
stats->duration = std::chrono::steady_clock::now() - start_time;
stats->end_time = std::chrono::steady_clock::now();
}

co_return stats;
Expand Down
11 changes: 7 additions & 4 deletions include/httpmicroservice/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <fmt/core.h>

#include <chrono>
#include <string>
#include <functional>

namespace httpmicroservice {

Expand All @@ -25,14 +25,16 @@ struct session_stats {
int num_request{0};
int bytes_read{0};
int bytes_write{0};
std::chrono::steady_clock::duration duration{};
std::chrono::steady_clock::time_point start_time{};
std::chrono::steady_clock::time_point end_time{};
};

} // namespace httpmicroservice

template <>
struct fmt::formatter<httpmicroservice::session_stats> {
constexpr auto parse(format_parse_context& ctx) {
constexpr auto parse(format_parse_context& ctx)
{
return ctx.begin();
}

Expand All @@ -45,6 +47,7 @@ struct fmt::formatter<httpmicroservice::session_stats> {
"{{\"fd\": {}, \"num_request\": {}, \"bytes_read\": {}, "
"\"bytes_write\": {}, \"duration\": {}}}",
stats.fd, stats.num_request, stats.bytes_read, stats.bytes_write,
std::chrono::duration<double>(stats.duration).count());
std::chrono::duration<double>(stats.end_time - stats.start_time)
.count());
}
};
4 changes: 2 additions & 2 deletions src/httpmicroservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace httpmicroservice {

response make_response(request req)
response make_response(const request& req)
{
return response(http::status::ok, req.version());
return response{http::status::ok, req.version()};
}

int run(int port, request_handler handler)
Expand Down

0 comments on commit 5622a8a

Please sign in to comment.