From 8f59c394961c4d99cc98be2c84c864bfdbd97d19 Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Thu, 21 Nov 2024 10:20:23 +0800 Subject: [PATCH] enhance http healthcheck --- common/src/http_healthcheck.cpp | 66 +++++++++++++++++++++++++++++---- platform/src/packet_adapter.cpp | 24 ------------ 2 files changed, 58 insertions(+), 32 deletions(-) diff --git a/common/src/http_healthcheck.cpp b/common/src/http_healthcheck.cpp index 30d1fd5..f36d59f 100644 --- a/common/src/http_healthcheck.cpp +++ b/common/src/http_healthcheck.cpp @@ -12,6 +12,8 @@ struct http_status_ctx { pthread_t pthread; struct event_base *base; + struct event *ev; + struct evhttp *http; unsigned int enable; unsigned int port; @@ -25,19 +27,67 @@ static void http_callback(struct evhttp_request *req, void *arg) evhttp_send_reply(req, HTTP_OK, "", NULL); } +static void gc_callback(evutil_socket_t fd, short what, void *arg) +{ + // do nothing + + // add gc_callback to avoid event_base_dispatch blocking, handle event_base_loopbreak inmediately +} + 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); + struct timeval gc_delay = {0, 500 * 1000}; // Microseconds, we set 500 miliseconds here. + printf("[http_healthcheck_thread]: running (listen: %s:%d path: %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); + if (g_ctx.base == NULL) + { + printf("[http_healthcheck_thread]: event_base_new failed\n"); + goto end; + } - printf("http_healthcheck_thread exit\n"); + g_ctx.ev = event_new(g_ctx.base, -1, EV_PERSIST, gc_callback, NULL); + if (g_ctx.ev == NULL) + { + printf("[http_healthcheck_thread]: event_new failed\n"); + goto end; + } + + if (evtimer_add(g_ctx.ev, &gc_delay) < 0) + { + printf("[http_healthcheck_thread]: evtimer_add failed\n"); + goto end; + } + + g_ctx.http = evhttp_new(g_ctx.base); + if (evhttp_set_cb(g_ctx.http, g_ctx.path, http_callback, NULL) < 0) + { + printf("[http_healthcheck_thread]: evhttp_set_cb failed\n"); + goto end; + } + + if (evhttp_bind_socket(g_ctx.http, g_ctx.addr, g_ctx.port) < 0) + { + printf("[http_healthcheck_thread]: evhttp_bind_socket failed\n"); + goto end; + } + event_base_dispatch(g_ctx.base); + +end: + if (g_ctx.http) + { + evhttp_free(g_ctx.http); + } + if (g_ctx.ev) + { + event_free(g_ctx.ev); + } + if (g_ctx.base) + { + event_base_free(g_ctx.base); + } + + printf("[http_healthcheck_thread]: exit\n"); return NULL; } diff --git a/platform/src/packet_adapter.cpp b/platform/src/packet_adapter.cpp index 6c83e19..5739a09 100644 --- a/platform/src/packet_adapter.cpp +++ b/platform/src/packet_adapter.cpp @@ -33,7 +33,6 @@ struct thread struct runtime_ctx { - int enable_debug; int need_stop; struct metrics metrics; @@ -92,16 +91,6 @@ error_out: static void signal_handler(int signo) { - if (signo == SIGUSR1) - { - runtime->enable_debug = 1; - LOG_ERROR("%s: received SIGUSR1, enable debug", LOG_MAIN); - } - if (signo == SIGUSR2) - { - runtime->enable_debug = 0; - LOG_ERROR("%s: received SIGUSR2, disable debug", LOG_MAIN); - } if (signo == SIGHUP) { LOG_RELOAD(); @@ -130,9 +119,6 @@ static void usage(char *cmd) fprintf(stderr, " -v -- show version\n"); fprintf(stderr, " -d -- run daemon\n"); fprintf(stderr, " -h -- show help\n"); - fprintf(stderr, "Signal: \n"); - fprintf(stderr, " kill -s SIGUSR1 `pidof %s` -- enable debug\n", cmd); - fprintf(stderr, " kill -s SIGUSR2 `pidof %s` -- disable debug\n", cmd); } int main(int argc, char **argv) @@ -164,16 +150,6 @@ int main(int argc, char **argv) LOG_ERROR("%s: TSG Packet Adapter Engine, Version: %s Start ...", LOG_MAIN, Packet_Adapter_Version); - if (signal(SIGUSR1, signal_handler) == SIG_ERR) - { - LOG_ERROR("%s: failed at signal(SIGUSR1), %d: %s", LOG_MAIN, errno, strerror(errno)); - goto error; - } - if (signal(SIGUSR2, signal_handler) == SIG_ERR) - { - LOG_ERROR("%s: failed at signal(SIGUSR2), %d: %s", LOG_MAIN, errno, strerror(errno)); - goto error; - } if (signal(SIGHUP, signal_handler) == SIG_ERR) { LOG_ERROR("%s: failed at signal(SIGHUP), %d: %s", LOG_MAIN, errno, strerror(errno));