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

Fix response streaming regression.

Kishore Nallan974280a4d580
diff --git a/include/http_data.h b/include/http_data.h
index c435e8d7..d96dee69 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -205,6 +205,49 @@ public:
virtual ~req_state_t() = default;
};
+struct stream_response_state_t {
+private:
+
+ h2o_req_t* req = nullptr;
+
+public:
+
+ bool is_req_early_exit = false;
+
+ bool is_res_start = true;
+ h2o_send_state_t send_state = H2O_SEND_STATE_IN_PROGRESS;
+
+ std::string res_body;
+ h2o_iovec_t res_buff;
+
+ std::string res_content_type;
+ int status = 0;
+ const char* reason = nullptr;
+
+ h2o_generator_t* generator = nullptr;
+
+ void set_response(uint32_t status_code, const std::string& content_type, std::string& body) {
+ std::string().swap(res_body);
+ res_body = std::move(body);
+ res_buff = h2o_iovec_t{.base = res_body.data(), .len = res_body.size()};
+
+ if(is_res_start) {
+ res_content_type = std::move(content_type);
+ status = (int)status_code;
+ reason = http_res::get_status_reason(status_code);
+ is_res_start = false;
+ }
+ }
+
+ void set_req(h2o_req_t* _req) {
+ req = _req;
+ }
+
+ h2o_req_t* get_req() {
+ return req;
+ }
+};
+
struct http_req {
static constexpr const char* AUTH_HEADER = "x-typesense-api-key";
static constexpr const char* USER_HEADER = "x-typesense-user-id";
@@ -248,6 +291,9 @@ struct http_req {
std::atomic<bool> is_diposed;
std::string client_ip = "0.0.0.0";
+ // stores http lib related datastructures to avoid race conditions between indexing and http write threads
+ stream_response_state_t res_state;
+
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),
diff --git a/include/http_server.h b/include/http_server.h
index 1ced92e7..b83ae775 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -40,50 +40,6 @@ struct h2o_custom_generator_t {
}
};
-struct stream_response_state_t {
-private:
-
- h2o_req_t* req = nullptr;
-
-public:
-
- bool is_req_early_exit = false;
-
- bool is_res_start = true;
- h2o_send_state_t send_state = H2O_SEND_STATE_IN_PROGRESS;
-
- std::string res_body;
- h2o_iovec_t res_buff;
-
- h2o_iovec_t res_content_type{};
- int status = 0;
- const char* reason = nullptr;
-
- h2o_generator_t* generator = nullptr;
-
- 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, std::string& body) {
- std::string().swap(res_body);
- res_body = std::move(body);
- res_buff = h2o_iovec_t{.base = res_body.data(), .len = res_body.size()};
-
- if(is_res_start) {
- res_content_type = h2o_strdup(&req->pool, content_type.c_str(), SIZE_MAX);
- status = status_code;
- reason = http_res::get_status_reason(status_code);
- }
- }
-
- h2o_req_t* get_req() {
- return req;
- }
-};
-
struct deferred_req_res_t {
const std::shared_ptr<http_req> req;
const std::shared_ptr<http_res> res;
@@ -110,13 +66,9 @@ public:
// used to manage lifecycle of async actions
const bool destroy_after_use;
- // stores http lib related datastructures to avoid race conditions between indexing and http write threads
- stream_response_state_t res_state;
-
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((std::shared_lock(res->mres), h_req->is_diposed ? nullptr : h_req->_req)) {
+ req(h_req), res(h_res), destroy_after_use(destroy_after_use) {
std::shared_lock lk(res->mres);
@@ -124,12 +76,10 @@ public:
return;
}
- // ***IMPORTANT***
- // We limit writing to fields of `res_state.req` to prevent race conditions with http thread
- // Check `HttpServer::stream_response()` for overlapping writes.
-
h2o_custom_generator_t* res_generator = static_cast<h2o_custom_generator_t*>(res->generator.load());
+ auto& res_state = req->res_state;
+ res_state.set_req(h_req->is_diposed ? nullptr : h_req->_req);
res_state.is_req_early_exit = (res_generator->rpath->async_req && res->final && !req->last_chunk_aggregate);
res_state.send_state = res->final ? H2O_SEND_STATE_FINAL : H2O_SEND_STATE_IN_PROGRESS;
res_state.generator = (res_generator == nullptr) ? nullptr : &res_generator->h2o_generator;
@@ -147,6 +97,10 @@ public:
void res_notify() {
return res->notify();
}
+
+ stream_response_state_t& get_res_state() {
+ return req->res_state;
+ }
};
struct defer_processing_t {
diff --git a/src/http_server.cpp b/src/http_server.cpp
index 813ee450..661a83ba 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -808,9 +808,11 @@ void HttpServer::stream_response(stream_response_state_t& state) {
h2o_req_t* req = state.get_req();
- if(state.is_res_start) {
+ bool start_of_res = (req->res.status == 0);
+
+ if(start_of_res) {
h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL,
- state.res_content_type.base, state.res_content_type.len);
+ state.res_content_type.data(), state.res_content_type.size());
req->res.status = state.status;
req->res.reason = state.reason;
}
@@ -829,7 +831,7 @@ void HttpServer::stream_response(stream_response_state_t& state) {
return ;
}
- if (state.is_res_start) {
+ if (start_of_res) {
/*LOG(INFO) << "h2o_start_response, content_type=" << state.res_content_type
<< ",response.status_code=" << state.res_status_code;*/
h2o_start_response(req, state.generator);
@@ -969,7 +971,7 @@ bool HttpServer::on_stream_response_message(void *data) {
// NOTE: access to `req` and `res` objects must be synchronized and wrapped by `req_res`
if(req_res->is_alive()) {
- stream_response(req_res->res_state);
+ stream_response(req_res->get_res_state());
} else {
// serialized request or generator has been disposed (underlying request is probably dead)
req_res->req_notify();