Mercurial > hg > nginx-mail
comparison src/http/modules/ngx_http_degradation_module.c @ 665:0b460e61bdcd default tip
Merge with nginx 1.0.0.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 25 Apr 2011 04:22:17 +0400 |
parents | 428c6e58046a |
children |
comparison
equal
deleted
inserted
replaced
572:06419a2298a9 | 665:0b460e61bdcd |
---|---|
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 ngx_http_degradation_loc_conf_t *dlcf; | |
93 | |
94 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_degradation_module); | |
95 | |
96 if (dlcf->degrade && ngx_http_degraded(r)) { | |
97 return dlcf->degrade; | |
98 } | |
99 | |
100 return NGX_DECLINED; | |
101 } | |
102 | |
103 | |
104 ngx_uint_t | |
105 ngx_http_degraded(ngx_http_request_t *r) | |
106 { | |
107 time_t now; | |
108 ngx_uint_t log; | |
109 static size_t sbrk_size; | |
110 static time_t sbrk_time; | |
111 ngx_http_degradation_main_conf_t *dmcf; | |
112 | |
113 dmcf = ngx_http_get_module_main_conf(r, ngx_http_degradation_module); | |
114 | |
115 if (dmcf->sbrk_size) { | |
116 | |
117 log = 0; | |
118 now = ngx_time(); | |
119 | |
120 /* lock mutex */ | |
121 | |
122 if (now != sbrk_time) { | |
123 | |
124 /* | |
125 * ELF/i386 is loaded at 0x08000000, 128M | |
126 * ELF/amd64 is loaded at 0x00400000, 4M | |
127 * | |
128 * use a function address to substract the loading address | |
129 */ | |
130 | |
131 sbrk_size = (size_t) sbrk(0) - ((uintptr_t) ngx_palloc & ~0x3FFFFF); | |
132 sbrk_time = now; | |
133 log = 1; | |
134 } | |
135 | |
136 /* unlock mutex */ | |
137 | |
138 if (sbrk_size >= dmcf->sbrk_size) { | |
139 if (log) { | |
140 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, | |
141 "degradation sbrk:%uzM", | |
142 sbrk_size / (1024 * 1024)); | |
143 } | |
144 | |
145 return 1; | |
146 } | |
147 } | |
148 | |
149 return 0; | |
150 } | |
151 | |
152 | |
153 static void * | |
154 ngx_http_degradation_create_main_conf(ngx_conf_t *cf) | |
155 { | |
156 ngx_http_degradation_main_conf_t *dmcf; | |
157 | |
158 dmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_degradation_main_conf_t)); | |
159 if (dmcf == NULL) { | |
160 return NULL; | |
161 } | |
162 | |
163 return dmcf; | |
164 } | |
165 | |
166 | |
167 static void * | |
168 ngx_http_degradation_create_loc_conf(ngx_conf_t *cf) | |
169 { | |
170 ngx_http_degradation_loc_conf_t *conf; | |
171 | |
172 conf = ngx_palloc(cf->pool, sizeof(ngx_http_degradation_loc_conf_t)); | |
173 if (conf == NULL) { | |
174 return NULL; | |
175 } | |
176 | |
177 conf->degrade = NGX_CONF_UNSET_UINT; | |
178 | |
179 return conf; | |
180 } | |
181 | |
182 | |
183 static char * | |
184 ngx_http_degradation_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
185 { | |
186 ngx_http_degradation_loc_conf_t *prev = parent; | |
187 ngx_http_degradation_loc_conf_t *conf = child; | |
188 | |
189 ngx_conf_merge_uint_value(conf->degrade, prev->degrade, 0); | |
190 | |
191 return NGX_CONF_OK; | |
192 } | |
193 | |
194 | |
195 static char * | |
196 ngx_http_degradation(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
197 { | |
198 ngx_http_degradation_main_conf_t *dmcf = conf; | |
199 | |
200 ngx_str_t *value, s; | |
201 | |
202 value = cf->args->elts; | |
203 | |
204 if (ngx_strncmp(value[1].data, "sbrk=", 5) == 0) { | |
205 | |
206 s.len = value[1].len - 5; | |
207 s.data = value[1].data + 5; | |
208 | |
209 dmcf->sbrk_size = ngx_parse_size(&s); | |
210 if (dmcf->sbrk_size == (size_t) NGX_ERROR) { | |
211 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
212 "invalid sbrk size \"%V\"", &value[1]); | |
213 return NGX_CONF_ERROR; | |
214 } | |
215 | |
216 return NGX_CONF_OK; | |
217 } | |
218 | |
219 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
220 "invalid parameter \"%V\"", &value[1]); | |
221 | |
222 return NGX_CONF_ERROR; | |
223 } | |
224 | |
225 | |
226 static ngx_int_t | |
227 ngx_http_degradation_init(ngx_conf_t *cf) | |
228 { | |
229 ngx_http_handler_pt *h; | |
230 ngx_http_core_main_conf_t *cmcf; | |
231 | |
232 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
233 | |
234 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers); | |
235 if (h == NULL) { | |
236 return NGX_ERROR; | |
237 } | |
238 | |
239 *h = ngx_http_degradation_handler; | |
240 | |
241 return NGX_OK; | |
242 } |