yhirose/cpp-httplib

How to detect client timeouts/disconnects when implementing long polling

frankosterfeld opened this issue · 1 comments

We're implementing a long polling scheme where the client sends a request, and the server blocks in the request handler until a server-side event occurs. The client has keep-alive enabled, and a 10s read timeout. To not block the worker thread forever/longer than needed, I'd like to detect the client disconnect, so that the request handler can periodically check for that, and abort waiting for the server-side event if the client connection is gone.

I see no API for that, is that missing, or by design?

(Alternatively, or in addition, I could implement a server-side timeout when waiting for the event, with the disadvantage that it might differ from the timeout used by the client, and that other disconnects wouldn't be detected)

@frankosterfeld I added DataSink::is_writable() with which you can check if the socket is still alive. You can see how it can be used in 'LongPollingTest.ClientCloseDetection' in test.cc. Hope it helps!