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

Make route_hash unsigned.

kishorenc89690101aedb
diff --git a/include/http_data.h b/include/http_data.h
index 7080bbc5..d413e6b5 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -96,20 +96,20 @@ struct http_res {
};
enum class ROUTE_CODES {
- NOT_FOUND = -1,
- ALREADY_HANDLED = -2,
+ NOT_FOUND = 1,
+ ALREADY_HANDLED = 2,
};
struct http_req {
h2o_req_t* _req;
std::string http_method;
- int route_hash;
+ uint64_t route_hash;
std::map<std::string, std::string> params;
std::string body;
- http_req(): route_hash(-1) {}
+ http_req(): route_hash(1) {}
- http_req(h2o_req_t* _req, const std::string & http_method, size_t route_hash,
+ http_req(h2o_req_t* _req, const std::string & http_method, uint64_t route_hash,
const std::map<std::string, std::string> & params, std::string body):
_req(_req), http_method(http_method), route_hash(route_hash), params(params), body(body) {}
@@ -153,7 +153,8 @@ struct route_path {
uint64_t route_hash() {
std::string path = StringUtils::join(path_parts, "/");
std::string method_path = http_method + path;
- return StringUtils::hash_wy(method_path.c_str(), method_path.size());
+ uint64_t hash = StringUtils::hash_wy(method_path.c_str(), method_path.size());
+ return (hash > 100) ? hash : (hash + 100); // [0-99] reserved for special codes
}
};
diff --git a/include/http_server.h b/include/http_server.h
index 125c9f66..70928dc6 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -34,7 +34,7 @@ private:
std::string version;
- std::vector<std::pair<uint32_t, route_path>> routes;
+ std::vector<std::pair<uint64_t, route_path>> routes;
const std::string listen_address;
@@ -90,7 +90,7 @@ public:
void send_response(http_req* request, const http_res* response);
- int find_route(const std::vector<std::string> & path_parts, const std::string & http_method,
+ uint64_t find_route(const std::vector<std::string> & path_parts, const std::string & http_method,
route_path** found_rpath);
bool get_route(uint64_t hash, route_path** found_rpath);
diff --git a/src/core_api.cpp b/src/core_api.cpp
index fd703902..faf68740 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -573,10 +573,10 @@ bool async_write_request(void *data) {
AsyncIndexArg* index_arg = static_cast<AsyncIndexArg*>(data);
std::unique_ptr<AsyncIndexArg> index_arg_guard(index_arg);
- if(index_arg->req->route_hash == static_cast<int>(ROUTE_CODES::NOT_FOUND)) {
+ if(index_arg->req->route_hash == static_cast<uint64_t>(ROUTE_CODES::NOT_FOUND)) {
// route not found
index_arg->res->send_400("Not found.");
- } else if(index_arg->req->route_hash != static_cast<int>(ROUTE_CODES::ALREADY_HANDLED)) {
+ } else if(index_arg->req->route_hash != static_cast<uint64_t>(ROUTE_CODES::ALREADY_HANDLED)) {
// call the underlying http handler
route_path* found_rpath = nullptr;
bool route_found = server->get_route(index_arg->req->route_hash, &found_rpath);
diff --git a/src/http_server.cpp b/src/http_server.cpp
index 84664df3..e6645142 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -212,7 +212,7 @@ std::map<std::string, std::string> HttpServer::parse_query(const std::string& qu
return query_map;
}
-int HttpServer::find_route(const std::vector<std::string> & path_parts, const std::string & http_method, route_path** found_rpath) {
+uint64_t HttpServer::find_route(const std::vector<std::string> & path_parts, const std::string & http_method, route_path** found_rpath) {
for (const auto& index_route : routes) {
const route_path & rpath = index_route.second;
@@ -237,7 +237,7 @@ int HttpServer::find_route(const std::vector<std::string> & path_parts, const st
}
}
- return static_cast<int>(ROUTE_CODES::NOT_FOUND);
+ return static_cast<uint64_t>(ROUTE_CODES::NOT_FOUND);
}
int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {
@@ -309,9 +309,9 @@ int HttpServer::catch_all_handler(h2o_handler_t *_self, h2o_req_t *req) {
}
route_path *rpath = nullptr;
- int route_hash = self->http_server->find_route(path_parts, http_method, &rpath);
+ uint64_t route_hash = self->http_server->find_route(path_parts, http_method, &rpath);
- if(route_hash != -1) {
+ if(route_hash != static_cast<uint64_t>(ROUTE_CODES::NOT_FOUND)) {
bool authenticated = self->http_server->auth_handler(*rpath, auth_key_from_header);
if(!authenticated) {
return send_401_unauthorized(req);
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index 739131f5..9032dd7f 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -98,6 +98,7 @@ int ReplicationState::start(const std::string & peering_address, int peering_por
if(!status.ok()) {
LOG(ERROR) << status.error_str();
}
+ init_readiness_count++; // sometimes, single node on_leader event does not fire
}
this->node = node;
@@ -111,7 +112,7 @@ void ReplicationState::write(http_req* request, http_res* response) {
LOG(ERROR) << "Rejecting write: could not find a leader.";
response->send_500("Could not find a leader.");
auto replication_arg = new AsyncIndexArg{request, response, nullptr};
- replication_arg->req->route_hash = static_cast<int>(ROUTE_CODES::ALREADY_HANDLED);
+ replication_arg->req->route_hash = static_cast<uint64_t>(ROUTE_CODES::ALREADY_HANDLED);
return message_dispatcher->send_message(REPLICATION_MSG, replication_arg);
}
@@ -142,7 +143,7 @@ void ReplicationState::write(http_req* request, http_res* response) {
}
auto replication_arg = new AsyncIndexArg{request, response, nullptr};
- replication_arg->req->route_hash = static_cast<int>(ROUTE_CODES::ALREADY_HANDLED);
+ replication_arg->req->route_hash = static_cast<uint64_t>(ROUTE_CODES::ALREADY_HANDLED);
message_dispatcher->send_message(REPLICATION_MSG, replication_arg);
});