mirror of
https://github.com/typesense/typesense.git
synced 2025-05-18 12:42:50 +08:00
Manage lifecycle of deferred_req_res_t better.
This commit is contained in:
parent
ddf54cbd6e
commit
ad28540fdc
@ -44,6 +44,14 @@ struct deferred_req_res_t {
|
||||
const std::shared_ptr<http_req> req;
|
||||
const std::shared_ptr<http_res> res;
|
||||
HttpServer* server;
|
||||
|
||||
// used to manage lifecycle of non-async responses
|
||||
bool destroy_after_stream_response;
|
||||
|
||||
deferred_req_res_t(const std::shared_ptr<http_req> &req, const std::shared_ptr<http_res> &res,
|
||||
HttpServer *server, bool destroy_after_stream_response = false) :
|
||||
req(req), res(res), server(server), destroy_after_stream_response(destroy_after_stream_response) {}
|
||||
|
||||
};
|
||||
|
||||
class HttpServer {
|
||||
|
@ -20,7 +20,7 @@ long HttpClient::post_response(const std::string &url, const std::string &body,
|
||||
|
||||
long HttpClient::post_response_async(const std::string &url, const std::shared_ptr<http_req> request,
|
||||
const std::shared_ptr<http_res> response, HttpServer* server) {
|
||||
deferred_req_res_t* req_res = new deferred_req_res_t{request, response, server};
|
||||
deferred_req_res_t* req_res = new deferred_req_res_t(request, response, server);
|
||||
std::unique_ptr<deferred_req_res_t> req_res_guard(req_res);
|
||||
struct curl_slist* chunk = nullptr;
|
||||
|
||||
|
@ -510,8 +510,9 @@ int HttpServer::process_request(const std::shared_ptr<http_req>& request, const
|
||||
(rpath->handler)(request, response);
|
||||
|
||||
if(!rpath->async_res) {
|
||||
deferred_req_res_t req_res{request, response, http_server};
|
||||
message_dispatcher->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, &req_res);
|
||||
// lifecycle of non async res will be owned by stream responder
|
||||
auto req_res = new deferred_req_res_t(request, response, http_server, true);
|
||||
message_dispatcher->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, req_res);
|
||||
response->wait();
|
||||
}
|
||||
//LOG(INFO) << "Response done " << response.get();
|
||||
@ -537,7 +538,7 @@ void HttpServer::defer_processing(const std::shared_ptr<http_req>& req, const st
|
||||
//LOG(INFO) << "defer_processing, exit_loop: " << exit_loop << ", res: " << res.get();
|
||||
|
||||
if(req->defer_timer.data == nullptr) {
|
||||
auto deferred_req_res = new deferred_req_res_t{req, res, this};
|
||||
auto deferred_req_res = new deferred_req_res_t(req, res, this);
|
||||
req->defer_timer.data = deferred_req_res;
|
||||
h2o_timer_init(&req->defer_timer.timer, on_deferred_process_request);
|
||||
} else {
|
||||
@ -682,7 +683,7 @@ void HttpServer::stream_response(const std::shared_ptr<http_req>& request, const
|
||||
|
||||
void HttpServer::destroy_request_response(const std::shared_ptr<http_req>& request,
|
||||
const std::shared_ptr<http_res>& response) {
|
||||
//LOG(INFO) << "destroy_request_response";
|
||||
//LOG(INFO) << "destroy_request_response " << response.get();
|
||||
|
||||
if(request->defer_timer.data != nullptr) {
|
||||
deferred_req_res_t* deferred_req_res = static_cast<deferred_req_res_t*>(request->defer_timer.data);
|
||||
@ -804,8 +805,13 @@ uint64_t HttpServer::node_state() const {
|
||||
|
||||
bool HttpServer::on_stream_response_message(void *data) {
|
||||
//LOG(INFO) << "on_stream_response_message";
|
||||
deferred_req_res_t* req_res = static_cast<deferred_req_res_t *>(data);
|
||||
auto req_res = static_cast<deferred_req_res_t *>(data);
|
||||
stream_response(req_res->req, req_res->res);
|
||||
|
||||
if(req_res->destroy_after_stream_response) {
|
||||
delete req_res;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -47,7 +47,7 @@ void benchmark_hn_titles(char* file_path) {
|
||||
Store *store = new Store("/tmp/typesense-data");
|
||||
CollectionManager & collectionManager = CollectionManager::get_instance();
|
||||
collectionManager.init(store, 4, "abcd");
|
||||
collectionManager.load();
|
||||
collectionManager.load(100, 100);
|
||||
|
||||
Collection *collection = collectionManager.get_collection("hnstories_direct").get();
|
||||
if(collection == nullptr) {
|
||||
@ -117,7 +117,7 @@ void benchmark_reactjs_pages(char* file_path) {
|
||||
Store *store = new Store("/tmp/typesense-data");
|
||||
CollectionManager & collectionManager = CollectionManager::get_instance();
|
||||
collectionManager.init(store, 4, "abcd");
|
||||
collectionManager.load();
|
||||
collectionManager.load(100, 100);
|
||||
|
||||
Collection* collection = collectionManager.get_collection("reactjs_pages").get();
|
||||
if(collection == nullptr) {
|
||||
|
@ -688,8 +688,8 @@ void OnDemandSnapshotClosure::Run() {
|
||||
res->status_code = status_code;
|
||||
res->body = response.dump();
|
||||
|
||||
deferred_req_res_t req_res{req, res, nullptr};
|
||||
replication_state->get_message_dispatcher()->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, &req_res);
|
||||
auto req_res = new deferred_req_res_t(req, res, nullptr, true);
|
||||
replication_state->get_message_dispatcher()->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, req_res);
|
||||
|
||||
// wait for response to be sent
|
||||
res->wait();
|
||||
|
Loading…
x
Reference in New Issue
Block a user