66 |
} |
} |
67 |
|
|
68 |
#ifdef ENABLE_SMP |
#ifdef ENABLE_SMP |
69 |
static pthread_mutex_t accept_mutex[2] = { |
static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER; |
|
PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER }; |
|
70 |
#endif |
#endif |
71 |
|
|
72 |
|
static int total_global_connections = 0; |
73 |
|
|
74 |
|
/* Decreases total_global_connections, but does some locking |
75 |
|
* too. |
76 |
|
*/ |
77 |
|
inline |
78 |
|
static void decrease_global_total_connections() |
79 |
|
{ |
80 |
|
/* if we do want to serv as much as possible, then |
81 |
|
* don't bother counting connections. |
82 |
|
*/ |
83 |
|
if (max_connections == INT_MAX) return; |
84 |
|
|
85 |
|
#ifdef ENABLE_SMP |
86 |
|
pthread_mutex_lock( &accept_mutex); |
87 |
|
#endif |
88 |
|
total_global_connections--; |
89 |
|
#ifdef ENABLE_SMP |
90 |
|
pthread_mutex_unlock( &accept_mutex); |
91 |
|
#endif |
92 |
|
|
93 |
|
} |
94 |
|
|
95 |
/* |
/* |
96 |
* Name: get_request |
* Name: get_request |
97 |
* |
* |
119 |
* 0 or 1, and we have 2 mutexes, one for the secure port, |
* 0 or 1, and we have 2 mutexes, one for the secure port, |
120 |
* and one of the normal http port. |
* and one of the normal http port. |
121 |
*/ |
*/ |
122 |
pthread_mutex_lock( &accept_mutex[ server_s->secure]); |
pthread_mutex_lock( &accept_mutex); |
123 |
#endif |
#endif |
|
fd = accept(server_s->socket, (struct sockaddr *) &remote_addr, |
|
|
&remote_addrlen); |
|
124 |
|
|
125 |
#ifdef ENABLE_SMP |
/* If we have reached our max connections limit |
|
/* No dead lock conditions here, since accept() is non blocking. |
|
126 |
*/ |
*/ |
127 |
pthread_mutex_unlock( &accept_mutex[ server_s->secure]); |
if (total_global_connections >= max_connections) { |
128 |
#endif |
server_s->pending_requests = 0; |
129 |
|
goto unlock; |
130 |
|
} |
131 |
|
|
132 |
|
fd = accept(server_s->socket, (struct sockaddr *) &remote_addr, |
133 |
|
&remote_addrlen); |
134 |
|
|
135 |
if (fd == -1) { |
if (fd == -1) { |
136 |
if (errno != EAGAIN && errno != EWOULDBLOCK) |
if (errno != EAGAIN && errno != EWOULDBLOCK) |
139 |
else |
else |
140 |
/* no requests */ |
/* no requests */ |
141 |
server_s->pending_requests = 0; |
server_s->pending_requests = 0; |
142 |
return; |
goto unlock; |
143 |
} |
} |
144 |
|
|
145 |
|
/* only count, if we have enabled a connection limit */ |
146 |
|
if (max_connections != INT_MAX) |
147 |
|
total_global_connections++; |
148 |
|
|
149 |
|
#ifdef ENABLE_SMP |
150 |
|
/* No dead lock conditions here, since accept() is non blocking. |
151 |
|
*/ |
152 |
|
pthread_mutex_unlock( &accept_mutex); |
153 |
|
#endif |
154 |
|
|
155 |
if (fd >= FD_SETSIZE) { |
if (fd >= FD_SETSIZE) { |
156 |
WARN("Got fd >= FD_SETSIZE."); |
WARN("Got fd >= FD_SETSIZE."); |
157 |
close(fd); |
close(fd); |
299 |
} |
} |
300 |
#endif /* TCP_CORK */ |
#endif /* TCP_CORK */ |
301 |
|
|
|
#ifndef NO_RATE_LIMIT |
|
|
/* We use the fact the file descriptors are |
|
|
* assigned sequentialy. |
|
|
*/ |
|
|
#define MAX(x,y) (x>y?x:y) |
|
|
if (conn->fd > (MAX(max_connections - 10, 0))) { |
|
|
send_r_service_unavailable(conn); |
|
|
conn->status = DONE; |
|
|
server_s->pending_requests = 0; |
|
|
} |
|
|
#endif /* NO_RATE_LIMIT */ |
|
|
|
|
302 |
params->total_connections++; |
params->total_connections++; |
303 |
|
|
304 |
enqueue(¶ms->request_ready, conn); |
enqueue(¶ms->request_ready, conn); |
305 |
|
|
306 |
|
return; |
307 |
|
|
308 |
|
unlock: |
309 |
|
#ifdef ENABLE_SMP |
310 |
|
pthread_mutex_unlock( &accept_mutex); |
311 |
|
#endif |
312 |
|
return; |
313 |
} |
} |
314 |
|
|
315 |
|
|
381 |
enqueue(¶ms->request_free, req); |
enqueue(¶ms->request_free, req); |
382 |
close(req->fd); |
close(req->fd); |
383 |
params->total_connections--; |
params->total_connections--; |
384 |
|
decrease_global_total_connections(); |
385 |
return; |
return; |
386 |
} |
} |
387 |
conn->fd = req->fd; |
conn->fd = req->fd; |
395 |
enqueue(¶ms->request_free, req); |
enqueue(¶ms->request_free, req); |
396 |
close(req->fd); |
close(req->fd); |
397 |
params->total_connections--; |
params->total_connections--; |
398 |
|
decrease_global_total_connections(); |
399 |
return; |
return; |
400 |
} |
} |
401 |
|
|
492 |
close(req->fd); |
close(req->fd); |
493 |
|
|
494 |
params->total_connections--; |
params->total_connections--; |
495 |
|
decrease_global_total_connections(); |
496 |
|
|
497 |
enqueue(¶ms->request_free, req); |
enqueue(¶ms->request_free, req); |
498 |
|
|