chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.89k stars 1.21k forks source link

Is it safe to start multiple async_accept calls on the same acceptor? #1350

Open zhllxt opened 1 year ago

zhllxt commented 1 year ago
net::awaitable<void> do_listen(tcp_acceptor& a)
{
    while (true)
    {
        tcp_socket socket(m_executor);
        co_await a.async_accept(socket);

        //...
    }
}
void start()
{
    // Here, multiple coroutines are started on the same acceptor to accept connections.
    for (int i = 0; i < 32; i++)
    {
        net::co_spawn(m_executor, do_listen(m_acceptor), net::detached);
    }
}
Jackarain commented 11 months ago

In some high-performance application scenarios, such as HLS streaming servers, the speed and volume of requests can be extremely high. In such cases, initiating multiple async_accept calls simultaneously makes a lot of sense, as it ensures that the server can handle a large number of connection requests as quickly and concurrently as possible.

When a server needs to process a significant number of concurrent connection requests, relying solely on a single async_accept might lead to latency and performance bottlenecks. By initiating multiple async_accept calls at the same time, the server can start processing new connection requests almost immediately, thereby enhancing the overall throughput and response time.

You can also refer to: https://github.com/boostorg/beast/blob/develop/example/http/server/fast/http_server_fast.cpp