drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.06k stars 1.06k forks source link

Add when_all to our coroutine library #1944

Open marty1885 opened 5 months ago

marty1885 commented 5 months ago

This PR implements when_all. A new function to wait for multiple coroutines to finish. This function is designed to not suffer from head of line blocking when a coroutine takes particularly long to finish.

Before this PR, we write the following. Which forces the next request to be sent after reeving response from the server.

for(int i=0;i<10;i++)
{
    co_await client->sendRequestCoro(req);
}

With this PR, we can do the following, Which will queue all request into the client before waiting for all responses.

std::vector<Task<>> tasks;
for(int i=0;i<10;i++)
{
    tasks.push_back(client->sendRequestCoro(req));
}

co_await when_all(std::move(tasks));