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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Defer deletion of old ssl context during SSL cert refresh.
Kishore Nallan60c03bce19e1
diff --git a/include/config.h b/include/config.hindex 4d475e0c..852acbb6 100644--- a/include/config.h+++ b/include/config.h@@ -26,6 +26,7 @@ private:std::string ssl_certificate;std::string ssl_certificate_key;+ uint32_t ssl_refresh_interval_seconds;bool enable_cors;@@ -60,6 +61,7 @@ protected:this->num_collections_parallel_load = 0; // will be set dynamically if not overriddenthis->num_documents_parallel_load = 1000;this->thread_pool_size = 0; // will be set dynamically if not overridden+ this->ssl_refresh_interval_seconds = 8 * 60 * 60;}Config(Config const&) {@@ -213,6 +215,10 @@ public:return this->thread_pool_size;}+ size_t get_ssl_refresh_interval_seconds() const {+ return this->ssl_refresh_interval_seconds;+ }+// loadersstd::string get_env(const char *name) {@@ -296,6 +302,10 @@ public:if(!get_env("TYPESENSE_THREAD_POOL_SIZE").empty()) {this->thread_pool_size = std::stoi(get_env("TYPESENSE_THREAD_POOL_SIZE"));}++ if(!get_env("TYPESENSE_SSL_REFRESH_INTERVAL_SECONDS").empty()) {+ this->ssl_refresh_interval_seconds = std::stoi(get_env("TYPESENSE_SSL_REFRESH_INTERVAL_SECONDS"));+ }}void load_config_file(cmdline::parser & options) {@@ -409,6 +419,10 @@ public:if(reader.Exists("server", "thread-pool-size")) {this->thread_pool_size = (int) reader.GetInteger("server", "thread-pool-size", 0);}++ if(reader.Exists("server", "ssl-refresh-interval-seconds")) {+ this->ssl_refresh_interval_seconds = (int) reader.GetInteger("server", "ssl-refresh-interval-seconds", 8 * 60 * 60);+ }}void load_config_cmd_args(cmdline::parser & options) {@@ -504,6 +518,10 @@ public:if(options.exist("thread-pool-size")) {this->thread_pool_size = options.get<uint32_t>("thread-pool-size");}++ if(options.exist("ssl-refresh-interval-seconds")) {+ this->ssl_refresh_interval_seconds = options.get<uint32_t>("ssl-refresh-interval-seconds");+ }}// validationdiff --git a/include/http_server.h b/include/http_server.hindex e402cb2c..ea7f8024 100644--- a/include/http_server.h+++ b/include/http_server.h@@ -78,7 +78,7 @@ private:static const size_t ACTIVE_STREAM_WINDOW_SIZE = 196605;static const size_t REQ_TIMEOUT_MS = 60000;- static const uint64_t SSL_REFRESH_INTERVAL_MS = 8 * 60 * 60 * 1000;+ const uint64_t SSL_REFRESH_INTERVAL_MS;h2o_custom_timer_t ssl_refresh_timer;h2o_custom_timer_t metrics_refresh_timer;@@ -117,6 +117,8 @@ private:static void on_ssl_refresh_timeout(h2o_timer_t *entry);+ static void on_ssl_ctx_delete_timeout(h2o_timer_t *entry);+static void on_metrics_refresh_timeout(h2o_timer_t *entry);int create_listener();@@ -141,6 +143,7 @@ public:const std::string & listen_address, uint32_t listen_port,const std::string & ssl_cert_path,const std::string & ssl_cert_key_path,+ const uint64_t ssl_refresh_interval_ms,bool cors_enabled, ThreadPool* thread_pool);~HttpServer();diff --git a/src/http_server.cpp b/src/http_server.cppindex d6e1c8f6..bf5b4540 100644--- a/src/http_server.cpp+++ b/src/http_server.cpp@@ -12,8 +12,9 @@#include "logger.h"HttpServer::HttpServer(const std::string & version, const std::string & listen_address,- uint32_t listen_port, const std::string & ssl_cert_path,- const std::string & ssl_cert_key_path, bool cors_enabled, ThreadPool* thread_pool):+ uint32_t listen_port, const std::string & ssl_cert_path, const std::string & ssl_cert_key_path,+ const uint64_t ssl_refresh_interval_ms, bool cors_enabled, ThreadPool* thread_pool):+ SSL_REFRESH_INTERVAL_MS(ssl_refresh_interval_ms),exit_loop(false), version(version), listen_address(listen_address), listen_port(listen_port),ssl_cert_path(ssl_cert_path), ssl_cert_key_path(ssl_cert_key_path),cors_enabled(cors_enabled), thread_pool(thread_pool) {@@ -74,23 +75,40 @@ void HttpServer::on_ssl_refresh_timeout(h2o_timer_t *entry) {LOG(INFO) << "Refreshing SSL certs from disk.";HttpServer *hs = static_cast<HttpServer*>(custom_timer->data);- if(!initialize_ssl_ctx(hs->ssl_cert_path.c_str(), hs->ssl_cert_key_path.c_str(), hs->accept_ctx)) {+ SSL_CTX* old_ssl_ctx = hs->accept_ctx->ssl_ctx;++ bool refresh_success = initialize_ssl_ctx(hs->ssl_cert_path.c_str(), hs->ssl_cert_key_path.c_str(), hs->accept_ctx);++ if (refresh_success) {+ // delete the old SSL context but after some time, to allow existing connections to drain+ h2o_custom_timer_t* ssl_ctx_delete_timer = new h2o_custom_timer_t(old_ssl_ctx);+ h2o_timer_init(&ssl_ctx_delete_timer->timer, on_ssl_ctx_delete_timeout);+ uint64_t delete_lag = std::max<uint64_t>(60 * 1000, hs->SSL_REFRESH_INTERVAL_MS / 2);+ h2o_timer_link(hs->ctx.loop, delete_lag, &ssl_ctx_delete_timer->timer);+ } else {LOG(ERROR) << "SSL cert refresh failed.";}// link the timer for the next cycle- h2o_timer_link(- hs->ctx.loop,- SSL_REFRESH_INTERVAL_MS,- &hs->ssl_refresh_timer.timer- );+ h2o_timer_link(hs->ctx.loop, hs->SSL_REFRESH_INTERVAL_MS, &hs->ssl_refresh_timer.timer);+}++void HttpServer::on_ssl_ctx_delete_timeout(h2o_timer_t *entry) {+ LOG(INFO) << "Deleting old SSL context.";++ h2o_custom_timer_t* custom_timer = reinterpret_cast<h2o_custom_timer_t*>(entry);+ SSL_CTX* old_ssl_ctx = static_cast<SSL_CTX*>(custom_timer->data);+ SSL_CTX_free(old_ssl_ctx);+ delete custom_timer;}int HttpServer::setup_ssl(const char *cert_file, const char *key_file) {// Set up a timer to refresh SSL config from disk. Also, initializing upfront so that destructor worksssl_refresh_timer = h2o_custom_timer_t(this);h2o_timer_init(&ssl_refresh_timer.timer, on_ssl_refresh_timeout);- h2o_timer_link(ctx.loop, SSL_REFRESH_INTERVAL_MS, &ssl_refresh_timer.timer); // every 8 hours+ h2o_timer_link(ctx.loop, SSL_REFRESH_INTERVAL_MS, &ssl_refresh_timer.timer);++ LOG(INFO) << "SSL cert refresh interval: " << (SSL_REFRESH_INTERVAL_MS / 1000) << "s";if(!initialize_ssl_ctx(cert_file, key_file, accept_ctx)) {return -1;@@ -956,14 +974,8 @@ bool HttpServer::initialize_ssl_ctx(const char *cert_file, const char *key_file,}h2o_ssl_register_alpn_protocols(new_ctx, h2o_http2_alpn_protocols);-- SSL_CTX* old_ctx = accept_ctx->ssl_ctx;accept_ctx->ssl_ctx = new_ctx;- if(old_ctx != nullptr) {- SSL_CTX_free(old_ctx);- }-return true;}diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cppindex c34945ff..cb668ca1 100644--- a/src/typesense_server_utils.cpp+++ b/src/typesense_server_utils.cpp@@ -73,6 +73,7 @@ void init_cmdline_options(cmdline::parser & options, int argc, char **argv) {options.add<std::string>("ssl-certificate", 'c', "Path to the SSL certificate file.", false, "");options.add<std::string>("ssl-certificate-key", 'k', "Path to the SSL certificate key file.", false, "");+ options.add<uint32_t>("ssl-refresh-interval-seconds", '\0', "Frequency of automatic reloading of SSL certs from disk.", false, 8 * 60 * 60);options.add("enable-cors", '\0', "Enable CORS requests.");@@ -377,6 +378,7 @@ int run_server(const Config & config, const std::string & version, void (*masterconfig.get_api_port(),config.get_ssl_cert(),config.get_ssl_cert_key(),+ config.get_ssl_refresh_interval_seconds() * 1000,config.get_enable_cors(),&server_thread_pool);