Skip to content

Commit 46595ad

Browse files
maximk-1denji
authored andcommitted
feat: expose $cache_purge_queue_size and $cache_purge_queue_max_size variables
Register two read-only nginx variables via postconfiguration hook: - $cache_purge_queue_size: live atomic read of queue->size - $cache_purge_queue_max_size: plain read of queue->max_size (immutable) Both return "-" when background purge is disabled. Covered by tests 7 and 8 in t/background_queue.t.
1 parent f880097 commit 46595ad

2 files changed

Lines changed: 147 additions & 1 deletion

File tree

ngx_cache_purge_module.c

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ static ngx_uint_t ngx_http_cache_purge_hash_key(ngx_str_t *cache_path,
216216
static ngx_http_cache_purge_queue_item_t *ngx_http_cache_purge_find_duplicate(
217217
ngx_http_cache_purge_queue_t *queue, ngx_uint_t hash,
218218
ngx_str_t *cache_path, ngx_str_t *key);
219+
static ngx_int_t ngx_http_cache_purge_add_variables(ngx_conf_t *cf);
220+
static ngx_int_t ngx_http_cache_purge_queue_size_variable(
221+
ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
222+
static ngx_int_t ngx_http_cache_purge_queue_max_size_variable(
223+
ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
219224

220225
# if (NGX_HTTP_FASTCGI)
221226
char *ngx_http_fastcgi_cache_purge_conf(ngx_conf_t *cf,
@@ -301,6 +306,21 @@ char *ngx_http_cache_purge_merge_loc_conf(ngx_conf_t *cf,
301306
void *parent, void *child);
302307

303308

309+
/* -- variable table ----------------------------------------------------- */
310+
311+
static ngx_http_variable_t ngx_http_cache_purge_vars[] = {
312+
313+
{ ngx_string("cache_purge_queue_size"), NULL,
314+
ngx_http_cache_purge_queue_size_variable, 0,
315+
NGX_HTTP_VAR_NOCACHEABLE, 0 },
316+
317+
{ ngx_string("cache_purge_queue_max_size"), NULL,
318+
ngx_http_cache_purge_queue_max_size_variable, 0,
319+
NGX_HTTP_VAR_NOCACHEABLE, 0 },
320+
321+
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
322+
};
323+
304324
/* -- module commands ---------------------------------------------------- */
305325

306326
static ngx_command_t ngx_http_cache_purge_module_commands[] = {
@@ -382,7 +402,7 @@ static ngx_command_t ngx_http_cache_purge_module_commands[] = {
382402

383403
static ngx_http_module_t ngx_http_cache_purge_module_ctx = {
384404
NULL, /* preconfiguration */
385-
NULL, /* postconfiguration */
405+
ngx_http_cache_purge_add_variables, /* postconfiguration */
386406
ngx_http_cache_purge_create_main_conf, /* create main conf */
387407
ngx_http_cache_purge_init_main_conf, /* init main conf */
388408
NULL, /* create srv conf */
@@ -411,6 +431,97 @@ static ngx_event_t ngx_cache_purge_event;
411431
static ngx_http_cache_purge_main_conf_t *ngx_cache_purge_main_conf;
412432

413433

434+
/* -- variables ---------------------------------------------------------- */
435+
436+
static ngx_int_t
437+
ngx_http_cache_purge_add_variables(ngx_conf_t *cf)
438+
{
439+
ngx_http_variable_t *var, *v;
440+
441+
for (v = ngx_http_cache_purge_vars; v->name.len; v++) {
442+
var = ngx_http_add_variable(cf, &v->name, v->flags);
443+
if (var == NULL) {
444+
return NGX_ERROR;
445+
}
446+
var->get_handler = v->get_handler;
447+
var->data = v->data;
448+
}
449+
450+
return NGX_OK;
451+
}
452+
453+
454+
static ngx_int_t
455+
ngx_http_cache_purge_queue_size_variable(ngx_http_request_t *r,
456+
ngx_http_variable_value_t *v, uintptr_t data)
457+
{
458+
ngx_http_cache_purge_main_conf_t *cmcf;
459+
ngx_uint_t size;
460+
u_char *p;
461+
u_char buf[NGX_ATOMIC_T_LEN];
462+
463+
cmcf = ngx_http_get_module_main_conf(r, ngx_http_cache_purge_module);
464+
465+
if (!cmcf->background_purge || cmcf->queue == NULL) {
466+
v->data = (u_char *) "-";
467+
v->len = 1;
468+
v->valid = 1;
469+
v->not_found = 0;
470+
return NGX_OK;
471+
}
472+
473+
size = (ngx_uint_t) ngx_atomic_fetch_add(&cmcf->queue->size, 0);
474+
475+
p = ngx_sprintf(buf, "%ui", size);
476+
477+
v->data = ngx_pnalloc(r->pool, p - buf);
478+
if (v->data == NULL) {
479+
return NGX_ERROR;
480+
}
481+
482+
ngx_memcpy(v->data, buf, p - buf);
483+
v->len = p - buf;
484+
v->valid = 1;
485+
v->not_found = 0;
486+
487+
return NGX_OK;
488+
}
489+
490+
491+
static ngx_int_t
492+
ngx_http_cache_purge_queue_max_size_variable(ngx_http_request_t *r,
493+
ngx_http_variable_value_t *v, uintptr_t data)
494+
{
495+
ngx_http_cache_purge_main_conf_t *cmcf;
496+
u_char *p;
497+
u_char buf[NGX_INT_T_LEN];
498+
499+
cmcf = ngx_http_get_module_main_conf(r, ngx_http_cache_purge_module);
500+
501+
if (!cmcf->background_purge || cmcf->queue == NULL) {
502+
v->data = (u_char *) "-";
503+
v->len = 1;
504+
v->valid = 1;
505+
v->not_found = 0;
506+
return NGX_OK;
507+
}
508+
509+
p = ngx_sprintf(buf, "%ui", cmcf->queue->max_size);
510+
511+
v->data = ngx_pnalloc(r->pool, p - buf);
512+
if (v->data == NULL) {
513+
return NGX_ERROR;
514+
}
515+
516+
ngx_memcpy(v->data, buf, p - buf);
517+
v->len = p - buf;
518+
v->valid = 1;
519+
v->not_found = 0;
520+
521+
return NGX_OK;
522+
}
523+
524+
414525
/* -- main configuration ------------------------------------------------- */
415526

416527
static void *

t/background_queue.t

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,38 @@ PURGE /cache/test
128128
[202, 202]
129129
--- response_body eval
130130
[qr{Key: /cache/same}, qr{Key: /cache/same}]
131+
132+
=== TEST 7: $cache_purge_queue_size and $cache_purge_queue_max_size return integers when queue is enabled
133+
--- http_config
134+
proxy_cache_path $TEST_NGINX_SERVROOT/cache levels=1:2
135+
keys_zone=qsv_zone:1m;
136+
cache_purge_background_queue on;
137+
cache_purge_queue_size 10;
138+
--- config
139+
location /health {
140+
add_header X-Queue-Size $cache_purge_queue_size;
141+
add_header X-Queue-MaxSize $cache_purge_queue_max_size;
142+
return 200 "ok";
143+
}
144+
--- request
145+
GET /health
146+
--- error_code: 200
147+
--- response_headers_like
148+
X-Queue-Size: \d+
149+
X-Queue-MaxSize: 10
150+
151+
=== TEST 8: $cache_purge_queue_size and $cache_purge_queue_max_size return "-" when queue is disabled
152+
--- http_config
153+
cache_purge_background_queue off;
154+
--- config
155+
location /health {
156+
add_header X-Queue-Size $cache_purge_queue_size;
157+
add_header X-Queue-MaxSize $cache_purge_queue_max_size;
158+
return 200 "ok";
159+
}
160+
--- request
161+
GET /health
162+
--- error_code: 200
163+
--- response_headers
164+
X-Queue-Size: -
165+
X-Queue-MaxSize: -

0 commit comments

Comments
 (0)