2003-05-12 23:52:24 +08:00
|
|
|
|
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
|
|
|
|
2003-07-23 21:10:12 +08:00
|
|
|
/* AF_INET only */
|
2003-05-12 23:52:24 +08:00
|
|
|
|
2003-08-01 22:56:33 +08:00
|
|
|
int ngx_event_connect_peer(ngx_peer_connection_t *pc)
|
2003-05-12 23:52:24 +08:00
|
|
|
{
|
2003-10-03 23:50:53 +08:00
|
|
|
int rc, instance, event;
|
2003-07-23 21:10:12 +08:00
|
|
|
time_t now;
|
2003-10-02 13:39:37 +08:00
|
|
|
ngx_err_t err;
|
|
|
|
ngx_peer_t *peer;
|
2003-07-23 21:10:12 +08:00
|
|
|
ngx_socket_t s;
|
2003-10-02 13:39:37 +08:00
|
|
|
ngx_event_t *rev, *wev;
|
|
|
|
ngx_connection_t *c;
|
|
|
|
struct sockaddr_in addr;
|
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 */
|
|
|
|
|
2003-08-14 14:00:28 +08:00
|
|
|
pc->connection = 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); */
|
|
|
|
|
|
|
|
pc->cached = 1;
|
|
|
|
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-02 13:39:37 +08:00
|
|
|
peer = &pc->peers->peers[0];
|
|
|
|
|
2003-07-22 00:24:25 +08:00
|
|
|
if (pc->peers->number > 1) {
|
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 */
|
|
|
|
|
2003-07-22 00:24:25 +08:00
|
|
|
pc->cur_peer = 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-22 00:24:25 +08:00
|
|
|
if (pc->peers->max_fails > 0) {
|
2003-05-12 23:52:24 +08:00
|
|
|
|
2003-07-21 05:15:59 +08:00
|
|
|
/* the peers support a fault tolerance */
|
|
|
|
|
2003-05-12 23:52:24 +08:00
|
|
|
for ( ;; ) {
|
2003-07-22 00:24:25 +08:00
|
|
|
peer = &pc->peers->peers[pc->cur_peer];
|
2003-05-12 23:52:24 +08:00
|
|
|
|
2003-07-22 00:24:25 +08:00
|
|
|
if (peer->fails <= pc->peers->max_fails
|
|
|
|
|| (now - peer->accessed > pc->peers->fail_timeout))
|
2003-05-12 23:52:24 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-07-22 00:24:25 +08:00
|
|
|
pc->cur_peer++;
|
2003-05-12 23:52:24 +08:00
|
|
|
|
2003-07-22 00:24:25 +08:00
|
|
|
if (pc->cur_peer >= pc->peers->number) {
|
|
|
|
pc->cur_peer = 0;
|
2003-05-12 23:52:24 +08:00
|
|
|
}
|
|
|
|
|
2003-07-22 00:24:25 +08:00
|
|
|
pc->tries--;
|
2003-05-12 23:52:24 +08:00
|
|
|
|
2003-07-22 00:24:25 +08:00
|
|
|
if (pc->tries == 0) {
|
2003-07-23 21:10:12 +08:00
|
|
|
/* ngx_unlock_mutex(pc->peers->mutex); */
|
2003-10-02 13:39:37 +08:00
|
|
|
|
2003-05-12 23:52:24 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-23 21:10:12 +08:00
|
|
|
/* ngx_unlock_mutex(pc->peers->mutex); */
|
|
|
|
|
2003-10-02 13:39:37 +08:00
|
|
|
#if 0
|
2003-07-23 03:53:10 +08:00
|
|
|
pc->addr_port_text = peer->addr_port_text;
|
2003-10-02 13:39:37 +08:00
|
|
|
#endif
|
2003-05-12 23:52:24 +08:00
|
|
|
|
|
|
|
s = ngx_socket(AF_INET, SOCK_STREAM, IPPROTO_IP, 0);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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,
|
2003-07-23 03:53:10 +08:00
|
|
|
(const void *) &pc->rcvbuf, sizeof(int)) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
|
2003-05-12 23:52:24 +08:00
|
|
|
"setsockopt(SO_RCVBUF) failed");
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2003-07-21 05:15:59 +08:00
|
|
|
#if (WIN32)
|
2003-07-23 03:53:10 +08:00
|
|
|
/*
|
|
|
|
* Winsock assignes a socket number divisible by 4
|
|
|
|
* so to find a connection we divide a socket number by 4.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (s % 4) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, pc->log, 0,
|
|
|
|
ngx_socket_n
|
|
|
|
" created socket %d, not divisible by 4", s);
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-07-21 05:15:59 +08:00
|
|
|
|
|
|
|
c = &ngx_cycle->connections[s / 4];
|
|
|
|
rev = &ngx_cycle->read_events[s / 4];
|
|
|
|
wev = &ngx_cycle->write_events[s / 4];
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
c = &ngx_cycle->connections[s];
|
|
|
|
rev = &ngx_cycle->read_events[s];
|
|
|
|
wev = &ngx_cycle->write_events[s];
|
|
|
|
|
|
|
|
#endif
|
2003-05-12 23:52:24 +08:00
|
|
|
|
|
|
|
instance = rev->instance;
|
|
|
|
|
|
|
|
ngx_memzero(c, sizeof(ngx_connection_t));
|
|
|
|
ngx_memzero(rev, sizeof(ngx_event_t));
|
|
|
|
ngx_memzero(wev, sizeof(ngx_event_t));
|
|
|
|
|
|
|
|
rev->index = wev->index = NGX_INVALID_INDEX;
|
|
|
|
rev->data = wev->data = c;
|
|
|
|
c->read = rev;
|
|
|
|
c->write = wev;
|
|
|
|
|
|
|
|
rev->instance = wev->instance = !instance;
|
|
|
|
|
2003-07-23 03:53:10 +08:00
|
|
|
rev->log = wev->log = c->log = pc->log;
|
2003-05-12 23:52:24 +08:00
|
|
|
c->fd = s;
|
2003-07-23 03:53:10 +08:00
|
|
|
|
|
|
|
pc->connection = c;
|
|
|
|
|
|
|
|
if (ngx_add_conn) {
|
|
|
|
if (ngx_add_conn(c) == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-03 23:50:53 +08:00
|
|
|
ngx_memzero(&addr, sizeof(struct sockaddr_in));
|
|
|
|
|
2003-10-02 13:39:37 +08:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = peer->addr;
|
2003-10-03 23:50:53 +08:00
|
|
|
addr.sin_port = peer->port;
|
2003-07-23 21:10:12 +08:00
|
|
|
|
2003-10-02 13:39:37 +08:00
|
|
|
rc = connect(s, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
|
2003-07-23 21:10:12 +08:00
|
|
|
|
|
|
|
if (rc == -1) {
|
|
|
|
err = ngx_socket_errno;
|
|
|
|
if (err != NGX_EINPROGRESS) {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, pc->log, err, "connect() failed");
|
|
|
|
|
|
|
|
if (ngx_close_socket(s) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
|
|
|
|
ngx_close_socket_n " failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-03 23:50:53 +08:00
|
|
|
/* TODO: epoll, aio, iocp */
|
|
|
|
|
|
|
|
if (ngx_event_flags & NGX_HAVE_CLEAR_EVENT) { /* kqueue */
|
|
|
|
event = NGX_CLEAR_EVENT;
|
|
|
|
|
|
|
|
} else { /* select, poll, /dev/poll */
|
|
|
|
event = NGX_LEVEL_EVENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == -1) {
|
|
|
|
|
|
|
|
/* NGX_EINPROGRESS */
|
|
|
|
|
|
|
|
if (ngx_add_event(wev, NGX_WRITE_EVENT, event) != NGX_OK) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2003-10-03 23:50:53 +08:00
|
|
|
pc->tries--;
|
|
|
|
|
2003-08-14 14:00:28 +08:00
|
|
|
return;
|
|
|
|
}
|