Compare commits

...
3 Commits
Author SHA1 Message Date
Hakase f898acf52b Version bump - v1.17.7 2019-11-23 11:46:15 +09:00
Hakase 2fc052617a Release - v1.17.6 2019-11-23 11:45:56 +09:00
Hakase d9ff2e8286 Latest update - 7596 2019-11-23 11:45:34 +09:00
7 changed files with 315 additions and 101 deletions
+1
View File
@@ -444,3 +444,4 @@ d2fd76709909767fc727a5b4affcf1dc9ca488a7 release-1.15.9
ed4303aa1b31a9aad5440640c0840d9d0af45fed release-1.17.3 ed4303aa1b31a9aad5440640c0840d9d0af45fed release-1.17.3
ce2ced3856909f36f8130c99eaa4dbdbae636ddc release-1.17.4 ce2ced3856909f36f8130c99eaa4dbdbae636ddc release-1.17.4
9af0dddbddb2c368bfedd2801bc100ffad01e19b release-1.17.5 9af0dddbddb2c368bfedd2801bc100ffad01e19b release-1.17.5
de68d0d94320cbf033599c6f3ca37e5335c67fd7 release-1.17.6
+32
View File
@@ -5,6 +5,38 @@
<change_log title="nginx"> <change_log title="nginx">
<changes ver="1.17.6" date="2019-11-19">
<change type="feature">
<para lang="ru">
переменные $proxy_protocol_server_addr и $proxy_protocol_server_port.
</para>
<para lang="en">
the $proxy_protocol_server_addr and $proxy_protocol_server_port variables.
</para>
</change>
<change type="feature">
<para lang="ru">
директива limit_conn_dry_run.
</para>
<para lang="en">
the "limit_conn_dry_run" directive.
</para>
</change>
<change type="feature">
<para lang="ru">
переменные $limit_req_status и $limit_conn_status.
</para>
<para lang="en">
the $limit_req_status and $limit_conn_status variables.
</para>
</change>
</changes>
<changes ver="1.17.5" date="2019-10-22"> <changes ver="1.17.5" date="2019-10-22">
<change type="feature"> <change type="feature">
+2 -2
View File
@@ -9,8 +9,8 @@
#define _NGINX_H_INCLUDED_ #define _NGINX_H_INCLUDED_
#define nginx_version 1017006 #define nginx_version 1017007
#define NGINX_VERSION "1.17.6" #define NGINX_VERSION "1.17.7"
#define NGINX_VER "nginx/" NGINX_VERSION " by Hakase" #define NGINX_VER "nginx/" NGINX_VERSION " by Hakase"
#ifndef NGINX_SERVER #ifndef NGINX_SERVER
+138 -50
View File
@@ -10,36 +10,49 @@
#include <ngx_http.h> #include <ngx_http.h>
#define NGX_HTTP_LIMIT_CONN_PASSED 1
#define NGX_HTTP_LIMIT_CONN_REJECTED 2
#define NGX_HTTP_LIMIT_CONN_REJECTED_DRY_RUN 3
typedef struct { typedef struct {
u_char color; u_char color;
u_char len; u_char len;
u_short conn; u_short conn;
u_char data[1]; u_char data[1];
} ngx_http_limit_conn_node_t; } ngx_http_limit_conn_node_t;
typedef struct { typedef struct {
ngx_shm_zone_t *shm_zone; ngx_shm_zone_t *shm_zone;
ngx_rbtree_node_t *node; ngx_rbtree_node_t *node;
} ngx_http_limit_conn_cleanup_t; } ngx_http_limit_conn_cleanup_t;
typedef struct { typedef struct {
ngx_rbtree_t *rbtree; ngx_rbtree_t rbtree;
ngx_http_complex_value_t key; ngx_rbtree_node_t sentinel;
} ngx_http_limit_conn_shctx_t;
typedef struct {
ngx_http_limit_conn_shctx_t *sh;
ngx_slab_pool_t *shpool;
ngx_http_complex_value_t key;
} ngx_http_limit_conn_ctx_t; } ngx_http_limit_conn_ctx_t;
typedef struct { typedef struct {
ngx_shm_zone_t *shm_zone; ngx_shm_zone_t *shm_zone;
ngx_uint_t conn; ngx_uint_t conn;
} ngx_http_limit_conn_limit_t; } ngx_http_limit_conn_limit_t;
typedef struct { typedef struct {
ngx_array_t limits; ngx_array_t limits;
ngx_uint_t log_level; ngx_uint_t log_level;
ngx_uint_t status_code; ngx_uint_t status_code;
ngx_flag_t dry_run;
} ngx_http_limit_conn_conf_t; } ngx_http_limit_conn_conf_t;
@@ -48,6 +61,8 @@ static ngx_rbtree_node_t *ngx_http_limit_conn_lookup(ngx_rbtree_t *rbtree,
static void ngx_http_limit_conn_cleanup(void *data); static void ngx_http_limit_conn_cleanup(void *data);
static ngx_inline void ngx_http_limit_conn_cleanup_all(ngx_pool_t *pool); static ngx_inline void ngx_http_limit_conn_cleanup_all(ngx_pool_t *pool);
static ngx_int_t ngx_http_limit_conn_status_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static void *ngx_http_limit_conn_create_conf(ngx_conf_t *cf); static void *ngx_http_limit_conn_create_conf(ngx_conf_t *cf);
static char *ngx_http_limit_conn_merge_conf(ngx_conf_t *cf, void *parent, static char *ngx_http_limit_conn_merge_conf(ngx_conf_t *cf, void *parent,
void *child); void *child);
@@ -55,6 +70,7 @@ static char *ngx_http_limit_conn_zone(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf); void *conf);
static char *ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, static char *ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf); void *conf);
static ngx_int_t ngx_http_limit_conn_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_http_limit_conn_init(ngx_conf_t *cf); static ngx_int_t ngx_http_limit_conn_init(ngx_conf_t *cf);
@@ -102,12 +118,19 @@ static ngx_command_t ngx_http_limit_conn_commands[] = {
offsetof(ngx_http_limit_conn_conf_t, status_code), offsetof(ngx_http_limit_conn_conf_t, status_code),
&ngx_http_limit_conn_status_bounds }, &ngx_http_limit_conn_status_bounds },
{ ngx_string("limit_conn_dry_run"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_limit_conn_conf_t, dry_run),
NULL },
ngx_null_command ngx_null_command
}; };
static ngx_http_module_t ngx_http_limit_conn_module_ctx = { static ngx_http_module_t ngx_http_limit_conn_module_ctx = {
NULL, /* preconfiguration */ ngx_http_limit_conn_add_variables, /* preconfiguration */
ngx_http_limit_conn_init, /* postconfiguration */ ngx_http_limit_conn_init, /* postconfiguration */
NULL, /* create main configuration */ NULL, /* create main configuration */
@@ -137,6 +160,22 @@ ngx_module_t ngx_http_limit_conn_module = {
}; };
static ngx_http_variable_t ngx_http_limit_conn_vars[] = {
{ ngx_string("limit_conn_status"), NULL,
ngx_http_limit_conn_status_variable, 0, NGX_HTTP_VAR_NOCACHEABLE, 0 },
ngx_http_null_variable
};
static ngx_str_t ngx_http_limit_conn_status[] = {
ngx_string("PASSED"),
ngx_string("REJECTED"),
ngx_string("REJECTED_DRY_RUN")
};
static ngx_int_t static ngx_int_t
ngx_http_limit_conn_handler(ngx_http_request_t *r) ngx_http_limit_conn_handler(ngx_http_request_t *r)
{ {
@@ -144,7 +183,6 @@ ngx_http_limit_conn_handler(ngx_http_request_t *r)
uint32_t hash; uint32_t hash;
ngx_str_t key; ngx_str_t key;
ngx_uint_t i; ngx_uint_t i;
ngx_slab_pool_t *shpool;
ngx_rbtree_node_t *node; ngx_rbtree_node_t *node;
ngx_pool_cleanup_t *cln; ngx_pool_cleanup_t *cln;
ngx_http_limit_conn_ctx_t *ctx; ngx_http_limit_conn_ctx_t *ctx;
@@ -153,7 +191,7 @@ ngx_http_limit_conn_handler(ngx_http_request_t *r)
ngx_http_limit_conn_limit_t *limits; ngx_http_limit_conn_limit_t *limits;
ngx_http_limit_conn_cleanup_t *lccln; ngx_http_limit_conn_cleanup_t *lccln;
if (r->main->limit_conn_set) { if (r->main->limit_conn_status) {
return NGX_DECLINED; return NGX_DECLINED;
} }
@@ -179,15 +217,13 @@ ngx_http_limit_conn_handler(ngx_http_request_t *r)
continue; continue;
} }
r->main->limit_conn_set = 1; r->main->limit_conn_status = NGX_HTTP_LIMIT_CONN_PASSED;
hash = ngx_crc32_short(key.data, key.len); hash = ngx_crc32_short(key.data, key.len);
shpool = (ngx_slab_pool_t *) limits[i].shm_zone->shm.addr; ngx_shmtx_lock(&ctx->shpool->mutex);
ngx_shmtx_lock(&shpool->mutex); node = ngx_http_limit_conn_lookup(&ctx->sh->rbtree, &key, hash);
node = ngx_http_limit_conn_lookup(ctx->rbtree, &key, hash);
if (node == NULL) { if (node == NULL) {
@@ -195,11 +231,20 @@ ngx_http_limit_conn_handler(ngx_http_request_t *r)
+ offsetof(ngx_http_limit_conn_node_t, data) + offsetof(ngx_http_limit_conn_node_t, data)
+ key.len; + key.len;
node = ngx_slab_alloc_locked(shpool, n); node = ngx_slab_alloc_locked(ctx->shpool, n);
if (node == NULL) { if (node == NULL) {
ngx_shmtx_unlock(&shpool->mutex); ngx_shmtx_unlock(&ctx->shpool->mutex);
ngx_http_limit_conn_cleanup_all(r->pool); ngx_http_limit_conn_cleanup_all(r->pool);
if (lccf->dry_run) {
r->main->limit_conn_status =
NGX_HTTP_LIMIT_CONN_REJECTED_DRY_RUN;
return NGX_DECLINED;
}
r->main->limit_conn_status = NGX_HTTP_LIMIT_CONN_REJECTED;
return lccf->status_code; return lccf->status_code;
} }
@@ -210,7 +255,7 @@ ngx_http_limit_conn_handler(ngx_http_request_t *r)
lc->conn = 1; lc->conn = 1;
ngx_memcpy(lc->data, key.data, key.len); ngx_memcpy(lc->data, key.data, key.len);
ngx_rbtree_insert(ctx->rbtree, node); ngx_rbtree_insert(&ctx->sh->rbtree, node);
} else { } else {
@@ -218,13 +263,23 @@ ngx_http_limit_conn_handler(ngx_http_request_t *r)
if ((ngx_uint_t) lc->conn >= limits[i].conn) { if ((ngx_uint_t) lc->conn >= limits[i].conn) {
ngx_shmtx_unlock(&shpool->mutex); ngx_shmtx_unlock(&ctx->shpool->mutex);
ngx_log_error(lccf->log_level, r->connection->log, 0, ngx_log_error(lccf->log_level, r->connection->log, 0,
"limiting connections by zone \"%V\"", "limiting connections%s by zone \"%V\"",
lccf->dry_run ? ", dry run," : "",
&limits[i].shm_zone->shm.name); &limits[i].shm_zone->shm.name);
ngx_http_limit_conn_cleanup_all(r->pool); ngx_http_limit_conn_cleanup_all(r->pool);
if (lccf->dry_run) {
r->main->limit_conn_status =
NGX_HTTP_LIMIT_CONN_REJECTED_DRY_RUN;
return NGX_DECLINED;
}
r->main->limit_conn_status = NGX_HTTP_LIMIT_CONN_REJECTED;
return lccf->status_code; return lccf->status_code;
} }
@@ -234,7 +289,7 @@ ngx_http_limit_conn_handler(ngx_http_request_t *r)
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"limit conn: %08Xi %d", node->key, lc->conn); "limit conn: %08Xi %d", node->key, lc->conn);
ngx_shmtx_unlock(&shpool->mutex); ngx_shmtx_unlock(&ctx->shpool->mutex);
cln = ngx_pool_cleanup_add(r->pool, cln = ngx_pool_cleanup_add(r->pool,
sizeof(ngx_http_limit_conn_cleanup_t)); sizeof(ngx_http_limit_conn_cleanup_t));
@@ -338,17 +393,15 @@ ngx_http_limit_conn_cleanup(void *data)
{ {
ngx_http_limit_conn_cleanup_t *lccln = data; ngx_http_limit_conn_cleanup_t *lccln = data;
ngx_slab_pool_t *shpool;
ngx_rbtree_node_t *node; ngx_rbtree_node_t *node;
ngx_http_limit_conn_ctx_t *ctx; ngx_http_limit_conn_ctx_t *ctx;
ngx_http_limit_conn_node_t *lc; ngx_http_limit_conn_node_t *lc;
ctx = lccln->shm_zone->data; ctx = lccln->shm_zone->data;
shpool = (ngx_slab_pool_t *) lccln->shm_zone->shm.addr;
node = lccln->node; node = lccln->node;
lc = (ngx_http_limit_conn_node_t *) &node->color; lc = (ngx_http_limit_conn_node_t *) &node->color;
ngx_shmtx_lock(&shpool->mutex); ngx_shmtx_lock(&ctx->shpool->mutex);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, lccln->shm_zone->shm.log, 0, ngx_log_debug2(NGX_LOG_DEBUG_HTTP, lccln->shm_zone->shm.log, 0,
"limit conn cleanup: %08Xi %d", node->key, lc->conn); "limit conn cleanup: %08Xi %d", node->key, lc->conn);
@@ -356,11 +409,11 @@ ngx_http_limit_conn_cleanup(void *data)
lc->conn--; lc->conn--;
if (lc->conn == 0) { if (lc->conn == 0) {
ngx_rbtree_delete(ctx->rbtree, node); ngx_rbtree_delete(&ctx->sh->rbtree, node);
ngx_slab_free_locked(shpool, node); ngx_slab_free_locked(ctx->shpool, node);
} }
ngx_shmtx_unlock(&shpool->mutex); ngx_shmtx_unlock(&ctx->shpool->mutex);
} }
@@ -386,8 +439,6 @@ ngx_http_limit_conn_init_zone(ngx_shm_zone_t *shm_zone, void *data)
ngx_http_limit_conn_ctx_t *octx = data; ngx_http_limit_conn_ctx_t *octx = data;
size_t len; size_t len;
ngx_slab_pool_t *shpool;
ngx_rbtree_node_t *sentinel;
ngx_http_limit_conn_ctx_t *ctx; ngx_http_limit_conn_ctx_t *ctx;
ctx = shm_zone->data; ctx = shm_zone->data;
@@ -406,48 +457,63 @@ ngx_http_limit_conn_init_zone(ngx_shm_zone_t *shm_zone, void *data)
return NGX_ERROR; return NGX_ERROR;
} }
ctx->rbtree = octx->rbtree; ctx->sh = octx->sh;
ctx->shpool = octx->shpool;
return NGX_OK; return NGX_OK;
} }
shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
if (shm_zone->shm.exists) { if (shm_zone->shm.exists) {
ctx->rbtree = shpool->data; ctx->sh = ctx->shpool->data;
return NGX_OK; return NGX_OK;
} }
ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t)); ctx->sh = ngx_slab_alloc(ctx->shpool, sizeof(ngx_http_limit_conn_shctx_t));
if (ctx->rbtree == NULL) { if (ctx->sh == NULL) {
return NGX_ERROR; return NGX_ERROR;
} }
shpool->data = ctx->rbtree; ctx->shpool->data = ctx->sh;
sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t)); ngx_rbtree_init(&ctx->sh->rbtree, &ctx->sh->sentinel,
if (sentinel == NULL) {
return NGX_ERROR;
}
ngx_rbtree_init(ctx->rbtree, sentinel,
ngx_http_limit_conn_rbtree_insert_value); ngx_http_limit_conn_rbtree_insert_value);
len = sizeof(" in limit_conn_zone \"\"") + shm_zone->shm.name.len; len = sizeof(" in limit_conn_zone \"\"") + shm_zone->shm.name.len;
shpool->log_ctx = ngx_slab_alloc(shpool, len); ctx->shpool->log_ctx = ngx_slab_alloc(ctx->shpool, len);
if (shpool->log_ctx == NULL) { if (ctx->shpool->log_ctx == NULL) {
return NGX_ERROR; return NGX_ERROR;
} }
ngx_sprintf(shpool->log_ctx, " in limit_conn_zone \"%V\"%Z", ngx_sprintf(ctx->shpool->log_ctx, " in limit_conn_zone \"%V\"%Z",
&shm_zone->shm.name); &shm_zone->shm.name);
return NGX_OK; return NGX_OK;
} }
static ngx_int_t
ngx_http_limit_conn_status_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
if (r->main->limit_conn_status == 0) {
v->not_found = 1;
return NGX_OK;
}
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->len = ngx_http_limit_conn_status[r->main->limit_conn_status - 1].len;
v->data = ngx_http_limit_conn_status[r->main->limit_conn_status - 1].data;
return NGX_OK;
}
static void * static void *
ngx_http_limit_conn_create_conf(ngx_conf_t *cf) ngx_http_limit_conn_create_conf(ngx_conf_t *cf)
{ {
@@ -466,6 +532,7 @@ ngx_http_limit_conn_create_conf(ngx_conf_t *cf)
conf->log_level = NGX_CONF_UNSET_UINT; conf->log_level = NGX_CONF_UNSET_UINT;
conf->status_code = NGX_CONF_UNSET_UINT; conf->status_code = NGX_CONF_UNSET_UINT;
conf->dry_run = NGX_CONF_UNSET;
return conf; return conf;
} }
@@ -485,6 +552,8 @@ ngx_http_limit_conn_merge_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_uint_value(conf->status_code, prev->status_code, ngx_conf_merge_uint_value(conf->status_code, prev->status_code,
NGX_HTTP_SERVICE_UNAVAILABLE); NGX_HTTP_SERVICE_UNAVAILABLE);
ngx_conf_merge_value(conf->dry_run, prev->dry_run, 0);
return NGX_CONF_OK; return NGX_CONF_OK;
} }
@@ -651,6 +720,25 @@ ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
} }
static ngx_int_t
ngx_http_limit_conn_add_variables(ngx_conf_t *cf)
{
ngx_http_variable_t *var, *v;
for (v = ngx_http_limit_conn_vars; v->name.len; v++) {
var = ngx_http_add_variable(cf, &v->name, v->flags);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = v->get_handler;
var->data = v->data;
}
return NGX_OK;
}
static ngx_int_t static ngx_int_t
ngx_http_limit_conn_init(ngx_conf_t *cf) ngx_http_limit_conn_init(ngx_conf_t *cf)
{ {
+2 -2
View File
@@ -510,9 +510,9 @@ struct ngx_http_request_s {
/* /*
* instead of using the request context data in * instead of using the request context data in
* ngx_http_limit_conn_module and ngx_http_limit_req_module * ngx_http_limit_conn_module and ngx_http_limit_req_module
* we use the single bits in the request structure * we use the bit fields in the request structure
*/ */
unsigned limit_conn_set:1; unsigned limit_conn_status:2;
unsigned limit_req_status:3; unsigned limit_req_status:3;
unsigned limit_rate_set:1; unsigned limit_rate_set:1;
+2
View File
@@ -226,6 +226,8 @@ struct ngx_stream_session_s {
unsigned stat_processing:1; unsigned stat_processing:1;
unsigned health_check:1; unsigned health_check:1;
unsigned limit_conn_status:2;
}; };
+138 -47
View File
@@ -10,35 +10,48 @@
#include <ngx_stream.h> #include <ngx_stream.h>
#define NGX_STREAM_LIMIT_CONN_PASSED 1
#define NGX_STREAM_LIMIT_CONN_REJECTED 2
#define NGX_STREAM_LIMIT_CONN_REJECTED_DRY_RUN 3
typedef struct { typedef struct {
u_char color; u_char color;
u_char len; u_char len;
u_short conn; u_short conn;
u_char data[1]; u_char data[1];
} ngx_stream_limit_conn_node_t; } ngx_stream_limit_conn_node_t;
typedef struct { typedef struct {
ngx_shm_zone_t *shm_zone; ngx_shm_zone_t *shm_zone;
ngx_rbtree_node_t *node; ngx_rbtree_node_t *node;
} ngx_stream_limit_conn_cleanup_t; } ngx_stream_limit_conn_cleanup_t;
typedef struct { typedef struct {
ngx_rbtree_t *rbtree; ngx_rbtree_t rbtree;
ngx_stream_complex_value_t key; ngx_rbtree_node_t sentinel;
} ngx_stream_limit_conn_shctx_t;
typedef struct {
ngx_stream_limit_conn_shctx_t *sh;
ngx_slab_pool_t *shpool;
ngx_stream_complex_value_t key;
} ngx_stream_limit_conn_ctx_t; } ngx_stream_limit_conn_ctx_t;
typedef struct { typedef struct {
ngx_shm_zone_t *shm_zone; ngx_shm_zone_t *shm_zone;
ngx_uint_t conn; ngx_uint_t conn;
} ngx_stream_limit_conn_limit_t; } ngx_stream_limit_conn_limit_t;
typedef struct { typedef struct {
ngx_array_t limits; ngx_array_t limits;
ngx_uint_t log_level; ngx_uint_t log_level;
ngx_flag_t dry_run;
} ngx_stream_limit_conn_conf_t; } ngx_stream_limit_conn_conf_t;
@@ -47,6 +60,8 @@ static ngx_rbtree_node_t *ngx_stream_limit_conn_lookup(ngx_rbtree_t *rbtree,
static void ngx_stream_limit_conn_cleanup(void *data); static void ngx_stream_limit_conn_cleanup(void *data);
static ngx_inline void ngx_stream_limit_conn_cleanup_all(ngx_pool_t *pool); static ngx_inline void ngx_stream_limit_conn_cleanup_all(ngx_pool_t *pool);
static ngx_int_t ngx_stream_limit_conn_status_variable(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data);
static void *ngx_stream_limit_conn_create_conf(ngx_conf_t *cf); static void *ngx_stream_limit_conn_create_conf(ngx_conf_t *cf);
static char *ngx_stream_limit_conn_merge_conf(ngx_conf_t *cf, void *parent, static char *ngx_stream_limit_conn_merge_conf(ngx_conf_t *cf, void *parent,
void *child); void *child);
@@ -54,6 +69,7 @@ static char *ngx_stream_limit_conn_zone(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf); void *conf);
static char *ngx_stream_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, static char *ngx_stream_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf); void *conf);
static ngx_int_t ngx_stream_limit_conn_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_stream_limit_conn_init(ngx_conf_t *cf); static ngx_int_t ngx_stream_limit_conn_init(ngx_conf_t *cf);
@@ -89,12 +105,19 @@ static ngx_command_t ngx_stream_limit_conn_commands[] = {
offsetof(ngx_stream_limit_conn_conf_t, log_level), offsetof(ngx_stream_limit_conn_conf_t, log_level),
&ngx_stream_limit_conn_log_levels }, &ngx_stream_limit_conn_log_levels },
{ ngx_string("limit_conn_dry_run"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_limit_conn_conf_t, dry_run),
NULL },
ngx_null_command ngx_null_command
}; };
static ngx_stream_module_t ngx_stream_limit_conn_module_ctx = { static ngx_stream_module_t ngx_stream_limit_conn_module_ctx = {
NULL, /* preconfiguration */ ngx_stream_limit_conn_add_variables, /* preconfiguration */
ngx_stream_limit_conn_init, /* postconfiguration */ ngx_stream_limit_conn_init, /* postconfiguration */
NULL, /* create main configuration */ NULL, /* create main configuration */
@@ -121,6 +144,22 @@ ngx_module_t ngx_stream_limit_conn_module = {
}; };
static ngx_stream_variable_t ngx_stream_limit_conn_vars[] = {
{ ngx_string("limit_conn_status"), NULL,
ngx_stream_limit_conn_status_variable, 0, NGX_STREAM_VAR_NOCACHEABLE, 0 },
ngx_stream_null_variable
};
static ngx_str_t ngx_stream_limit_conn_status[] = {
ngx_string("PASSED"),
ngx_string("REJECTED"),
ngx_string("REJECTED_DRY_RUN")
};
static ngx_int_t static ngx_int_t
ngx_stream_limit_conn_handler(ngx_stream_session_t *s) ngx_stream_limit_conn_handler(ngx_stream_session_t *s)
{ {
@@ -128,7 +167,6 @@ ngx_stream_limit_conn_handler(ngx_stream_session_t *s)
uint32_t hash; uint32_t hash;
ngx_str_t key; ngx_str_t key;
ngx_uint_t i; ngx_uint_t i;
ngx_slab_pool_t *shpool;
ngx_rbtree_node_t *node; ngx_rbtree_node_t *node;
ngx_pool_cleanup_t *cln; ngx_pool_cleanup_t *cln;
ngx_stream_limit_conn_ctx_t *ctx; ngx_stream_limit_conn_ctx_t *ctx;
@@ -159,13 +197,13 @@ ngx_stream_limit_conn_handler(ngx_stream_session_t *s)
continue; continue;
} }
s->limit_conn_status = NGX_STREAM_LIMIT_CONN_PASSED;
hash = ngx_crc32_short(key.data, key.len); hash = ngx_crc32_short(key.data, key.len);
shpool = (ngx_slab_pool_t *) limits[i].shm_zone->shm.addr; ngx_shmtx_lock(&ctx->shpool->mutex);
ngx_shmtx_lock(&shpool->mutex); node = ngx_stream_limit_conn_lookup(&ctx->sh->rbtree, &key, hash);
node = ngx_stream_limit_conn_lookup(ctx->rbtree, &key, hash);
if (node == NULL) { if (node == NULL) {
@@ -173,11 +211,20 @@ ngx_stream_limit_conn_handler(ngx_stream_session_t *s)
+ offsetof(ngx_stream_limit_conn_node_t, data) + offsetof(ngx_stream_limit_conn_node_t, data)
+ key.len; + key.len;
node = ngx_slab_alloc_locked(shpool, n); node = ngx_slab_alloc_locked(ctx->shpool, n);
if (node == NULL) { if (node == NULL) {
ngx_shmtx_unlock(&shpool->mutex); ngx_shmtx_unlock(&ctx->shpool->mutex);
ngx_stream_limit_conn_cleanup_all(s->connection->pool); ngx_stream_limit_conn_cleanup_all(s->connection->pool);
if (lccf->dry_run) {
s->limit_conn_status =
NGX_STREAM_LIMIT_CONN_REJECTED_DRY_RUN;
return NGX_DECLINED;
}
s->limit_conn_status = NGX_STREAM_LIMIT_CONN_REJECTED;
return NGX_STREAM_SERVICE_UNAVAILABLE; return NGX_STREAM_SERVICE_UNAVAILABLE;
} }
@@ -188,7 +235,7 @@ ngx_stream_limit_conn_handler(ngx_stream_session_t *s)
lc->conn = 1; lc->conn = 1;
ngx_memcpy(lc->data, key.data, key.len); ngx_memcpy(lc->data, key.data, key.len);
ngx_rbtree_insert(ctx->rbtree, node); ngx_rbtree_insert(&ctx->sh->rbtree, node);
} else { } else {
@@ -196,13 +243,23 @@ ngx_stream_limit_conn_handler(ngx_stream_session_t *s)
if ((ngx_uint_t) lc->conn >= limits[i].conn) { if ((ngx_uint_t) lc->conn >= limits[i].conn) {
ngx_shmtx_unlock(&shpool->mutex); ngx_shmtx_unlock(&ctx->shpool->mutex);
ngx_log_error(lccf->log_level, s->connection->log, 0, ngx_log_error(lccf->log_level, s->connection->log, 0,
"limiting connections by zone \"%V\"", "limiting connections%s by zone \"%V\"",
lccf->dry_run ? ", dry run," : "",
&limits[i].shm_zone->shm.name); &limits[i].shm_zone->shm.name);
ngx_stream_limit_conn_cleanup_all(s->connection->pool); ngx_stream_limit_conn_cleanup_all(s->connection->pool);
if (lccf->dry_run) {
s->limit_conn_status =
NGX_STREAM_LIMIT_CONN_REJECTED_DRY_RUN;
return NGX_DECLINED;
}
s->limit_conn_status = NGX_STREAM_LIMIT_CONN_REJECTED;
return NGX_STREAM_SERVICE_UNAVAILABLE; return NGX_STREAM_SERVICE_UNAVAILABLE;
} }
@@ -212,7 +269,7 @@ ngx_stream_limit_conn_handler(ngx_stream_session_t *s)
ngx_log_debug2(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, ngx_log_debug2(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"limit conn: %08Xi %d", node->key, lc->conn); "limit conn: %08Xi %d", node->key, lc->conn);
ngx_shmtx_unlock(&shpool->mutex); ngx_shmtx_unlock(&ctx->shpool->mutex);
cln = ngx_pool_cleanup_add(s->connection->pool, cln = ngx_pool_cleanup_add(s->connection->pool,
sizeof(ngx_stream_limit_conn_cleanup_t)); sizeof(ngx_stream_limit_conn_cleanup_t));
@@ -317,17 +374,15 @@ ngx_stream_limit_conn_cleanup(void *data)
{ {
ngx_stream_limit_conn_cleanup_t *lccln = data; ngx_stream_limit_conn_cleanup_t *lccln = data;
ngx_slab_pool_t *shpool;
ngx_rbtree_node_t *node; ngx_rbtree_node_t *node;
ngx_stream_limit_conn_ctx_t *ctx; ngx_stream_limit_conn_ctx_t *ctx;
ngx_stream_limit_conn_node_t *lc; ngx_stream_limit_conn_node_t *lc;
ctx = lccln->shm_zone->data; ctx = lccln->shm_zone->data;
shpool = (ngx_slab_pool_t *) lccln->shm_zone->shm.addr;
node = lccln->node; node = lccln->node;
lc = (ngx_stream_limit_conn_node_t *) &node->color; lc = (ngx_stream_limit_conn_node_t *) &node->color;
ngx_shmtx_lock(&shpool->mutex); ngx_shmtx_lock(&ctx->shpool->mutex);
ngx_log_debug2(NGX_LOG_DEBUG_STREAM, lccln->shm_zone->shm.log, 0, ngx_log_debug2(NGX_LOG_DEBUG_STREAM, lccln->shm_zone->shm.log, 0,
"limit conn cleanup: %08Xi %d", node->key, lc->conn); "limit conn cleanup: %08Xi %d", node->key, lc->conn);
@@ -335,11 +390,11 @@ ngx_stream_limit_conn_cleanup(void *data)
lc->conn--; lc->conn--;
if (lc->conn == 0) { if (lc->conn == 0) {
ngx_rbtree_delete(ctx->rbtree, node); ngx_rbtree_delete(&ctx->sh->rbtree, node);
ngx_slab_free_locked(shpool, node); ngx_slab_free_locked(ctx->shpool, node);
} }
ngx_shmtx_unlock(&shpool->mutex); ngx_shmtx_unlock(&ctx->shpool->mutex);
} }
@@ -365,8 +420,6 @@ ngx_stream_limit_conn_init_zone(ngx_shm_zone_t *shm_zone, void *data)
ngx_stream_limit_conn_ctx_t *octx = data; ngx_stream_limit_conn_ctx_t *octx = data;
size_t len; size_t len;
ngx_slab_pool_t *shpool;
ngx_rbtree_node_t *sentinel;
ngx_stream_limit_conn_ctx_t *ctx; ngx_stream_limit_conn_ctx_t *ctx;
ctx = shm_zone->data; ctx = shm_zone->data;
@@ -385,48 +438,64 @@ ngx_stream_limit_conn_init_zone(ngx_shm_zone_t *shm_zone, void *data)
return NGX_ERROR; return NGX_ERROR;
} }
ctx->rbtree = octx->rbtree; ctx->sh = octx->sh;
ctx->shpool = octx->shpool;
return NGX_OK; return NGX_OK;
} }
shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
if (shm_zone->shm.exists) { if (shm_zone->shm.exists) {
ctx->rbtree = shpool->data; ctx->sh = ctx->shpool->data;
return NGX_OK; return NGX_OK;
} }
ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t)); ctx->sh = ngx_slab_alloc(ctx->shpool,
if (ctx->rbtree == NULL) { sizeof(ngx_stream_limit_conn_shctx_t));
if (ctx->sh == NULL) {
return NGX_ERROR; return NGX_ERROR;
} }
shpool->data = ctx->rbtree; ctx->shpool->data = ctx->sh;
sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t)); ngx_rbtree_init(&ctx->sh->rbtree, &ctx->sh->sentinel,
if (sentinel == NULL) {
return NGX_ERROR;
}
ngx_rbtree_init(ctx->rbtree, sentinel,
ngx_stream_limit_conn_rbtree_insert_value); ngx_stream_limit_conn_rbtree_insert_value);
len = sizeof(" in limit_conn_zone \"\"") + shm_zone->shm.name.len; len = sizeof(" in limit_conn_zone \"\"") + shm_zone->shm.name.len;
shpool->log_ctx = ngx_slab_alloc(shpool, len); ctx->shpool->log_ctx = ngx_slab_alloc(ctx->shpool, len);
if (shpool->log_ctx == NULL) { if (ctx->shpool->log_ctx == NULL) {
return NGX_ERROR; return NGX_ERROR;
} }
ngx_sprintf(shpool->log_ctx, " in limit_conn_zone \"%V\"%Z", ngx_sprintf(ctx->shpool->log_ctx, " in limit_conn_zone \"%V\"%Z",
&shm_zone->shm.name); &shm_zone->shm.name);
return NGX_OK; return NGX_OK;
} }
static ngx_int_t
ngx_stream_limit_conn_status_variable(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data)
{
if (s->limit_conn_status == 0) {
v->not_found = 1;
return NGX_OK;
}
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->len = ngx_stream_limit_conn_status[s->limit_conn_status - 1].len;
v->data = ngx_stream_limit_conn_status[s->limit_conn_status - 1].data;
return NGX_OK;
}
static void * static void *
ngx_stream_limit_conn_create_conf(ngx_conf_t *cf) ngx_stream_limit_conn_create_conf(ngx_conf_t *cf)
{ {
@@ -444,6 +513,7 @@ ngx_stream_limit_conn_create_conf(ngx_conf_t *cf)
*/ */
conf->log_level = NGX_CONF_UNSET_UINT; conf->log_level = NGX_CONF_UNSET_UINT;
conf->dry_run = NGX_CONF_UNSET;
return conf; return conf;
} }
@@ -461,6 +531,8 @@ ngx_stream_limit_conn_merge_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_uint_value(conf->log_level, prev->log_level, NGX_LOG_ERR); ngx_conf_merge_uint_value(conf->log_level, prev->log_level, NGX_LOG_ERR);
ngx_conf_merge_value(conf->dry_run, prev->dry_run, 0);
return NGX_CONF_OK; return NGX_CONF_OK;
} }
@@ -627,6 +699,25 @@ ngx_stream_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
} }
static ngx_int_t
ngx_stream_limit_conn_add_variables(ngx_conf_t *cf)
{
ngx_stream_variable_t *var, *v;
for (v = ngx_stream_limit_conn_vars; v->name.len; v++) {
var = ngx_stream_add_variable(cf, &v->name, v->flags);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = v->get_handler;
var->data = v->data;
}
return NGX_OK;
}
static ngx_int_t static ngx_int_t
ngx_stream_limit_conn_init(ngx_conf_t *cf) ngx_stream_limit_conn_init(ngx_conf_t *cf)
{ {