diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index c992c3d..6a9adcc 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -5,7 +5,7 @@ src/tap.cpp src/io_uring.cpp src/intercept_policy.cpp src/tfe_fieldstat.cpp src/tuple.cpp src/tfe_packet_io.cpp src/tfe_session_table.cpp src/tfe_ctrl_packet.cpp src/packet.cpp src/tfe_packet_io_fs.cpp - src/mpack.cpp src/dablooms.cpp src/murmur.cpp src/timestamp.cpp) + src/mpack.cpp src/dablooms.cpp src/murmur.cpp src/timestamp.cpp src/http_healthcheck.cpp) target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include) target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../bpf/) target_include_directories(common PRIVATE ${CMAKE_CURRENT_LIST_DIR}/../platform/include/internal) diff --git a/common/include/http_healthcheck.h b/common/include/http_healthcheck.h new file mode 100644 index 0000000..a883984 --- /dev/null +++ b/common/include/http_healthcheck.h @@ -0,0 +1,24 @@ +#ifndef _HTTP_HEALTHCHECK_H +#define _HTTP_HEALTHCHECK_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +/* + * [http_healthcheck_server] + * enable=1 + * port=8080 + * addr="0.0.0.0" + * path="/status" + */ + +void http_healthcheck_server_start(const char *profile); +void http_healthcheck_server_stop(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/common/src/http_healthcheck.cpp b/common/src/http_healthcheck.cpp new file mode 100644 index 0000000..30d1fd5 --- /dev/null +++ b/common/src/http_healthcheck.cpp @@ -0,0 +1,66 @@ +#include +#include +#include + +#include "event2/event.h" +#include "event2/buffer.h" +#include "event2/http.h" +#include "http_healthcheck.h" +#include + +struct http_status_ctx +{ + pthread_t pthread; + struct event_base *base; + + unsigned int enable; + unsigned int port; + char addr[PATH_MAX]; + char path[PATH_MAX]; + uint64_t is_error; +} g_ctx = {0}; + +static void http_callback(struct evhttp_request *req, void *arg) +{ + evhttp_send_reply(req, HTTP_OK, "", NULL); +} + +static void *http_healthcheck_thread(void *arg) +{ + printf("http_healthcheck_thread running (%s:%d/%s)\n", g_ctx.addr, g_ctx.port, g_ctx.path); + + g_ctx.base = event_base_new(); + struct evhttp *http = evhttp_new(g_ctx.base); + evhttp_set_cb(http, g_ctx.path, http_callback, NULL); + evhttp_bind_socket(http, g_ctx.addr, g_ctx.port); + event_base_dispatch(g_ctx.base); + evhttp_free(http); + event_base_free(g_ctx.base); + + printf("http_healthcheck_thread exit\n"); + return NULL; +} + +void http_healthcheck_server_start(const char *profile) +{ + MESA_load_profile_uint_def(profile, "http_healthcheck_server", "enable", &g_ctx.enable, 0); + MESA_load_profile_uint_def(profile, "http_healthcheck_server", "port", &g_ctx.port, 8080); + MESA_load_profile_string_def(profile, "http_healthcheck_server", "addr", g_ctx.addr, sizeof(g_ctx.addr), "127.0.0.1"); + MESA_load_profile_string_def(profile, "http_healthcheck_server", "path", g_ctx.path, sizeof(g_ctx.path), "/status"); + + if (!g_ctx.enable) + { + return; + } + + pthread_create(&g_ctx.pthread, NULL, http_healthcheck_thread, (void *)&g_ctx); +} + +void http_healthcheck_server_stop() +{ + if (g_ctx.enable) + { + event_base_loopbreak(g_ctx.base); + pthread_join(g_ctx.pthread, NULL); + } +} diff --git a/conf/tfe/tfe.conf b/conf/tfe/tfe.conf index a43a381..386da69 100644 --- a/conf/tfe/tfe.conf +++ b/conf/tfe/tfe.conf @@ -266,3 +266,9 @@ buff_size=2048 # IORING_SETUP_SUBMIT_ALL (1U << 7) /* continue submit on error */ flags=0 sq_thread_idle=0 + +[http_healthcheck_server] +enable=1 +port=8080 +addr="0.0.0.0" +path="/status" diff --git a/platform/src/proxy.cpp b/platform/src/proxy.cpp index b8d89c9..45d6a1d 100644 --- a/platform/src/proxy.cpp +++ b/platform/src/proxy.cpp @@ -54,6 +54,7 @@ #include #include #include +#include /* Breakpad */ #include @@ -717,6 +718,8 @@ int main(int argc, char * argv[]) TFE_LOG_ERROR(g_default_logger, "Tango Frontend Engine initialized, Version: %s.", __tfe_version); + http_healthcheck_server_start(main_profile); + /* If TFE is run by systemd's notify, then tell the systemd our tfe is ready. * and disable the stderr log, only print logs into files */ if(check_is_started_by_notify()) @@ -729,6 +732,9 @@ int main(int argc, char * argv[]) worker_thread_ready = 1; event_base_dispatch(g_default_proxy->evbase); + + http_healthcheck_server_stop(); + return 0; }