From 98c1186cb8d4e8902c7d5156ac90f7ae9cd60670 Mon Sep 17 00:00:00 2001 From: Soner Tari Date: Sun, 24 Mar 2019 01:31:19 +0300 Subject: [PATCH] Improve documentation, and simplify code --- pxythrmgr.c | 2 +- pxythrmgr.h | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pxythrmgr.c b/pxythrmgr.c index 8302622..c1fb7b6 100644 --- a/pxythrmgr.c +++ b/pxythrmgr.c @@ -275,7 +275,7 @@ pxy_thrmgr_timer_cb(UNUSED evutil_socket_t fd, UNUSED short what, UNUSED void *a // @attention Print thread info only if stats logging is enabled, if disabled debug logs are not printed either if (ctx->thrmgr->opts->statslog) { ctx->timeout_count++; - if (ctx->timeout_count * ctx->thrmgr->opts->expired_conn_check_period >= ctx->thrmgr->opts->stats_period * ctx->thrmgr->opts->expired_conn_check_period) { + if (ctx->timeout_count >= ctx->thrmgr->opts->stats_period) { ctx->timeout_count = 0; pxy_thrmgr_print_thr_info(ctx); } diff --git a/pxythrmgr.h b/pxythrmgr.h index 51bd37f..f3fa968 100644 --- a/pxythrmgr.h +++ b/pxythrmgr.h @@ -53,7 +53,12 @@ typedef struct pxy_thr_ctx { struct event_base *evbase; struct evdns_base *dnsbase; int running; - unsigned int timeout_count; + + // Per-thread locking is necessary during connection setup and termination + // to prevent multithreading issues between thrmgr thread and conn handling threads + pthread_mutex_t mutex; + + // Statistics evutil_socket_t max_fd; size_t max_load; size_t timedout_conns; @@ -64,10 +69,19 @@ typedef struct pxy_thr_ctx { long long unsigned int intif_out_bytes; long long unsigned int extif_in_bytes; long long unsigned int extif_out_bytes; + // Each stats has an id, incremented on each stats print unsigned short stats_id; + // Used to print statistics, compared against stats_period + unsigned int timeout_count; + + // List of active connections on the thread pxy_conn_ctx_t *conns; + + // Per-thread sqlite stmt is necessary to prevent multithreading issues between threads struct sqlite3_stmt *get_user; - pthread_mutex_t mutex; + + // SSL conns wait for the first readcb to complete connection setup + // We keep track of conns at that stage using this list, to close them if they time out pxy_conn_ctx_t *pending_ssl_conns; long long unsigned int pending_ssl_conn_count; } pxy_thr_ctx_t; @@ -76,6 +90,8 @@ struct pxy_thrmgr_ctx { int num_thr; opts_t *opts; pxy_thr_ctx_t **thr; + // Provides unique conn id, always goes up, never down + // There is no risk of collision if/when it rolls back to 0 long long unsigned int conn_count; };