Search code examples
asynchronouswebnetwork-programming

how top-level async web requests handle the reponse


I have fundumental question about how async requests work at top level.

Imagin if we have a top level route called HomePage(). This route is an async route and within this route we call to 10 different APIs before sending the response(image it takes like 5 seconds, remember this is an example to understand the concept and these numbers are for learning purposes). All of these api requests are awaited. So the request handler just releases the thread hanlding this request and goes to handle other requests until the response for these apis come back. So lets add this constraint. Our network card can handle only 1 connection and that one is held open till the response for the request to HomePage is ready. Therefor we cannot make any other requests to the server so whats the difference if this whole thing was sync from the beggining. We cannot drop the connection to the first request to HomePage because if that's the case then how are we ever going to send back the response for that request and we cannot handle new requests because the connection is kept open. I suspect that my problem is how the reponse is sent back on top level async routes. Can anybody give a deep dive explaination on how these requests are handled that can take more requests and still send back the response(because if it can send back a response the connection HAS TO HAVE KEPT ALIVE). Examples would be much appreciated.


Solution

  • So lets add this constraint. Our network card can handle only 1 connection

    That constraint cannot exist. Network cards handle packets, not connections. Connections are a virtual construct that exist in the host computer.

    Can anybody give a deep dive explaination on how these requests are handled that can take more requests and still send back the response(because if it can send back a response the connection HAS TO HAVE KEPT ALIVE).

    Of course the connection is kept alive. The top-level async method will return the thread to the thread pool, where it is available to handle any other requests.

    If you have some artificial constraint on your web app that prevents it from having more than one connection, then there won't be any other requests to handle, and the thread pool threads will do nothing.