mirror of
https://github.com/falcosecurity/falco.git
synced 2026-03-18 18:58:41 +00:00
refactor: use client to check if the web server is running
Using a local variable won't work since the server and the main application don't share the same memory space anymore. To check that the server is running, a call to the health endpoint is used. While the start of the server should be pretty fast, a simple exponential backoff has been implemented to allow for room in case the start is slowed for some reason. Signed-off-by: Samuel Gaist <samuel.gaist@idiap.ch>
This commit is contained in:
@@ -23,6 +23,8 @@ limitations under the License.
|
||||
#include <atomic>
|
||||
#include <signal.h>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
falco_webserver::~falco_webserver() {
|
||||
stop();
|
||||
}
|
||||
@@ -64,12 +66,12 @@ void falco_webserver::start(const falco::app::state &state,
|
||||
throw falco_exception("invalid webserver configuration");
|
||||
}
|
||||
|
||||
m_failed.store(false, std::memory_order_release);
|
||||
|
||||
// fork the server
|
||||
m_pid = fork();
|
||||
|
||||
if(m_pid == 0) {
|
||||
if(m_pid < 0) {
|
||||
throw falco_exception("Webserver: an error occurred while forking webserver");
|
||||
} else if(m_pid == 0) {
|
||||
falco_logger::log(falco_logger::level::INFO, "Webserver: forked\n");
|
||||
int res = setgid(webserver_config.m_uid);
|
||||
if(res != NOERROR) {
|
||||
@@ -90,10 +92,30 @@ void falco_webserver::start(const falco::app::state &state,
|
||||
} catch(std::exception &e) {
|
||||
falco_logger::log(falco_logger::level::ERR,
|
||||
"Webserver: " + std::string(e.what()) + "\n");
|
||||
m_failed.store(true, std::memory_order_release);
|
||||
}
|
||||
} else if(m_pid < 0) {
|
||||
throw falco_exception("Webserver: an error occurred while forking webserver");
|
||||
} else {
|
||||
std::string schema = "http";
|
||||
if(webserver_config.m_ssl_enabled) {
|
||||
schema = "https";
|
||||
}
|
||||
std::string url = schema + "://localhost:" + std::to_string(webserver_config.m_listen_port);
|
||||
httplib::Client cli(url);
|
||||
|
||||
const int max_retries = 10;
|
||||
std::chrono::seconds delay = 1s;
|
||||
int retry = 0;
|
||||
m_running = false;
|
||||
while(retry++ < max_retries) {
|
||||
if(auto res = cli.Get(webserver_config.m_k8s_healthz_endpoint)) {
|
||||
falco_logger::log(falco_logger::level::INFO, "Webserver: successfully started\n");
|
||||
m_running = true;
|
||||
break;
|
||||
}
|
||||
std::this_thread::sleep_for(delay * retry);
|
||||
}
|
||||
if(!m_running) {
|
||||
throw falco_exception("Webserver: the server is not running");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,5 @@ private:
|
||||
bool m_running = false;
|
||||
std::unique_ptr<httplib::Server> m_server = nullptr;
|
||||
std::thread m_server_thread;
|
||||
std::atomic<bool> m_failed;
|
||||
int m_pid = -1;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user