comparison src/http/modules/ngx_http_degradation_module.c @ 3772:c1e3cb4c669c

ngx_http_degraded()
author Igor Sysoev <igor@sysoev.ru>
date Mon, 04 Oct 2010 14:59:41 +0000
parents de27a5d044ff
children 147d8a86dd02
comparison
equal deleted inserted replaced
3771:5dbbc90e7002 3772:c1e3cb4c669c
84 NULL, /* exit master */ 84 NULL, /* exit master */
85 NGX_MODULE_V1_PADDING 85 NGX_MODULE_V1_PADDING
86 }; 86 };
87 87
88 88
89 static ngx_uint_t ngx_degraded;
90
91
89 static ngx_int_t 92 static ngx_int_t
90 ngx_http_degradation_handler(ngx_http_request_t *r) 93 ngx_http_degradation_handler(ngx_http_request_t *r)
91 { 94 {
95 ngx_http_degradation_loc_conf_t *dlcf;
96
97 dlcf = ngx_http_get_module_loc_conf(r, ngx_http_degradation_module);
98
99 if (dlcf->degrade && ngx_http_degraded(r)) {
100 return dlcf->degrade;
101 }
102
103 return NGX_DECLINED;
104 }
105
106
107 ngx_uint_t
108 ngx_http_degraded(ngx_http_request_t *r)
109 {
92 time_t now; 110 time_t now;
111 ngx_uint_t log;
93 static size_t sbrk_size; 112 static size_t sbrk_size;
94 static time_t sbrk_time; 113 static time_t sbrk_time;
95 ngx_http_degradation_loc_conf_t *dlcf;
96 ngx_http_degradation_main_conf_t *dmcf; 114 ngx_http_degradation_main_conf_t *dmcf;
97 115
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); 116 dmcf = ngx_http_get_module_main_conf(r, ngx_http_degradation_module);
105 117
106 if (dmcf->sbrk_size) { 118 if (dmcf->sbrk_size) {
107 119
120 log = 0;
108 now = ngx_time(); 121 now = ngx_time();
109 122
110 /* lock mutex */ 123 /* lock mutex */
111 124
112 if (now != sbrk_time) { 125 if (now != sbrk_time) {
118 * use a function address to substract the loading address 131 * use a function address to substract the loading address
119 */ 132 */
120 133
121 sbrk_size = (size_t) sbrk(0) - ((uintptr_t) ngx_palloc & ~0x3FFFFF); 134 sbrk_size = (size_t) sbrk(0) - ((uintptr_t) ngx_palloc & ~0x3FFFFF);
122 sbrk_time = now; 135 sbrk_time = now;
136 log = 1;
123 } 137 }
124 138
125 /* unlock mutex */ 139 /* unlock mutex */
126 140
127 if (sbrk_size >= dmcf->sbrk_size) { 141 if (sbrk_size >= dmcf->sbrk_size) {
128 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 142 ngx_degraded = 1;
129 "degradation sbrk: %uz", sbrk_size); 143
130 144 if (log) {
131 return dlcf->degrade; 145 ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0,
146 "degradation sbrk:%uzM",
147 sbrk_size / (1024 * 1024));
148 }
149
150 return 1;
132 } 151 }
133 } 152 }
134 153
135 return NGX_DECLINED; 154 ngx_degraded = 0;
155
156 return 0;
136 } 157 }
137 158
138 159
139 static void * 160 static void *
140 ngx_http_degradation_create_main_conf(ngx_conf_t *cf) 161 ngx_http_degradation_create_main_conf(ngx_conf_t *cf)