debug(D_WEB_CLIENT, "%llu: Reseting client.", w->id);
if(w->stats_received_bytes || w->stats_sent_bytes) {
- global_statistics_lock();
+ if(web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
+ global_statistics_lock();
+
global_statistics.web_requests++;
global_statistics.web_usec += usecdiff(&tv, &w->tv_in);
global_statistics.bytes_received += w->stats_received_bytes;
global_statistics.bytes_sent += w->stats_sent_bytes;
- global_statistics_unlock();
+
+ if(web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
+ global_statistics_unlock();
}
w->stats_received_bytes = 0;
w->stats_sent_bytes = 0;
return t;
}
- if(unlikely(w->keepalive == 0)) {
+ if(unlikely(!w->keepalive)) {
debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %ld bytes sent.", w->id, w->response.sent);
- errno = 0;
- return(-1);
+ WEB_CLIENT_IS_DEAD(w);
+ return t;
}
// reset the client
// we have to wait, more data will come
debug(D_WEB_CLIENT, "%llu: Waiting for more data to become available.", w->id);
w->wait_send = 0;
- return(0);
+ return 0;
}
- if(unlikely(w->keepalive == 0)) {
+ if(unlikely(!w->keepalive)) {
debug(D_WEB_CLIENT, "%llu: Closing (keep-alive is not enabled). %ld bytes sent.", w->id, w->response.sent);
- errno = 0;
- return(-1);
+ WEB_CLIENT_IS_DEAD(w);
+ return 0;
}
web_client_reset(w);
debug(D_WEB_CLIENT, "%llu: Done sending all data on socket. Waiting for next request on the same socket.", w->id);
- return(0);
+ return 0;
}
bytes = send(w->ofd, &w->response.data->buffer[w->response.sent], w->response.data->len - w->response.sent, MSG_DONTWAIT);
for(;;) {
if(unlikely(w->dead)) {
- error("%llu: client is dead.");
+ error("%llu: client is dead.", w->id);
break;
}
else if(unlikely(!w->wait_receive && !w->wait_send)) {
FD_ZERO (&ofds);
FD_ZERO (&efds);
+ if(w->ifd < 0 || w->ifd >= FD_SETSIZE || w->ofd < 0 || w->ofd >= FD_SETSIZE) {
+ error("%llu: invalid file descriptor, ifd = %d, ofd = %d (required 0 <= fd < FD_SETSIZE (%d)", w->id, w->ifd, w->ofd, FD_SETSIZE);
+ break;
+ }
+
FD_SET(w->ifd, &efds);
if(w->ifd != w->ofd)
}
if(w->wait_send && FD_ISSET(w->ofd, &ofds)) {
- ssize_t bytes;
- if((bytes = web_client_send(w)) < 0) {
+ if(web_client_send(w) < 0) {
debug(D_WEB_CLIENT, "%llu: Cannot send data to client. Closing client.", w->id);
- errno = 0;
break;
}
}
if(w->wait_receive && FD_ISSET(w->ifd, &ifds)) {
- ssize_t bytes;
- if((bytes = web_client_receive(w)) < 0) {
+ if(web_client_receive(w) < 0) {
debug(D_WEB_CLIENT, "%llu: Cannot receive data from client. Closing client.", w->id);
- errno = 0;
break;
}
if(w->mode == WEB_CLIENT_MODE_NORMAL) {
- debug(D_WEB_CLIENT, "%llu: Attempting to process received data (%ld bytes).", w->id, bytes);
- // info("%llu: Attempting to process received data (%ld bytes).", w->id, bytes);
+ debug(D_WEB_CLIENT, "%llu: Attempting to process received data.", w->id);
web_client_process(w);
}
-
}
}
#include "../config.h"
int listen_backlog = LISTEN_BACKLOG;
-
int listen_fd = -1;
int listen_port = LISTEN_PORT;
+int web_server_mode = WEB_SERVER_MODE_MULTI_THREADED;
#ifdef NETDATA_INTERNAL_CHECKS
static void log_allocations(void)
void *socket_listen_main_multi_threaded(void *ptr) {
if(ptr) { ; }
+ web_server_mode = WEB_SERVER_MODE_MULTI_THREADED;
info("Multi-threaded WEB SERVER thread created with task id %d", gettid());
struct web_client *w;
void *socket_listen_main_single_threaded(void *ptr) {
if(ptr) { ; }
+ web_server_mode = WEB_SERVER_MODE_SINGLE_THREADED;
+
info("Single threaded WEB SERVER thread created with task id %d", gettid());
struct web_client *w;
for(w = web_clients; w ; w = w->next) {
if(unlikely(w->dead)) {
- error("%llu: client is dead.");
+ error("%llu: client is dead.", w->id);
w->obsolete = 1;
}
else if(unlikely(!w->wait_receive && !w->wait_send)) {
w->obsolete = 1;
}
+ if(w->ifd < 0 || w->ifd >= FD_SETSIZE || w->ofd < 0 || w->ofd >= FD_SETSIZE) {
+ error("%llu: invalid file descriptor, ifd = %d, ofd = %d (required 0 <= fd < FD_SETSIZE (%d)", w->id, w->ifd, w->ofd, FD_SETSIZE);
+ w->obsolete = 1;
+ }
+
if(unlikely(w->obsolete)) {
has_obsolete++;
continue;