nginx/src/http/ngx_http_busy_lock.c

307 lines
6.4 KiB
C
Raw Normal View History

2003-11-05 06:12:39 +08:00
/*
* Copyright (C) Igor Sysoev
*/
2003-11-05 06:12:39 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
static int ngx_http_busy_lock_look_cachable(ngx_http_busy_lock_t *bl,
ngx_http_busy_lock_ctx_t *bc,
int lock);
int ngx_http_busy_lock(ngx_http_busy_lock_t *bl, ngx_http_busy_lock_ctx_t *bc)
2003-11-06 01:03:41 +08:00
{
if (bl->busy < bl->max_busy) {
bl->busy++;
if (bc->time) {
bc->time = 0;
bl->waiting--;
}
return NGX_OK;
}
if (bc->time) {
if (bc->time < bl->timeout) {
ngx_add_timer(bc->event, 1000);
return NGX_AGAIN;
}
bl->waiting--;
return NGX_DONE;
}
if (bl->timeout == 0) {
return NGX_DONE;
}
if (bl->waiting < bl->max_waiting) {
bl->waiting++;
2004-03-01 05:03:02 +08:00
nginx-0.1.29-RELEASE import *) Feature: the ngx_http_ssi_module supports "include virtual" command. *) Feature: the ngx_http_ssi_module supports the condition command like 'if expr="$NAME"' and "else" and "endif" commands. Only one nested level is supported. *) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and DATE_GMT variables and "config timefmt" command. *) Feature: the "ssi_ignore_recycled_buffers" directive. *) Bugfix: the "echo" command did not show the default value for the empty QUERY_STRING variable. *) Change: the ngx_http_proxy_module was rewritten. *) Feature: the "proxy_redirect", "proxy_pass_request_headers", "proxy_pass_request_body", and "proxy_method" directives. *) Feature: the "proxy_set_header" directive. The "proxy_x_var" was canceled and must be replaced with the proxy_set_header directive. *) Change: the "proxy_preserve_host" is canceled and must be replaced with the "proxy_set_header Host $host" and the "proxy_redirect off" directives, the "proxy_set_header Host $host:$proxy_port" directive and the appropriate proxy_redirect directives. *) Change: the "proxy_set_x_real_ip" is canceled and must be replaced with the "proxy_set_header X-Real-IP $remote_addr" directive. *) Change: the "proxy_add_x_forwarded_for" is canceled and must be replaced with the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for" directive. *) Change: the "proxy_set_x_url" is canceled and must be replaced with the "proxy_set_header X-URL http://$host:$server_port$request_uri" directive. *) Feature: the "fastcgi_param" directive. *) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params" directive are canceled and must be replaced with the fastcgi_param directives. *) Feature: the "index" directive can use the variables. *) Feature: the "index" directive can be used at http and server levels. *) Change: the last index only in the "index" directive can be absolute. *) Feature: the "rewrite" directive can use the variables. *) Feature: the "internal" directive. *) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR, SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME, REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables. *) Change: nginx now passes the invalid lines in a client request headers or a backend response header. *) Bugfix: if the backend did not transfer response for a long time and the "send_timeout" was less than "proxy_read_timeout", then nginx returned the 408 response. *) Bugfix: the segmentation fault was occurred if the backend sent an invalid line in response header; the bug had appeared in 0.1.26. *) Bugfix: the segmentation fault may occurred in FastCGI fault tolerance configuration. *) Bugfix: the "expires" directive did not remove the previous "Expires" and "Cache-Control" headers. *) Bugfix: nginx did not take into account trailing dot in "Host" header line. *) Bugfix: the ngx_http_auth_module did not work under Linux. *) Bugfix: the rewrite directive worked incorrectly, if the arguments were in a request. *) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
#if 0
ngx_add_timer(bc->event, 1000);
bc->event->event_handler = bc->event_handler;
nginx-0.1.29-RELEASE import *) Feature: the ngx_http_ssi_module supports "include virtual" command. *) Feature: the ngx_http_ssi_module supports the condition command like 'if expr="$NAME"' and "else" and "endif" commands. Only one nested level is supported. *) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and DATE_GMT variables and "config timefmt" command. *) Feature: the "ssi_ignore_recycled_buffers" directive. *) Bugfix: the "echo" command did not show the default value for the empty QUERY_STRING variable. *) Change: the ngx_http_proxy_module was rewritten. *) Feature: the "proxy_redirect", "proxy_pass_request_headers", "proxy_pass_request_body", and "proxy_method" directives. *) Feature: the "proxy_set_header" directive. The "proxy_x_var" was canceled and must be replaced with the proxy_set_header directive. *) Change: the "proxy_preserve_host" is canceled and must be replaced with the "proxy_set_header Host $host" and the "proxy_redirect off" directives, the "proxy_set_header Host $host:$proxy_port" directive and the appropriate proxy_redirect directives. *) Change: the "proxy_set_x_real_ip" is canceled and must be replaced with the "proxy_set_header X-Real-IP $remote_addr" directive. *) Change: the "proxy_add_x_forwarded_for" is canceled and must be replaced with the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for" directive. *) Change: the "proxy_set_x_url" is canceled and must be replaced with the "proxy_set_header X-URL http://$host:$server_port$request_uri" directive. *) Feature: the "fastcgi_param" directive. *) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params" directive are canceled and must be replaced with the fastcgi_param directives. *) Feature: the "index" directive can use the variables. *) Feature: the "index" directive can be used at http and server levels. *) Change: the last index only in the "index" directive can be absolute. *) Feature: the "rewrite" directive can use the variables. *) Feature: the "internal" directive. *) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR, SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME, REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables. *) Change: nginx now passes the invalid lines in a client request headers or a backend response header. *) Bugfix: if the backend did not transfer response for a long time and the "send_timeout" was less than "proxy_read_timeout", then nginx returned the 408 response. *) Bugfix: the segmentation fault was occurred if the backend sent an invalid line in response header; the bug had appeared in 0.1.26. *) Bugfix: the segmentation fault may occurred in FastCGI fault tolerance configuration. *) Bugfix: the "expires" directive did not remove the previous "Expires" and "Cache-Control" headers. *) Bugfix: nginx did not take into account trailing dot in "Host" header line. *) Bugfix: the ngx_http_auth_module did not work under Linux. *) Bugfix: the rewrite directive worked incorrectly, if the arguments were in a request. *) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
#endif
/* TODO: ngx_handle_level_read_event() */
return NGX_AGAIN;
}
return NGX_ERROR;
}
int ngx_http_busy_lock_cachable(ngx_http_busy_lock_t *bl,
ngx_http_busy_lock_ctx_t *bc, int lock)
{
int rc;
rc = ngx_http_busy_lock_look_cachable(bl, bc, lock);
2004-02-12 01:08:49 +08:00
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, bc->event->log, 0,
"http busylock: %d w:%d mw::%d",
rc, bl->waiting, bl->max_waiting);
if (rc == NGX_OK) { /* no the same request, there's free slot */
return NGX_OK;
}
if (rc == NGX_ERROR && !lock) { /* no the same request, no free slot */
return NGX_OK;
}
/* rc == NGX_AGAIN: the same request */
if (bc->time) {
if (bc->time < bl->timeout) {
ngx_add_timer(bc->event, 1000);
return NGX_AGAIN;
}
bl->waiting--;
return NGX_DONE;
}
if (bl->timeout == 0) {
return NGX_DONE;
}
if (bl->waiting < bl->max_waiting) {
nginx-0.1.29-RELEASE import *) Feature: the ngx_http_ssi_module supports "include virtual" command. *) Feature: the ngx_http_ssi_module supports the condition command like 'if expr="$NAME"' and "else" and "endif" commands. Only one nested level is supported. *) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and DATE_GMT variables and "config timefmt" command. *) Feature: the "ssi_ignore_recycled_buffers" directive. *) Bugfix: the "echo" command did not show the default value for the empty QUERY_STRING variable. *) Change: the ngx_http_proxy_module was rewritten. *) Feature: the "proxy_redirect", "proxy_pass_request_headers", "proxy_pass_request_body", and "proxy_method" directives. *) Feature: the "proxy_set_header" directive. The "proxy_x_var" was canceled and must be replaced with the proxy_set_header directive. *) Change: the "proxy_preserve_host" is canceled and must be replaced with the "proxy_set_header Host $host" and the "proxy_redirect off" directives, the "proxy_set_header Host $host:$proxy_port" directive and the appropriate proxy_redirect directives. *) Change: the "proxy_set_x_real_ip" is canceled and must be replaced with the "proxy_set_header X-Real-IP $remote_addr" directive. *) Change: the "proxy_add_x_forwarded_for" is canceled and must be replaced with the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for" directive. *) Change: the "proxy_set_x_url" is canceled and must be replaced with the "proxy_set_header X-URL http://$host:$server_port$request_uri" directive. *) Feature: the "fastcgi_param" directive. *) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params" directive are canceled and must be replaced with the fastcgi_param directives. *) Feature: the "index" directive can use the variables. *) Feature: the "index" directive can be used at http and server levels. *) Change: the last index only in the "index" directive can be absolute. *) Feature: the "rewrite" directive can use the variables. *) Feature: the "internal" directive. *) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR, SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME, REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables. *) Change: nginx now passes the invalid lines in a client request headers or a backend response header. *) Bugfix: if the backend did not transfer response for a long time and the "send_timeout" was less than "proxy_read_timeout", then nginx returned the 408 response. *) Bugfix: the segmentation fault was occurred if the backend sent an invalid line in response header; the bug had appeared in 0.1.26. *) Bugfix: the segmentation fault may occurred in FastCGI fault tolerance configuration. *) Bugfix: the "expires" directive did not remove the previous "Expires" and "Cache-Control" headers. *) Bugfix: nginx did not take into account trailing dot in "Host" header line. *) Bugfix: the ngx_http_auth_module did not work under Linux. *) Bugfix: the rewrite directive worked incorrectly, if the arguments were in a request. *) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
#if 0
bl->waiting++;
ngx_add_timer(bc->event, 1000);
bc->event->event_handler = bc->event_handler;
nginx-0.1.29-RELEASE import *) Feature: the ngx_http_ssi_module supports "include virtual" command. *) Feature: the ngx_http_ssi_module supports the condition command like 'if expr="$NAME"' and "else" and "endif" commands. Only one nested level is supported. *) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and DATE_GMT variables and "config timefmt" command. *) Feature: the "ssi_ignore_recycled_buffers" directive. *) Bugfix: the "echo" command did not show the default value for the empty QUERY_STRING variable. *) Change: the ngx_http_proxy_module was rewritten. *) Feature: the "proxy_redirect", "proxy_pass_request_headers", "proxy_pass_request_body", and "proxy_method" directives. *) Feature: the "proxy_set_header" directive. The "proxy_x_var" was canceled and must be replaced with the proxy_set_header directive. *) Change: the "proxy_preserve_host" is canceled and must be replaced with the "proxy_set_header Host $host" and the "proxy_redirect off" directives, the "proxy_set_header Host $host:$proxy_port" directive and the appropriate proxy_redirect directives. *) Change: the "proxy_set_x_real_ip" is canceled and must be replaced with the "proxy_set_header X-Real-IP $remote_addr" directive. *) Change: the "proxy_add_x_forwarded_for" is canceled and must be replaced with the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for" directive. *) Change: the "proxy_set_x_url" is canceled and must be replaced with the "proxy_set_header X-URL http://$host:$server_port$request_uri" directive. *) Feature: the "fastcgi_param" directive. *) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params" directive are canceled and must be replaced with the fastcgi_param directives. *) Feature: the "index" directive can use the variables. *) Feature: the "index" directive can be used at http and server levels. *) Change: the last index only in the "index" directive can be absolute. *) Feature: the "rewrite" directive can use the variables. *) Feature: the "internal" directive. *) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR, SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME, REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables. *) Change: nginx now passes the invalid lines in a client request headers or a backend response header. *) Bugfix: if the backend did not transfer response for a long time and the "send_timeout" was less than "proxy_read_timeout", then nginx returned the 408 response. *) Bugfix: the segmentation fault was occurred if the backend sent an invalid line in response header; the bug had appeared in 0.1.26. *) Bugfix: the segmentation fault may occurred in FastCGI fault tolerance configuration. *) Bugfix: the "expires" directive did not remove the previous "Expires" and "Cache-Control" headers. *) Bugfix: nginx did not take into account trailing dot in "Host" header line. *) Bugfix: the ngx_http_auth_module did not work under Linux. *) Bugfix: the rewrite directive worked incorrectly, if the arguments were in a request. *) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
#endif
/* TODO: ngx_handle_level_read_event() */
return NGX_AGAIN;
}
return NGX_ERROR;
}
2003-11-11 05:09:22 +08:00
void ngx_http_busy_unlock(ngx_http_busy_lock_t *bl,
ngx_http_busy_lock_ctx_t *bc)
{
2003-11-19 00:49:00 +08:00
if (bl == NULL) {
return;
}
2003-11-11 05:09:22 +08:00
if (bl->md5) {
bl->md5_mask[bc->slot / 8] &= ~(1 << (bc->slot & 7));
bl->cachable--;
}
bl->busy--;
}
static int ngx_http_busy_lock_look_cachable(ngx_http_busy_lock_t *bl,
ngx_http_busy_lock_ctx_t *bc,
int lock)
{
int i, b, cachable, free;
2003-11-06 01:03:41 +08:00
u_int mask;
b = 0;
cachable = 0;
2003-11-06 01:03:41 +08:00
free = -1;
#if (NGX_SUPPRESS_WARN)
mask = 0;
#endif
for (i = 0; i < bl->max_busy; i++) {
2003-11-06 01:03:41 +08:00
if ((b & 7) == 0) {
mask = bl->md5_mask[i / 8];
2003-11-06 01:03:41 +08:00
}
if (mask & 1) {
if (ngx_memcmp(&bl->md5[i * 16], bc->md5, 16) == 0) {
2003-11-06 01:03:41 +08:00
return NGX_AGAIN;
}
cachable++;
2003-11-06 01:03:41 +08:00
} else if (free == -1) {
free = i;
}
#if 1
if (cachable == bl->cachable) {
if (free == -1 && cachable < bl->max_busy) {
2003-11-06 01:03:41 +08:00
free = i + 1;
}
break;
}
#endif
2003-11-06 01:03:41 +08:00
mask >>= 1;
b++;
}
if (free == -1) {
return NGX_ERROR;
}
if (lock) {
if (bl->busy == bl->max_busy) {
return NGX_ERROR;
}
ngx_memcpy(&bl->md5[free * 16], bc->md5, 16);
bl->md5_mask[free / 8] |= 1 << (free & 7);
bc->slot = free;
2003-11-06 01:03:41 +08:00
bl->cachable++;
bl->busy++;
}
2003-11-06 01:03:41 +08:00
return NGX_OK;
}
2003-11-05 06:12:39 +08:00
char *ngx_http_set_busy_lock_slot(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf)
{
char *p = conf;
2004-03-16 15:10:12 +08:00
ngx_uint_t i, dup, invalid;
ngx_str_t *value, line;
2003-11-05 06:12:39 +08:00
ngx_http_busy_lock_t *bl, **blp;
blp = (ngx_http_busy_lock_t **) (p + cmd->offset);
if (*blp) {
return "is duplicate";
}
/* ngx_calloc_shared() */
bl = ngx_pcalloc(cf->pool, sizeof(ngx_http_busy_lock_t));
if (bl == NULL) {
2003-11-05 06:12:39 +08:00
return NGX_CONF_ERROR;
}
*blp = bl;
2004-03-01 05:03:02 +08:00
/* ngx_calloc_shared() */
bl->mutex = ngx_pcalloc(cf->pool, sizeof(ngx_event_mutex_t));
if (bl->mutex == NULL) {
2004-03-01 05:03:02 +08:00
return NGX_CONF_ERROR;
}
dup = 0;
invalid = 0;
2003-11-26 04:44:56 +08:00
value = cf->args->elts;
2003-11-05 06:12:39 +08:00
for (i = 1; i < cf->args->nelts; i++) {
if (value[i].data[1] != '=') {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid value \"%s\"", value[i].data);
return NGX_CONF_ERROR;
}
2003-11-05 06:12:39 +08:00
switch (value[i].data[0]) {
case 'b':
if (bl->max_busy) {
dup = 1;
break;
2003-11-05 06:12:39 +08:00
}
bl->max_busy = ngx_atoi(value[i].data + 2, value[i].len - 2);
if (bl->max_busy == NGX_ERROR) {
invalid = 1;
break;
2003-11-05 06:12:39 +08:00
}
continue;
case 'w':
2003-11-05 06:12:39 +08:00
if (bl->max_waiting) {
dup = 1;
break;
2003-11-05 06:12:39 +08:00
}
bl->max_waiting = ngx_atoi(value[i].data + 2, value[i].len - 2);
if (bl->max_waiting == NGX_ERROR) {
invalid = 1;
break;
}
continue;
case 't':
if (bl->timeout) {
dup = 1;
break;
}
line.len = value[i].len - 2;
line.data = value[i].data + 2;
bl->timeout = ngx_parse_time(&line, 1);
if (bl->timeout == NGX_ERROR) {
invalid = 1;
break;
2003-11-05 06:12:39 +08:00
}
continue;
default:
invalid = 1;
2003-11-05 06:12:39 +08:00
}
if (dup) {
2003-11-05 06:12:39 +08:00
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"duplicate value \"%s\"", value[i].data);
2003-11-05 06:12:39 +08:00
return NGX_CONF_ERROR;
}
if (invalid) {
2003-11-05 06:12:39 +08:00
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid value \"%s\"", value[i].data);
2003-11-05 06:12:39 +08:00
return NGX_CONF_ERROR;
}
}
if (bl->timeout == 0 && bl->max_waiting) {
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
2004-03-03 05:14:37 +08:00
"busy lock waiting is useless with zero timeout, ignoring");
}
2003-11-05 06:12:39 +08:00
return NGX_CONF_OK;
}