From b6b549a68d8c967da9c2d5c6b933b7609fb7ce89 Mon Sep 17 00:00:00 2001 From: "Y.Horie" Date: Sat, 12 Sep 2026 16:24:03 +0900 Subject: [PATCH] refactor: let a variable read the kind of the member it was given The table says of every member whether it is a counter or a queue, and the two are read differently. The variables were handed the offset and not the kind, so the one that has to average a queue recognised itself by comparing its offset against stat_request_times - the only queue a variable has ever named. The knowledge that a field is a queue sat in two places, and the copy in variables.c named a single field. Hand the row instead of the offset, and ask it. The offset is still what the value is read at, so nothing moves: of the eighteen members a variable can read, one is a queue and it is that one, and every one of the eighteen takes the branch it took before. While here, say why responseMsecCounter and responseMsec are the two members no variable reads, which the table did not. A variable is answered from the server zone of the request, and the upstream times are written to the node of the peer that served it, in shm.c. On a server zone they are the zeroes the node was created with, so a variable named here would report 0 for every request, for ever, with nothing to say it was wrong - before this change and after it alike, since a zeroed queue averages to zero. set_by_filter names them because it is told which zone to read, and can be told an upstream one. --- src/ngx_http_vhost_traffic_status_node.c | 11 +++++++++++ src/ngx_http_vhost_traffic_status_variables.c | 14 +++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/ngx_http_vhost_traffic_status_node.c b/src/ngx_http_vhost_traffic_status_node.c index 6d0c948..22e3803 100644 --- a/src/ngx_http_vhost_traffic_status_node.c +++ b/src/ngx_http_vhost_traffic_status_node.c @@ -909,6 +909,17 @@ ngx_http_vhost_traffic_status_member_t NGX_HTTP_VHOST_TRAFFIC_STATUS_MEMBER_QUEUE, ngx_vts_member(stat_request_times) }, + /* + * These two have no variable because there is nothing for one to read. + * A variable is answered from the server zone of the request, and the + * upstream times are written to the node of the peer that served it, in + * shm.c. On a server zone they are the zeroes the node was created with, + * so a variable named here would report 0 for every request forever. + * + * set_by_filter can name them because it is given the zone to read, and + * an upstream zone is one of the zones it can be given. + */ + { ngx_null_string, ngx_string("responseMsecCounter"), ngx_null_string, NGX_HTTP_VHOST_TRAFFIC_STATUS_MEMBER_COUNTER, ngx_vts_member(stat_upstream.response_time_counter) }, diff --git a/src/ngx_http_vhost_traffic_status_variables.c b/src/ngx_http_vhost_traffic_status_variables.c index e6e434e..1e71078 100644 --- a/src/ngx_http_vhost_traffic_status_variables.c +++ b/src/ngx_http_vhost_traffic_status_variables.c @@ -20,8 +20,11 @@ ngx_http_vhost_traffic_status_node_variable(ngx_http_request_t *r, ngx_slab_pool_t *shpool; ngx_rbtree_node_t *node; ngx_http_vhost_traffic_status_node_t *vtsn; + ngx_http_vhost_traffic_status_member_t *m; ngx_http_vhost_traffic_status_loc_conf_t *vtscf; + m = (ngx_http_vhost_traffic_status_member_t *) data; + vtscf = ngx_http_get_module_loc_conf(r, ngx_http_vhost_traffic_status_module); ngx_http_vhost_traffic_status_find_name(r, &dst); @@ -54,16 +57,17 @@ ngx_http_vhost_traffic_status_node_variable(ngx_http_request_t *r, vtsn = (ngx_http_vhost_traffic_status_node_t *) &node->color; - if (data == offsetof(ngx_http_vhost_traffic_status_node_t, stat_request_times)) { + if (m->kind == NGX_HTTP_VHOST_TRAFFIC_STATUS_MEMBER_QUEUE) { /* the queue is the value, there is no counter kept for it */ value = (ngx_atomic_t) ngx_http_vhost_traffic_status_node_time_queue_average( - &vtsn->stat_request_times, vtscf->average_method, - vtscf->average_period); + (ngx_http_vhost_traffic_status_node_time_queue_t *) + ((char *) vtsn + m->offset), + vtscf->average_method, vtscf->average_period); } else { - value = *((ngx_atomic_t *) ((char *) vtsn + data)); + value = *((ngx_atomic_t *) ((char *) vtsn + m->offset)); } v->len = ngx_sprintf(p, "%uA", value) - p; @@ -117,7 +121,7 @@ ngx_http_vhost_traffic_status_add_variables(ngx_conf_t *cf) } var->get_handler = ngx_http_vhost_traffic_status_node_variable; - var->data = (uintptr_t) m->offset; + var->data = (uintptr_t) m; } return NGX_OK;