reduce response time while using async cpp boost socket

i’ve created an async beast server that gets a request from a browser, opens a second socket , writes the request , gets the response and sends it back to the browser. all async . as the “send back to browser” action waits to the read handler completion to trigger

void
on_write(
    boost::system::error_code ec,
    std::size_t bytes_transferred)
{
    boost::ignore_unused(bytes_transferred);

    if(ec)
        return fail2(ec, "write");
    std::cout << "===========on_write============" << std::endl;
    stopper("async_write" , 0);
    stopper("on_write" , 1);
    // Receive the HTTP response
    http::async_read(redirect_stream_, redirect_buffer_, redirect_res_,
        std::bind(
            &session2::on_read,
            shared_from_this(),
            std::placeholders::_1,
            std::placeholders::_2));
}

void
on_read(
    boost::system::error_code ec,
    std::size_t bytes_transferred)
{
    boost::ignore_unused(bytes_transferred);

    if(ec)
        return fail2(ec, "read");
    std::cout << "===========on_read============" << std::endl;
    stopper("on_write" , 0);
    stopper("on_read" , 1);
    // Write the message to standard out
    std::cout << redirect_res_.base() << std::endl;
    http::async_write(stream_, redirect_res_,
                      std::bind(
                          &session2::start_shutdown,
                          shared_from_this(),
                          std::placeholders::_1,
                          std::placeholders::_2));
    // Gracefully close the stream

}

it seems (from checks i have done) that it takes to long before the “write to browser” action is triggered (the on_read function) is there a better way to reduce the response to browser time? maybe by “read_some” method?

Answer

Depends a lot on the goal. If you really wish to transparently relay request/response without any modification, then read_some will be a better approach (but you won’t need anything from Beast, just Asio).

Using Beast, you can read partial requests as well, assuming you might want to modify some things about the messages being relayed, you might e.g. use message_parser<...> instead of message<> and use http::read_header and a loop to read a buffer_body.

There’s an example in the docs that implements things like this: HTTP Relay example.

You can also search my answers: https://stackoverflow.com/search?q=user%3A85371+beast+read_header or for buffer_body and or request_parser/response_parser

Beyond that there are many small ways to reduce latency (proper planning of threads, executors, allocators). But for something like that you should probably post your code (to CodeReview?).