Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement vhost_traffic_status_upstream_no_cache filter #194

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
- name: 'prepare cpanm'
run: |
sudo apt install -y cpanminus
sudo cpanm install Test::Nginx::Socket
sudo cpanm --notest Test::Nginx::Socket > build.log 2>&1 || (cat build.log && exit 1)
- name: 'prepare promtool'
run: |
sudo apt-get update && sudo apt-get install -y curl
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Table of Contents
* [vhost_traffic_status_filter_by_set_key](#vhost_traffic_status_filter_by_set_key)
* [vhost_traffic_status_filter_check_duplicate](#vhost_traffic_status_filter_check_duplicate)
* [vhost_traffic_status_filter_max_node](#vhost_traffic_status_filter_max_node)
* [vhost_traffic_status_filter_upstream_no_cache](#vhost_traffic_status_filter_upstream_no_cache)
* [vhost_traffic_status_limit](#vhost_traffic_status_limit)
* [vhost_traffic_status_limit_traffic](#vhost_traffic_status_limit_traffic)
* [vhost_traffic_status_limit_traffic_by_set_key](#vhost_traffic_status_limit_traffic_by_set_key)
Expand Down Expand Up @@ -1479,6 +1480,32 @@ http {
In the above example, the `/^uris.*/` and `/^client::ports.*/` group string patterns are limited to a total of 16 nodes.
The other filters like `country::.*` are not limited.

### vhost_traffic_status_filter_upstream_no_cache

| - | - |
| --- | --- |
| **Syntax** | **vhost_traffic_status_filter_upstream_no_cache** *name* |
| **Default** | - |
| **Context** | http, server, location |

`Description:` Create a group name that counts requests that have an upstream
but should not be cached. The group is visible under serverZones.
Note: This is event is not "cache miss" or "bypass cache". Example:
`$ edit nginx.conf`

```Nginx
http {
...
vhost_traffic_status_zone;
...
server {
server_name example.org;
...
vhost_traffic_status_filter_upstream_no_cache NO_CACHE;
}
}
```

### vhost_traffic_status_limit

| - | - |
Expand Down
10 changes: 10 additions & 0 deletions src/ngx_http_vhost_traffic_status_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,15 +524,25 @@ ngx_http_vhost_traffic_status_display_get_size(ngx_http_request_t *r,
ngx_int_t format)
{
ngx_uint_t size, un;
ngx_slab_pool_t *shpool;
ngx_http_vhost_traffic_status_loc_conf_t *vtscf;
ngx_http_vhost_traffic_status_shm_info_t *shm_info;

vtscf = ngx_http_get_module_loc_conf(r, ngx_http_vhost_traffic_status_module);
shpool = (ngx_slab_pool_t *) vtscf->shm_zone->shm.addr;

shm_info = ngx_pcalloc(r->pool, sizeof(ngx_http_vhost_traffic_status_shm_info_t));
if (shm_info == NULL) {
return NGX_ERROR;
}

/* Caveat: Do not use duplicate ngx_shmtx_lock() before this function. */
ngx_shmtx_lock(&shpool->mutex);

ngx_http_vhost_traffic_status_shm_info(r, shm_info);

ngx_shmtx_unlock(&shpool->mutex);

/* allocate memory for the upstream groups even if upstream node not exists */
un = shm_info->used_node
+ (ngx_uint_t) ngx_http_vhost_traffic_status_display_get_upstream_nelts(r);
Expand Down
23 changes: 22 additions & 1 deletion src/ngx_http_vhost_traffic_status_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ static ngx_command_t ngx_http_vhost_traffic_status_commands[] = {
offsetof(ngx_http_vhost_traffic_status_loc_conf_t, bypass_stats),
NULL },

{ ngx_string("vhost_traffic_status_filter_upstream_no_cache"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_vhost_traffic_status_loc_conf_t, no_cache_server_zone),
NULL },

ngx_null_command
};

Expand Down Expand Up @@ -265,12 +272,21 @@ ngx_http_vhost_traffic_status_handler(ngx_http_request_t *r)
return NGX_DECLINED;
}

rc = ngx_http_vhost_traffic_status_shm_add_server(r);
rc = ngx_http_vhost_traffic_status_shm_add_server(r, NULL);
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"handler::shm_add_server() failed");
}

if (ngx_strlen(vtscf->no_cache_server_zone.data) > 0) {
rc = ngx_http_vhost_traffic_status_shm_add_server(
r, &vtscf->no_cache_server_zone);
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"handler::shm_add_server(NO_CACHE) failed");
}
}

rc = ngx_http_vhost_traffic_status_shm_add_upstream(r);
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
Expand Down Expand Up @@ -889,6 +905,8 @@ ngx_http_vhost_traffic_status_create_loc_conf(ngx_conf_t *cf)
* conf->histogram_buckets = { NULL, ... };
* conf->bypass_limit = 0;
* conf->bypass_stats = 0;
*
* conf->no_cache_server_zone = { 0, NULL };
*/

conf->shm_zone = NGX_CONF_UNSET_PTR;
Expand Down Expand Up @@ -1019,6 +1037,9 @@ ngx_http_vhost_traffic_status_merge_loc_conf(ngx_conf_t *cf, void *parent, void
ngx_conf_merge_value(conf->bypass_limit, prev->bypass_limit, 0);
ngx_conf_merge_value(conf->bypass_stats, prev->bypass_stats, 0);

ngx_conf_merge_str_value(conf->no_cache_server_zone,
prev->no_cache_server_zone, "");

name = ctx->shm_name;

shm_zone = ngx_shared_memory_add(cf, &name, 0,
Expand Down
3 changes: 3 additions & 0 deletions src/ngx_http_vhost_traffic_status_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ typedef struct {
ngx_flag_t bypass_limit;
ngx_flag_t bypass_stats;

/* Controls name of "no cache" server zone */
ngx_str_t no_cache_server_zone;

ngx_rbtree_node_t **node_caches;
} ngx_http_vhost_traffic_status_loc_conf_t;

Expand Down
31 changes: 20 additions & 11 deletions src/ngx_http_vhost_traffic_status_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ ngx_http_vhost_traffic_status_shm_add_filter_node(ngx_http_request_t *r,


ngx_int_t
ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r)
ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r,
const ngx_str_t *no_cache_server_zone)
{
unsigned type;
ngx_int_t rc;
Expand All @@ -366,17 +367,25 @@ ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r)

cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

if (vtscf->filter && vtscf->filter_host && r->headers_in.server.len) {
/* set the key by host header */
dst = r->headers_in.server;

if (no_cache_server_zone != NULL &&
ngx_strlen(no_cache_server_zone->data) > 0) {
if (r->upstream == NULL || r->upstream->cache_status != 0) {
return NGX_OK;
}
dst.len = no_cache_server_zone->len;
dst.data = no_cache_server_zone->data;
} else {
/* set the key by server_name variable */
dst = cscf->server_name;
if (dst.len == 0) {
dst.len = 1;
dst.data = (u_char *) "_";
}
if (vtscf->filter && vtscf->filter_host && r->headers_in.server.len) {
/* set the key by host header */
dst = r->headers_in.server;
} else {
/* set the key by server_name variable */
dst = cscf->server_name;
if (dst.len == 0) {
dst.len = 1;
dst.data = (u_char *) "_";
}
}
}

type = NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_NO;
Expand Down
3 changes: 2 additions & 1 deletion src/ngx_http_vhost_traffic_status_shm.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ typedef struct {
} ngx_http_vhost_traffic_status_shm_info_t;


ngx_int_t ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r);
ngx_int_t ngx_http_vhost_traffic_status_shm_add_server(
ngx_http_request_t *r, const ngx_str_t *no_cache_server_zone);
ngx_int_t ngx_http_vhost_traffic_status_shm_add_filter(ngx_http_request_t *r);
ngx_int_t ngx_http_vhost_traffic_status_shm_add_upstream(ngx_http_request_t *r);

Expand Down