-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: ability to check if the connection is still alive #2017
Comments
@ngxson thanks for the report. Regarding the first option ( As for the second option, it could be possible to pass on the stream object down to a request handler. But we are not able to change Could you show me an example code which shows how the client code checks if the socket is alive? The usage example may help me come up with better ideas. Thanks! |
Yup sure, a minimal example could be: svr.Get("/task", [&](const Request& req, Response& res) {
const char * result = nullptr;
task.run();
while (result == nullptr) { // wait loop
sleep(1);
result = task.get_result();
}
res.set_content(result, "text/plain");
}); If I'm in the JS world (i.e. using while (result == nullptr) { // wait loop
sleep(1);
if (req.connection.closed) {
task.cancel();
return;
}
result = task.get_result();
} Because everything still happen in the scope of I looked again at the implementation of @ochafik . The main difference of his approach compared to mine is that he wants to check the state of connection from outside of handler thread: svr.Get("/task", [&](const Request& req, Response& res) {
// delegate the check to `task` thread
task.run([&req]() -> bool { return req.connection.closed; });
res.set_content(task.get_result(), "text/plain");
}); Indeed, I would also not recommend doing this because it can be difficult to keep track the life cycle of Therefore, I'm proposing here that whatever check must be strictly done in the scope of Not sure if this is clear enough, but feel free to discuss more. Thank you! |
@ngxson thanks for helping me to understand your use case clearly! I have added |
The latest version 0.18.5 includes this change. |
Following up #1956, I would like to discuss more about the ability to add a method to check if the socket is still alive. This can be useful to cancel the underlay processing for a request, as described in this issue: ggerganov/llama.cpp#9273
Currently, one way to do is to use
set_chunked_content_provider
, but the problem is that we cannot change the HTTP status code once we're already inside the callback function.Therefore, I propose 2 solutions here:
My first suggestion is to add something like
set_async_response
, which works likeset_chunked_content_provider
but send out the whole response, instead of only the body.Second suggestion is to add
is_alive()
to theHandler
:Server::routing
already hasStream &strm
in its scope, so we can pass it toServer::dispatch_request
Handler
, by wrapping it inside a functionis_alive()
. I'm not sure how it should look like, but that's the ideaHappy do discuss more to see what's the best way to implement this. Thank you!
The text was updated successfully, but these errors were encountered: