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

Better synchronization during slow request logging.

Kishore Nallan1e241e970d6d
diff --git a/include/http_data.h b/include/http_data.h
index 5a01bdf7..82725c69 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -233,10 +233,12 @@ struct http_req {
int64_t log_index;
std::atomic<bool> is_http_v1;
+ std::atomic<bool> is_diposed;
http_req(): _req(nullptr), route_hash(1),
first_chunk_aggregate(true), last_chunk_aggregate(false),
- chunk_len(0), body_index(0), data(nullptr), ready(false), log_index(0), is_http_v1(true) {
+ chunk_len(0), body_index(0), data(nullptr), ready(false), log_index(0), is_http_v1(true),
+ is_diposed(false) {
start_ts = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
@@ -250,7 +252,7 @@ struct http_req {
params(params), embedded_params_vec(embedded_params_vec),
first_chunk_aggregate(true), last_chunk_aggregate(false),
chunk_len(0), body(body), body_index(0), data(nullptr), ready(false),
- log_index(0) {
+ log_index(0), is_diposed(false) {
start_ts = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
@@ -274,7 +276,7 @@ struct http_req {
AppMetrics::get_instance().increment_duration(metric_identifier, ms_since_start);
AppMetrics::get_instance().increment_write_metrics(route_hash, ms_since_start);
- if(config.get_log_slow_requests_time_ms() > 0 && int(ms_since_start) > config.get_log_slow_requests_time_ms()) {
+ if(config.get_log_slow_requests_time_ms() >= 0 && int(ms_since_start) > config.get_log_slow_requests_time_ms()) {
// log slow request if logging is enabled
std::string query_string = "?";
for(const auto& kv: params) {
@@ -283,8 +285,13 @@ struct http_req {
}
}
std::string full_url_path = metric_identifier + query_string;
+ std::string req_ip = "0.0.0.0";
+ if(!is_diposed) {
+ req_ip = http_req::get_ip_addr(_req).ip;
+ }
+
LOG(INFO) << "SLOW REQUEST: " << "(" + std::to_string(ms_since_start) + " ms) "
- << http_req::get_ip_addr(_req).ip << " " << full_url_path;
+ << req_ip << " " << full_url_path;
}
}
}
diff --git a/include/http_server.h b/include/http_server.h
index f46b436c..328518aa 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -57,8 +57,10 @@ public:
h2o_generator_t* generator = nullptr;
- stream_response_state_t(h2o_req_t* _req): req(_req), is_res_start(req->res.status == 0) {
-
+ explicit stream_response_state_t(h2o_req_t* _req): req(_req) {
+ if(req != nullptr) {
+ is_res_start = (req->res.status == 0);
+ }
}
void set_response(uint32_t status_code, const std::string& content_type, const std::string& body) {
@@ -108,7 +110,8 @@ public:
async_req_res_t(const std::shared_ptr<http_req>& h_req, const std::shared_ptr<http_res>& h_res,
const bool destroy_after_use) :
- req(h_req), res(h_res), destroy_after_use(destroy_after_use), res_state(h_req->_req) {
+ req(h_req), res(h_res), destroy_after_use(destroy_after_use),
+ res_state((std::shared_lock(res->mres), h_req->is_diposed ? nullptr : h_req->_req)) {
std::shared_lock lk(res->mres);
diff --git a/src/http_server.cpp b/src/http_server.cpp
index 59cbaa4f..b5cdec99 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -270,6 +270,7 @@ void HttpServer::on_res_generator_dispose(void *self) {
custom_generator->res()->final = true;
custom_generator->res()->generator = nullptr;
custom_generator->res()->is_alive = false;
+ custom_generator->req()->is_diposed = true;
custom_generator->req()->notify();
custom_generator->res()->notify();
}
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index 85961a3a..9e56a8f3 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -88,7 +88,7 @@ void init_cmdline_options(cmdline::parser & options, int argc, char **argv) {
options.add<int>("snapshot-max-byte-count-per-rpc", '\0', "Maximum snapshot file size in bytes transferred for each RPC.", false, 4194304);
options.add<size_t>("healthy-read-lag", '\0', "Reads are rejected if the updates lag behind this threshold.", false, 1000);
options.add<size_t>("healthy-write-lag", '\0', "Writes are rejected if the updates lag behind this threshold.", false, 500);
- options.add<int>("log-slow-requests-time-ms", '\0', "When > 0, requests that take longer than this duration are logged.", false, -1);
+ options.add<int>("log-slow-requests-time-ms", '\0', "When >= 0, requests that take longer than this duration are logged.", false, -1);
options.add<uint32_t>("num-collections-parallel-load", '\0', "Number of collections that are loaded in parallel during start up.", false, 4);
options.add<uint32_t>("num-documents-parallel-load", '\0', "Number of documents per collection that are indexed in parallel during start up.", false, 1000);