nginx/src/event/ngx_event_connect.c

346 lines
7.7 KiB
C
Raw Normal View History

2003-05-12 23:52:24 +08:00
/*
* Copyright (C) Igor Sysoev
*/
2003-10-02 13:39:37 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
2003-05-12 23:52:24 +08:00
#include <ngx_event_connect.h>
2003-08-14 14:00:28 +08:00
ngx_int_t
ngx_event_connect_peer(ngx_peer_connection_t *pc)
2003-05-12 23:52:24 +08:00
{
int rc;
ngx_uint_t level, i;
u_int event;
time_t now;
ngx_err_t err;
ngx_peer_t *peer;
ngx_socket_t s;
ngx_event_t *rev, *wev;
ngx_connection_t *c;
2003-07-23 21:10:12 +08:00
now = ngx_time();
2003-05-12 23:52:24 +08:00
2003-07-23 03:53:10 +08:00
/* ngx_lock_mutex(pc->peers->mutex); */
if (pc->peers->last_cached) {
/* cached connection */
2004-07-07 23:01:00 +08:00
c = pc->peers->cached[pc->peers->last_cached];
2003-07-23 03:53:10 +08:00
pc->peers->last_cached--;
/* ngx_unlock_mutex(pc->peers->mutex); */
2004-07-07 23:01:00 +08:00
#if (NGX_THREADS)
c->read->lock = c->read->own_lock;
c->write->lock = c->write->own_lock;
#endif
pc->connection = c;
2003-07-23 03:53:10 +08:00
pc->cached = 1;
2003-07-23 03:53:10 +08:00
return NGX_OK;
}
pc->cached = 0;
2003-08-01 22:56:33 +08:00
pc->connection = NULL;
2003-07-21 05:15:59 +08:00
2003-10-31 15:10:36 +08:00
if (pc->peers->number == 1) {
peer = &pc->peers->peer[0];
2003-10-31 15:10:36 +08:00
} else {
2003-05-12 23:52:24 +08:00
2003-07-21 05:15:59 +08:00
/* there are several peers */
2003-05-12 23:52:24 +08:00
2003-07-22 00:24:25 +08:00
if (pc->tries == pc->peers->number) {
2003-05-12 23:52:24 +08:00
2003-07-21 05:15:59 +08:00
/* it's a first try - get a current peer */
pc->cur_peer = pc->peers->current;
pc->peers->weight--;
if (pc->peers->weight == 0) {
pc->peers->current++;
}
2003-05-12 23:52:24 +08:00
2003-10-02 13:39:37 +08:00
if (pc->peers->current >= pc->peers->number) {
2003-07-22 00:24:25 +08:00
pc->peers->current = 0;
2003-05-12 23:52:24 +08:00
}
if (pc->peers->weight == 0) {
pc->peers->weight = pc->peers->peer[pc->peers->current].weight;
}
}
2003-07-21 05:15:59 +08:00
for ( ;; ) {
peer = &pc->peers->peer[pc->cur_peer];
2003-05-12 23:52:24 +08:00
if (peer->max_fails == 0 || peer->fails <= peer->max_fails) {
break;
}
if (now - peer->accessed > peer->fail_timeout) {
peer->fails = 0;
break;
}
2003-05-12 23:52:24 +08:00
pc->cur_peer++;
2003-05-12 23:52:24 +08:00
if (pc->cur_peer >= pc->peers->number) {
pc->cur_peer = 0;
}
2003-05-12 23:52:24 +08:00
pc->tries--;
2003-05-12 23:52:24 +08:00
if (pc->tries == 0) {
/* all peers failed, mark them as live for quick recovery */
for (i = 0; i < pc->peers->number; i++) {
pc->peers->peer[i].fails = 0;
}
/* ngx_unlock_mutex(pc->peers->mutex); */
2003-10-02 13:39:37 +08:00
return NGX_BUSY;
2003-05-12 23:52:24 +08:00
}
}
}
2003-07-23 21:10:12 +08:00
/* ngx_unlock_mutex(pc->peers->mutex); */
2003-05-12 23:52:24 +08:00
s = ngx_socket(peer->sockaddr->sa_family, SOCK_STREAM, 0);
2003-05-12 23:52:24 +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
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, pc->log, 0,
"socket %d", s);
2003-05-12 23:52:24 +08:00
if (s == -1) {
2003-07-23 03:53:10 +08:00
ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
2003-05-12 23:52:24 +08:00
ngx_socket_n " failed");
2003-07-21 05:15:59 +08:00
return NGX_ERROR;
2003-05-12 23:52:24 +08:00
}
2004-04-22 02:54:33 +08:00
c = ngx_get_connection(s, pc->log);
2004-04-22 02:54:33 +08:00
if (c == NULL) {
2004-04-22 02:54:33 +08:00
if (ngx_close_socket(s) == -1) {
ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
ngx_close_socket_n "failed");
}
return NGX_ERROR;
}
2003-07-23 03:53:10 +08:00
if (pc->rcvbuf) {
2003-05-12 23:52:24 +08:00
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
(const void *) &pc->rcvbuf, sizeof(int)) == -1)
{
2003-07-23 03:53:10 +08:00
ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
2003-05-12 23:52:24 +08:00
"setsockopt(SO_RCVBUF) failed");
ngx_free_connection(c);
2003-05-12 23:52:24 +08:00
if (ngx_close_socket(s) == -1) {
2003-07-23 03:53:10 +08:00
ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
2003-05-12 23:52:24 +08:00
ngx_close_socket_n " failed");
}
2003-07-21 05:15:59 +08:00
return NGX_ERROR;
2003-05-12 23:52:24 +08:00
}
}
if (ngx_nonblocking(s) == -1) {
2003-07-23 03:53:10 +08:00
ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
2003-05-12 23:52:24 +08:00
ngx_nonblocking_n " failed");
ngx_free_connection(c);
2003-05-12 23:52:24 +08:00
if (ngx_close_socket(s) == -1) {
2003-07-23 03:53:10 +08:00
ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
2003-05-12 23:52:24 +08:00
ngx_close_socket_n " failed");
}
2003-07-21 05:15:59 +08:00
return NGX_ERROR;
2003-05-12 23:52:24 +08:00
}
c->recv = ngx_recv;
c->send = ngx_send;
c->recv_chain = ngx_recv_chain;
c->send_chain = ngx_send_chain;
c->log_error = pc->log_error;
2003-07-23 03:53:10 +08:00
if (peer->sockaddr->sa_family != AF_INET) {
c->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
c->tcp_nodelay = NGX_TCP_NODELAY_DISABLED;
2003-07-23 03:53:10 +08:00
}
2003-07-21 05:15:59 +08:00
rev = c->read;
wev = c->write;
2003-05-12 23:52:24 +08:00
2003-11-20 00:26:41 +08:00
rev->log = pc->log;
wev->log = pc->log;
2003-05-12 23:52:24 +08:00
2003-07-23 03:53:10 +08:00
pc->connection = c;
2004-02-12 01:08:49 +08:00
/*
* TODO: MT: - ngx_atomic_fetch_add()
2004-02-12 01:08:49 +08:00
* or protection by critical section or mutex
*
* TODO: MP: - allocated in a shared memory
* - ngx_atomic_fetch_add()
2004-02-12 01:08:49 +08:00
* or protection by critical section or mutex
*/
c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);
2004-02-12 01:08:49 +08:00
2004-07-05 23:08:23 +08:00
#if (NGX_THREADS)
rev->lock = pc->lock;
wev->lock = pc->lock;
2004-07-07 14:15:04 +08:00
rev->own_lock = &c->lock;
wev->own_lock = &c->lock;
2004-07-05 23:08:23 +08:00
#endif
2003-07-23 03:53:10 +08:00
if (ngx_add_conn) {
if (ngx_add_conn(c) == NGX_ERROR) {
return NGX_ERROR;
}
}
2003-07-23 03:53:10 +08:00
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, pc->log, 0,
"connect to %V, fd:%d #%d", &peer->name, s, c->number);
2003-10-28 00:16:17 +08:00
rc = connect(s, peer->sockaddr, peer->socklen);
2003-07-23 21:10:12 +08:00
if (rc == -1) {
err = ngx_socket_errno;
2003-11-17 05:49:42 +08:00
2004-03-15 04:46:25 +08:00
/* Winsock returns WSAEWOULDBLOCK (NGX_EAGAIN) */
2003-11-17 05:49:42 +08:00
if (err != NGX_EINPROGRESS && err != NGX_EAGAIN) {
2003-07-23 21:10:12 +08:00
if (err == NGX_ECONNREFUSED || err == NGX_EHOSTUNREACH) {
level = NGX_LOG_ERR;
} else {
level = NGX_LOG_CRIT;
}
ngx_log_error(level, c->log, err, "connect() to %V failed",
&peer->name);
2003-10-07 23:30:05 +08:00
return NGX_DECLINED;
2003-07-23 21:10:12 +08:00
}
}
2004-06-01 14:04:46 +08:00
if (ngx_add_conn) {
if (rc == -1) {
2004-06-01 14:04:46 +08:00
/* NGX_EINPROGRESS */
2004-06-01 14:04:46 +08:00
return NGX_AGAIN;
}
2004-06-01 14:04:46 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, pc->log, 0, "connected");
wev->ready = 1;
2004-06-01 14:04:46 +08:00
return NGX_OK;
}
2003-10-29 16:30:44 +08:00
if (ngx_event_flags & NGX_USE_AIO_EVENT) {
2003-10-31 00:51:33 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, pc->log, ngx_socket_errno,
"connect(): %d", rc);
2003-10-29 16:30:44 +08:00
/* aio, iocp */
2003-10-31 00:51:33 +08:00
if (ngx_blocking(s) == -1) {
ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
ngx_blocking_n " failed");
return NGX_ERROR;
}
/*
* FreeBSD's aio allows to post an operation on non-connected socket.
2003-11-17 05:49:42 +08:00
* NT does not support it.
*
2003-11-01 00:05:33 +08:00
* TODO: check in Win32, etc. As workaround we can use NGX_ONESHOT_EVENT
2003-10-31 00:51:33 +08:00
*/
2003-10-30 16:51:06 +08:00
rev->ready = 1;
2003-10-29 16:30:44 +08:00
wev->ready = 1;
2003-10-30 16:51:06 +08:00
2003-10-29 16:30:44 +08:00
return NGX_OK;
}
if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
/* kqueue */
2003-10-03 23:50:53 +08:00
event = NGX_CLEAR_EVENT;
} else {
/* select, poll, /dev/poll */
2003-10-03 23:50:53 +08:00
event = NGX_LEVEL_EVENT;
}
2003-10-28 05:01:00 +08:00
if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) {
return NGX_ERROR;
}
2003-10-03 23:50:53 +08:00
if (rc == -1) {
/* NGX_EINPROGRESS */
if (ngx_add_event(wev, NGX_WRITE_EVENT, event) != NGX_OK) {
return NGX_ERROR;
}
return NGX_AGAIN;
}
2004-02-12 01:08:49 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, pc->log, 0, "connected");
2003-11-19 00:49:00 +08:00
2003-10-17 04:19:16 +08:00
wev->ready = 1;
2003-10-02 13:39:37 +08:00
return NGX_OK;
2003-07-21 05:15:59 +08:00
}
2003-08-14 14:00:28 +08:00
void
ngx_event_connect_peer_failed(ngx_peer_connection_t *pc, ngx_uint_t down)
2003-08-14 14:00:28 +08:00
{
2003-10-31 15:10:36 +08:00
time_t now;
if (down) {
now = ngx_time();
2003-10-31 15:10:36 +08:00
/* ngx_lock_mutex(pc->peers->mutex); */
2003-10-31 15:10:36 +08:00
pc->peers->peer[pc->cur_peer].fails++;
pc->peers->peer[pc->cur_peer].accessed = now;
2003-10-31 15:10:36 +08:00
/* ngx_unlock_mutex(pc->peers->mutex); */
}
2003-10-31 15:10:36 +08:00
2003-10-28 00:16:17 +08:00
pc->cur_peer++;
if (pc->cur_peer >= pc->peers->number) {
pc->cur_peer = 0;
}
if (pc->tries) {
pc->tries--;
}
2003-08-14 14:00:28 +08:00
}