mirror of
https://github.com/nginx/nginx.git
synced 2025-02-25 18:55:26 -06:00
ngx_http_degradation_module
This commit is contained in:
parent
c05f20ec2e
commit
4985109dba
@ -316,6 +316,11 @@ if [ $HTTP_SECURE_LINK = YES ]; then
|
||||
HTTP_SRCS="$HTTP_SRCS $HTTP_SECURE_LINK_SRCS"
|
||||
fi
|
||||
|
||||
if [ $HTTP_DEGRADATION = YES ]; then
|
||||
HTTP_MODULES="$HTTP_MODULES $HTTP_DEGRADATION_MODULE"
|
||||
HTTP_SRCS="$HTTP_SRCS $HTTP_DEGRADATION_SRCS"
|
||||
fi
|
||||
|
||||
if [ $HTTP_FLV = YES ]; then
|
||||
HTTP_MODULES="$HTTP_MODULES $HTTP_FLV_MODULE"
|
||||
HTTP_SRCS="$HTTP_SRCS $HTTP_FLV_SRCS"
|
||||
|
@ -85,6 +85,7 @@ HTTP_LIMIT_REQ=YES
|
||||
HTTP_EMPTY_GIF=YES
|
||||
HTTP_BROWSER=YES
|
||||
HTTP_SECURE_LINK=NO
|
||||
HTTP_DEGRADATION=NO
|
||||
HTTP_FLV=NO
|
||||
HTTP_GZIP_STATIC=NO
|
||||
HTTP_UPSTREAM_IP_HASH=YES
|
||||
@ -194,6 +195,7 @@ do
|
||||
--with-http_gzip_static_module) HTTP_GZIP_STATIC=YES ;;
|
||||
--with-http_random_index_module) HTTP_RANDOM_INDEX=YES ;;
|
||||
--with-http_secure_link_module) HTTP_SECURE_LINK=YES ;;
|
||||
--with-http_degradation_module) HTTP_DEGRADATION=YES ;;
|
||||
|
||||
--without-http_charset_module) HTTP_CHARSET=NO ;;
|
||||
--without-http_gzip_module) HTTP_GZIP=NO ;;
|
||||
@ -322,6 +324,7 @@ cat << END
|
||||
--with-http_gzip_static_module enable ngx_http_gzip_static_module
|
||||
--with-http_random_index_module enable ngx_http_random_index_module
|
||||
--with-http_secure_link_module enable ngx_http_secure_link_module
|
||||
--with-http_degradation_module enable ngx_http_degradation_module
|
||||
--with-http_stub_status_module enable ngx_http_stub_status_module
|
||||
|
||||
--without-http_charset_module disable ngx_http_charset_module
|
||||
|
@ -438,6 +438,10 @@ HTTP_SECURE_LINK_MODULE=ngx_http_secure_link_module
|
||||
HTTP_SECURE_LINK_SRCS=src/http/modules/ngx_http_secure_link_module.c
|
||||
|
||||
|
||||
HTTP_DEGRADATION_MODULE=ngx_http_degradation_module
|
||||
HTTP_DEGRADATION_SRCS=src/http/modules/ngx_http_degradation_module.c
|
||||
|
||||
|
||||
HTTP_FLV_MODULE=ngx_http_flv_module
|
||||
HTTP_FLV_SRCS=src/http/modules/ngx_http_flv_module.c
|
||||
|
||||
|
223
src/http/modules/ngx_http_degradation_module.c
Normal file
223
src/http/modules/ngx_http_degradation_module.c
Normal file
@ -0,0 +1,223 @@
|
||||
|
||||
/*
|
||||
* Copyright (C) Igor Sysoev
|
||||
*/
|
||||
|
||||
|
||||
#include <ngx_config.h>
|
||||
#include <ngx_core.h>
|
||||
#include <ngx_http.h>
|
||||
|
||||
|
||||
typedef struct {
|
||||
size_t sbrk_size;
|
||||
} ngx_http_degradation_main_conf_t;
|
||||
|
||||
|
||||
typedef struct {
|
||||
ngx_uint_t degrade;
|
||||
} ngx_http_degradation_loc_conf_t;
|
||||
|
||||
|
||||
static ngx_conf_enum_t ngx_http_degrade[] = {
|
||||
{ ngx_string("204"), 204 },
|
||||
{ ngx_string("444"), 444 },
|
||||
{ ngx_null_string, 0 }
|
||||
};
|
||||
|
||||
|
||||
static void *ngx_http_degradation_create_main_conf(ngx_conf_t *cf);
|
||||
static void *ngx_http_degradation_create_loc_conf(ngx_conf_t *cf);
|
||||
static char *ngx_http_degradation_merge_loc_conf(ngx_conf_t *cf, void *parent,
|
||||
void *child);
|
||||
static char *ngx_http_degradation(ngx_conf_t *cf, ngx_command_t *cmd,
|
||||
void *conf);
|
||||
static ngx_int_t ngx_http_degradation_init(ngx_conf_t *cf);
|
||||
|
||||
|
||||
static ngx_command_t ngx_http_degradation_commands[] = {
|
||||
|
||||
{ ngx_string("degradation"),
|
||||
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
|
||||
ngx_http_degradation,
|
||||
NGX_HTTP_MAIN_CONF_OFFSET,
|
||||
0,
|
||||
NULL },
|
||||
|
||||
{ ngx_string("degrade"),
|
||||
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
||||
ngx_conf_set_enum_slot,
|
||||
NGX_HTTP_LOC_CONF_OFFSET,
|
||||
offsetof(ngx_http_degradation_loc_conf_t, degrade),
|
||||
&ngx_http_degrade },
|
||||
|
||||
ngx_null_command
|
||||
};
|
||||
|
||||
|
||||
static ngx_http_module_t ngx_http_degradation_module_ctx = {
|
||||
NULL, /* preconfiguration */
|
||||
ngx_http_degradation_init, /* postconfiguration */
|
||||
|
||||
ngx_http_degradation_create_main_conf, /* create main configuration */
|
||||
NULL, /* init main configuration */
|
||||
|
||||
NULL, /* create server configuration */
|
||||
NULL, /* merge server configuration */
|
||||
|
||||
ngx_http_degradation_create_loc_conf, /* create location configuration */
|
||||
ngx_http_degradation_merge_loc_conf /* merge location configuration */
|
||||
};
|
||||
|
||||
|
||||
ngx_module_t ngx_http_degradation_module = {
|
||||
NGX_MODULE_V1,
|
||||
&ngx_http_degradation_module_ctx, /* module context */
|
||||
ngx_http_degradation_commands, /* module directives */
|
||||
NGX_HTTP_MODULE, /* module type */
|
||||
NULL, /* init master */
|
||||
NULL, /* init module */
|
||||
NULL, /* init process */
|
||||
NULL, /* init thread */
|
||||
NULL, /* exit thread */
|
||||
NULL, /* exit process */
|
||||
NULL, /* exit master */
|
||||
NGX_MODULE_V1_PADDING
|
||||
};
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_http_degradation_handler(ngx_http_request_t *r)
|
||||
{
|
||||
time_t now;
|
||||
static size_t sbrk_size;
|
||||
static time_t sbrk_time;
|
||||
ngx_http_degradation_loc_conf_t *dlcf;
|
||||
ngx_http_degradation_main_conf_t *dmcf;
|
||||
|
||||
dlcf = ngx_http_get_module_loc_conf(r, ngx_http_degradation_module);
|
||||
|
||||
if (dlcf->degrade == 0) {
|
||||
return NGX_DECLINED;
|
||||
}
|
||||
|
||||
dmcf = ngx_http_get_module_main_conf(r, ngx_http_degradation_module);
|
||||
|
||||
if (dmcf->sbrk_size) {
|
||||
|
||||
now = ngx_time();
|
||||
|
||||
/* lock mutex */
|
||||
|
||||
if (now != sbrk_time) {
|
||||
|
||||
/*
|
||||
* ELF/i386 is loaded at 0x08000000, 128M
|
||||
* ELF/amd64 is loaded at 0x00400000, 4M
|
||||
*
|
||||
* use a function address to substract the loading address
|
||||
*/
|
||||
|
||||
sbrk_size = (size_t) sbrk(0) - ((uintptr_t) ngx_palloc & ~0x3FFFFF);
|
||||
sbrk_time = now;
|
||||
}
|
||||
|
||||
/* unlock mutex */
|
||||
|
||||
if (sbrk_size >= dmcf->sbrk_size) {
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
||||
"degradation sbrk: %uz", sbrk_size);
|
||||
|
||||
return dlcf->degrade;
|
||||
}
|
||||
}
|
||||
|
||||
return NGX_DECLINED;
|
||||
}
|
||||
|
||||
|
||||
static void *
|
||||
ngx_http_degradation_create_main_conf(ngx_conf_t *cf)
|
||||
{
|
||||
ngx_http_degradation_main_conf_t *dmcf;
|
||||
|
||||
dmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_degradation_main_conf_t));
|
||||
if (dmcf == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return dmcf;
|
||||
}
|
||||
|
||||
|
||||
static void *
|
||||
ngx_http_degradation_create_loc_conf(ngx_conf_t *cf)
|
||||
{
|
||||
ngx_http_degradation_loc_conf_t *conf;
|
||||
|
||||
conf = ngx_palloc(cf->pool, sizeof(ngx_http_degradation_loc_conf_t));
|
||||
if (conf == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
conf->degrade = NGX_CONF_UNSET_UINT;
|
||||
|
||||
return conf;
|
||||
}
|
||||
|
||||
|
||||
static char *
|
||||
ngx_http_degradation_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
||||
{
|
||||
ngx_http_degradation_loc_conf_t *prev = parent;
|
||||
ngx_http_degradation_loc_conf_t *conf = child;
|
||||
|
||||
ngx_conf_merge_uint_value(conf->degrade, prev->degrade, 0);
|
||||
|
||||
return NGX_CONF_OK;
|
||||
}
|
||||
|
||||
|
||||
static char *
|
||||
ngx_http_degradation(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
||||
{
|
||||
ngx_http_degradation_main_conf_t *dmcf = conf;
|
||||
|
||||
ngx_str_t *value, s;
|
||||
|
||||
value = cf->args->elts;
|
||||
|
||||
if (ngx_strncmp(value[1].data, "sbrk=", 5) == 0) {
|
||||
|
||||
s.len = value[1].len - 5;
|
||||
s.data = value[1].data + 5;
|
||||
|
||||
dmcf->sbrk_size = ngx_parse_size(&s);
|
||||
if (dmcf->sbrk_size == (size_t) NGX_ERROR) {
|
||||
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
||||
"invalid sbrk size \"%V\"", &value[1]);
|
||||
return NGX_CONF_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return NGX_CONF_OK;
|
||||
}
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_http_degradation_init(ngx_conf_t *cf)
|
||||
{
|
||||
ngx_http_handler_pt *h;
|
||||
ngx_http_core_main_conf_t *cmcf;
|
||||
|
||||
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
||||
|
||||
h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
|
||||
if (h == NULL) {
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
*h = ngx_http_degradation_handler;
|
||||
|
||||
return NGX_OK;
|
||||
}
|
Loading…
Reference in New Issue
Block a user