Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Manage lifecycle of deferred_req_res_t better.

kishorencad28540fdc2d
diff --git a/include/http_server.h b/include/http_server.h
index c5288829..3d976dd8 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -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 {
diff --git a/src/http_client.cpp b/src/http_client.cpp
index f0b17dbe..08d23623 100644
--- a/src/http_client.cpp
+++ b/src/http_client.cpp
@@ -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;
diff --git a/src/http_server.cpp b/src/http_server.cpp
index cb433a7e..35898bbd 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -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;
}
diff --git a/src/main/benchmark.cpp b/src/main/benchmark.cpp
index 06762461..147dad3c 100644
--- a/src/main/benchmark.cpp
+++ b/src/main/benchmark.cpp
@@ -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) {
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index fd06cf6d..01cd65a7 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -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();