chriskohlhoff / asio

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

async_resolve does not perform in parallel #449

Open MrElectrify opened 4 years ago

MrElectrify commented 4 years ago

asio::ip::tcp::resolver::async_resolve resolves names in-order on Windows, I.E. multiple name resolutions do not take place at a given time. As I suspected, getaddrinfo is used in a worker thread in the implementation. I believe this can be improved two ways:

The lesser preferable of the two is spawning a thread for every resolution, which is not feasible for obvious reasons, or using a thread pool, which would only improve the situation and would not solve it.

The latter of the two is by using GetAddrInfoExA with a completion callback, or getaddrinfo_a on Linux (I haven't tested on Linux outside of WSL which may still use getaddrinfo internally so I don't know if the limitation exists on Linux). This would not work on Windows 7 or Server 2008 R2 or before, as per MSDN, [lpOverlapped] is currently reserved and must be set to NULL since asynchronous operations are not supported. However, it would still improve most Windows use cases that do large amounts of name resolution.

vinipsmaker commented 2 years ago

@MrElectrify here's sample code on how to use GetAddrInfoExW() with Boost.Asio: https://gitlab.com/emilua/emilua/-/blob/45bd0b5f951df53f346d8dab511bc8d12e4e4b80/src/ip.cpp#L2517

I've tested on a Windows 10 machine.

vinipsmaker commented 2 years ago

@MrElectrify it seems to me that getaddrinfo_a() on Linux is not worth the hassle. Is it my impression or will it just create another thread? Boost.Asio already does that, so what's the point? Something like https://c-ares.org/ would be more interesting in this case, but that's something that should be left to the user. Boost.Asio should map to OS interfaces only (e.g. NSS must be respected).

vinipsmaker commented 2 years ago

Does anyone know of an alternative for macOS? The only thing close to a tutorial that I've found was a patch lying around in the Emacs mailing list, but the API they're using has been deprecated for a long time already.

feliwir commented 2 years ago

Why can we not call getaddrinfo_a on Linux? The current behaviour causes us a lot of headache on linux/android