cpp-httplib
                                
                                 cpp-httplib copied to clipboard
                                
                                    cpp-httplib copied to clipboard
                            
                            
                            
                        Client Get operation stalls in getaddrinfo when network is down
Hello!
We are seeing the following issue when the network is completely unavailable: the Get operations stalls in the getaddrinfo for about 30 seconds. We set the connection timeout and read timeout, but they don't have any effect.
Is it possible that this logic also respects the connection timeout?
@TheMostDiligent thanks for the feedback. As far as I know, getaddrinfo is a blocking function and doesn't provide timeout option. So there is no way for me to deal with it... Do you know how other HTTP libraries handle this situation?
I am not very profound in HTTP handling. However, I tried to research the subject a bit and apparently there is an alternative function: getaddrinfo_a that supports asynchronous wait mode (GAI_NOWAIT).
In this mode, the function exits immediately and an event is triggered when the operation completes. I think this will allow to wait for the event in the loop and abort the loop if timeout elapses. Or even better, if the user-provided callback returns false similar to the callback of the Get function.
It looks like only Linux supports it... How could it be possible to do the same in Windows and MacOS?
It looks like only Linux supports it
It does seem so. On Mac, there is DNSServiceGetAddrInfo that allows async wait. This article describes it in detail. Looks like it is quite a hassle to implement this...
On Windows, I actually did not see that problem.
It seems like we may use GetAddrInfoEx which supports timeout parameter.
https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfoexw
In any case, supporting this feature on all three platforms and ensuring they work correctly will not likely be an easy task for me. So please don't expect me to implement it in the near future. A pull request is always welcome! :)