3326
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 typedef struct {
|
|
13 size_t sbrk_size;
|
|
14 } ngx_http_degradation_main_conf_t;
|
|
15
|
|
16
|
|
17 typedef struct {
|
|
18 ngx_uint_t degrade;
|
|
19 } ngx_http_degradation_loc_conf_t;
|
|
20
|
|
21
|
|
22 static ngx_conf_enum_t ngx_http_degrade[] = {
|
|
23 { ngx_string("204"), 204 },
|
|
24 { ngx_string("444"), 444 },
|
|
25 { ngx_null_string, 0 }
|
|
26 };
|
|
27
|
|
28
|
|
29 static void *ngx_http_degradation_create_main_conf(ngx_conf_t *cf);
|
|
30 static void *ngx_http_degradation_create_loc_conf(ngx_conf_t *cf);
|
|
31 static char *ngx_http_degradation_merge_loc_conf(ngx_conf_t *cf, void *parent,
|
|
32 void *child);
|
|
33 static char *ngx_http_degradation(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
34 void *conf);
|
|
35 static ngx_int_t ngx_http_degradation_init(ngx_conf_t *cf);
|
|
36
|
|
37
|
|
38 static ngx_command_t ngx_http_degradation_commands[] = {
|
|
39
|
|
40 { ngx_string("degradation"),
|
|
41 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
|
|
42 ngx_http_degradation,
|
|
43 NGX_HTTP_MAIN_CONF_OFFSET,
|
|
44 0,
|
|
45 NULL },
|
|
46
|
|
47 { ngx_string("degrade"),
|
|
48 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
49 ngx_conf_set_enum_slot,
|
|
50 NGX_HTTP_LOC_CONF_OFFSET,
|
|
51 offsetof(ngx_http_degradation_loc_conf_t, degrade),
|
|
52 &ngx_http_degrade },
|
|
53
|
|
54 ngx_null_command
|
|
55 };
|
|
56
|
|
57
|
|
58 static ngx_http_module_t ngx_http_degradation_module_ctx = {
|
|
59 NULL, /* preconfiguration */
|
|
60 ngx_http_degradation_init, /* postconfiguration */
|
|
61
|
|
62 ngx_http_degradation_create_main_conf, /* create main configuration */
|
|
63 NULL, /* init main configuration */
|
|
64
|
|
65 NULL, /* create server configuration */
|
|
66 NULL, /* merge server configuration */
|
|
67
|
|
68 ngx_http_degradation_create_loc_conf, /* create location configuration */
|
|
69 ngx_http_degradation_merge_loc_conf /* merge location configuration */
|
|
70 };
|
|
71
|
|
72
|
|
73 ngx_module_t ngx_http_degradation_module = {
|
|
74 NGX_MODULE_V1,
|
|
75 &ngx_http_degradation_module_ctx, /* module context */
|
|
76 ngx_http_degradation_commands, /* module directives */
|
|
77 NGX_HTTP_MODULE, /* module type */
|
|
78 NULL, /* init master */
|
|
79 NULL, /* init module */
|
|
80 NULL, /* init process */
|
|
81 NULL, /* init thread */
|
|
82 NULL, /* exit thread */
|
|
83 NULL, /* exit process */
|
|
84 NULL, /* exit master */
|
|
85 NGX_MODULE_V1_PADDING
|
|
86 };
|
|
87
|
|
88
|
|
89 static ngx_int_t
|
|
90 ngx_http_degradation_handler(ngx_http_request_t *r)
|
|
91 {
|
|
92 time_t now;
|
|
93 static size_t sbrk_size;
|
|
94 static time_t sbrk_time;
|
|
95 ngx_http_degradation_loc_conf_t *dlcf;
|
|
96 ngx_http_degradation_main_conf_t *dmcf;
|
|
97
|
|
98 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_degradation_module);
|
|
99
|
|
100 if (dlcf->degrade == 0) {
|
|
101 return NGX_DECLINED;
|
|
102 }
|
|
103
|
|
104 dmcf = ngx_http_get_module_main_conf(r, ngx_http_degradation_module);
|
|
105
|
|
106 if (dmcf->sbrk_size) {
|
|
107
|
|
108 now = ngx_time();
|
|
109
|
|
110 /* lock mutex */
|
|
111
|
|
112 if (now != sbrk_time) {
|
|
113
|
|
114 /*
|
|
115 * ELF/i386 is loaded at 0x08000000, 128M
|
|
116 * ELF/amd64 is loaded at 0x00400000, 4M
|
|
117 *
|
|
118 * use a function address to substract the loading address
|
|
119 */
|
|
120
|
|
121 sbrk_size = (size_t) sbrk(0) - ((uintptr_t) ngx_palloc & ~0x3FFFFF);
|
|
122 sbrk_time = now;
|
|
123 }
|
|
124
|
|
125 /* unlock mutex */
|
|
126
|
|
127 if (sbrk_size >= dmcf->sbrk_size) {
|
|
128 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
129 "degradation sbrk: %uz", sbrk_size);
|
|
130
|
|
131 return dlcf->degrade;
|
|
132 }
|
|
133 }
|
|
134
|
|
135 return NGX_DECLINED;
|
|
136 }
|
|
137
|
|
138
|
|
139 static void *
|
|
140 ngx_http_degradation_create_main_conf(ngx_conf_t *cf)
|
|
141 {
|
|
142 ngx_http_degradation_main_conf_t *dmcf;
|
|
143
|
|
144 dmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_degradation_main_conf_t));
|
|
145 if (dmcf == NULL) {
|
|
146 return NULL;
|
|
147 }
|
|
148
|
|
149 return dmcf;
|
|
150 }
|
|
151
|
|
152
|
|
153 static void *
|
|
154 ngx_http_degradation_create_loc_conf(ngx_conf_t *cf)
|
|
155 {
|
|
156 ngx_http_degradation_loc_conf_t *conf;
|
|
157
|
|
158 conf = ngx_palloc(cf->pool, sizeof(ngx_http_degradation_loc_conf_t));
|
|
159 if (conf == NULL) {
|
|
160 return NULL;
|
|
161 }
|
|
162
|
|
163 conf->degrade = NGX_CONF_UNSET_UINT;
|
|
164
|
|
165 return conf;
|
|
166 }
|
|
167
|
|
168
|
|
169 static char *
|
|
170 ngx_http_degradation_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
171 {
|
|
172 ngx_http_degradation_loc_conf_t *prev = parent;
|
|
173 ngx_http_degradation_loc_conf_t *conf = child;
|
|
174
|
|
175 ngx_conf_merge_uint_value(conf->degrade, prev->degrade, 0);
|
|
176
|
|
177 return NGX_CONF_OK;
|
|
178 }
|
|
179
|
|
180
|
|
181 static char *
|
|
182 ngx_http_degradation(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
183 {
|
|
184 ngx_http_degradation_main_conf_t *dmcf = conf;
|
|
185
|
|
186 ngx_str_t *value, s;
|
|
187
|
|
188 value = cf->args->elts;
|
|
189
|
|
190 if (ngx_strncmp(value[1].data, "sbrk=", 5) == 0) {
|
|
191
|
|
192 s.len = value[1].len - 5;
|
|
193 s.data = value[1].data + 5;
|
|
194
|
|
195 dmcf->sbrk_size = ngx_parse_size(&s);
|
|
196 if (dmcf->sbrk_size == (size_t) NGX_ERROR) {
|
|
197 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
198 "invalid sbrk size \"%V\"", &value[1]);
|
|
199 return NGX_CONF_ERROR;
|
|
200 }
|
|
201 }
|
|
202
|
|
203 return NGX_CONF_OK;
|
|
204 }
|
|
205
|
|
206
|
|
207 static ngx_int_t
|
|
208 ngx_http_degradation_init(ngx_conf_t *cf)
|
|
209 {
|
|
210 ngx_http_handler_pt *h;
|
|
211 ngx_http_core_main_conf_t *cmcf;
|
|
212
|
|
213 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
214
|
|
215 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
|
|
216 if (h == NULL) {
|
|
217 return NGX_ERROR;
|
|
218 }
|
|
219
|
|
220 *h = ngx_http_degradation_handler;
|
|
221
|
|
222 return NGX_OK;
|
|
223 }
|