From efd36b0270e60258e879bcc458e195ab721bcadd Mon Sep 17 00:00:00 2001 From: Behnam M <58621210+ibehnam@users.noreply.github.com> Date: Wed, 10 Jan 2024 14:39:37 -0500 Subject: [PATCH] made ServerState atomic and turned two-line spaces into one-line --- examples/server/server.cpp | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/examples/server/server.cpp b/examples/server/server.cpp index 18212c4f2..1cca634d5 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #ifndef SERVER_VERBOSE #define SERVER_VERBOSE 1 @@ -146,15 +147,12 @@ static std::vector base64_decode(const std::string & encoded_string) // parallel // - enum ServerState { LOADING_MODEL, // Server is starting up, model not fully loaded yet READY, // Server is ready and model is loaded ERROR // An error occurred, load_model failed }; - - enum task_type { COMPLETION_TASK, CANCEL_TASK @@ -2462,7 +2460,6 @@ static void server_params_parse(int argc, char **argv, server_params &sparams, } } - static std::string random_string() { static const std::string str("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); @@ -2801,14 +2798,15 @@ int main(int argc, char **argv) httplib::Server svr; - ServerState server_state = LOADING_MODEL; + std::atomic server_state{LOADING_MODEL}; svr.set_default_headers({{"Server", "llama.cpp"}, {"Access-Control-Allow-Origin", "*"}, {"Access-Control-Allow-Headers", "content-type"}}); svr.Get("/health", [&](const httplib::Request&, httplib::Response& res) { - switch(server_state) { + ServerState current_state = server_state.load(); + switch(current_state) { case READY: res.set_content(R"({"status": "ok"})", "application/json"); res.status = 200; // HTTP OK @@ -2887,29 +2885,27 @@ int main(int argc, char **argv) log_data["api_key"] = "api_key: ****" + sparams.api_key.substr(sparams.api_key.length() - 4); } - LOG_INFO("HTTP server listening", log_data); // run the HTTP server in a thread - see comment below std::thread t([&]() { if (!svr.listen_after_bind()) { - server_state = ERROR; + server_state.store(ERROR); return 1; } return 0; }); - // load the model if (!llama.load_model(params)) { - server_state = ERROR; + server_state.store(ERROR); return 1; } else { llama.initialize(); - server_state = READY; + server_state.store(READY); } // Middleware for API key validation @@ -2938,7 +2934,6 @@ int main(int argc, char **argv) return false; }; - // this is only called if no index.html is found in the public --path svr.Get("/", [](const httplib::Request &, httplib::Response &res) { @@ -3046,7 +3041,6 @@ int main(int argc, char **argv) } }); - svr.Get("/v1/models", [¶ms](const httplib::Request&, httplib::Response& res) { std::time_t t = std::time(0);