comparison src/event/ngx_event_connect.c @ 122:cd91e4a1ad0d

nginx-0.0.1-2003-07-23-17:10:12 import
author Igor Sysoev <igor@sysoev.ru>
date Wed, 23 Jul 2003 13:10:12 +0000
parents b3655b21375a
children 842a78cebbb7
comparison
equal deleted inserted replaced
121:b3655b21375a 122:cd91e4a1ad0d
1 1
2 #include <ngx_event_connect.h> 2 #include <ngx_event_connect.h>
3 3
4 /* AF_INET only */
4 5
5 int ngx_event_connect_peer(ngx_peer_connecttion_t *pc) 6 int ngx_event_connect_peer(ngx_peer_connecttion_t *pc)
6 { 7 {
7 time_t now; 8 time_t now;
8 ngx_socket_t s; 9 ngx_peer_r *peer;
10 ngx_socket_t s;
11 struct sockaddr_in *addr;
12
13
14 now = ngx_time();
9 15
10 /* ngx_lock_mutex(pc->peers->mutex); */ 16 /* ngx_lock_mutex(pc->peers->mutex); */
11 17
12 if (pc->peers->last_cached) { 18 if (pc->peers->last_cached) {
13 19
20 26
21 pc->cached = 1; 27 pc->cached = 1;
22 return NGX_OK; 28 return NGX_OK;
23 } 29 }
24 30
25 /* ngx_unlock_mutex(pc->peers->mutex); */
26
27 pc->cached = 0; 31 pc->cached = 0;
28
29 now = ngx_time();
30 32
31 if (pc->peers->number > 1) { 33 if (pc->peers->number > 1) {
32 34
33 /* there are several peers */ 35 /* there are several peers */
34 36
35 if (pc->tries == pc->peers->number) { 37 if (pc->tries == pc->peers->number) {
36 38
37 /* it's a first try - get a current peer */ 39 /* it's a first try - get a current peer */
38 40
39 /* Here is the race condition when the peers are shared between
40 the threads or the processes but it should not be serious */
41
42 pc->cur_peer = pc->peers->current++; 41 pc->cur_peer = pc->peers->current++;
43 42
44 if (cp->peers->current >= cp->peers->number) { 43 if (cp->peers->current >= cp->peers->number) {
45 pc->peers->current = 0; 44 pc->peers->current = 0;
46 } 45 }
47
48 /* the end of the race condition */
49
50 #if (NGX_MULTITHREADED || NGX_MULTIPROCESSED)
51 /* eliminate the sequences of the race condition */
52
53 if (pc->cur_peer >= pc->peers->number) {
54 pc->cur_peer = 0;
55 }
56 #endif
57 } 46 }
58 47
59 if (pc->peers->max_fails > 0) { 48 if (pc->peers->max_fails > 0) {
60 49
61 /* the peers support a fault tolerance */ 50 /* the peers support a fault tolerance */
62 51
63 for ( ;; ) { 52 for ( ;; ) {
64 peer = &pc->peers->peers[pc->cur_peer]; 53 peer = &pc->peers->peers[pc->cur_peer];
65 54
66 /* Here is the race condition when the peers are shared between
67 the threads or the processes but it should not be serious */
68
69 if (peer->fails <= pc->peers->max_fails 55 if (peer->fails <= pc->peers->max_fails
70 || (now - peer->accessed > pc->peers->fail_timeout)) 56 || (now - peer->accessed > pc->peers->fail_timeout))
71 { 57 {
72 break; 58 break;
73 } 59 }
74
75 /* the end of the race condition */
76 60
77 pc->cur_peer++; 61 pc->cur_peer++;
78 62
79 if (pc->cur_peer >= pc->peers->number) { 63 if (pc->cur_peer >= pc->peers->number) {
80 pc->cur_peer = 0; 64 pc->cur_peer = 0;
81 } 65 }
82 66
83 pc->tries--; 67 pc->tries--;
84 68
85 if (pc->tries == 0) { 69 if (pc->tries == 0) {
70 /* ngx_unlock_mutex(pc->peers->mutex); */
86 return NGX_ERROR; 71 return NGX_ERROR;
87 } 72 }
88 } 73 }
89 } 74 }
90 } 75 }
76
77 /* ngx_unlock_mutex(pc->peers->mutex); */
91 78
92 pc->addr_port_text = peer->addr_port_text; 79 pc->addr_port_text = peer->addr_port_text;
93 80
94 s = ngx_socket(AF_INET, SOCK_STREAM, IPPROTO_IP, 0); 81 s = ngx_socket(AF_INET, SOCK_STREAM, IPPROTO_IP, 0);
95 82
173 if (ngx_add_conn(c) == NGX_ERROR) { 160 if (ngx_add_conn(c) == NGX_ERROR) {
174 return NGX_ERROR; 161 return NGX_ERROR;
175 } 162 }
176 } 163 }
177 164
165 addr = p->sockaddr;
166
167 addr->sin_family = AF_INET;
168 addr->sin_addr.s_addr = peer->addr;
169 addr->sin_port = htons(peer->port);
170
171 rc = connect(s, p->sockaddr, sizeof(struct sockaddr_in));
172
173 if (rc == -1) {
174 err = ngx_socket_errno;
175 if (err != NGX_EINPROGRESS) {
176 ngx_log_error(NGX_LOG_CRIT, pc->log, err, "connect() failed");
177
178 if (ngx_close_socket(s) == -1) {
179 ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
180 ngx_close_socket_n " failed");
181 }
182
183 return NGX_CONNECT_ERROR;
184 }
185 }
186
187 c->data = ???;
188
189
178 } 190 }