@cryptotaxi247 / netdata-1 / commits / 584dc4cf2

faster multi-threads web server

Costa Tsaousis committed May 29, 2016 at 16:42 UTC 584dc4cf2b8a807ec1f9378b43b7e4b5e9b95c56
2 files changed +36 -16
src/web_client.c
+14 -1
@@ -2191,8 +2191,21 @@ void *web_client_main(void *ptr)
2191 debug(D_WEB_CLIENT, "%llu: done...", w->id);
2192
2193 web_client_reset(w);
2194 +
2195 + // close the sockets/files now
2196 + // to free file descriptors
2197 + if(w->ifd == w->ofd) {
2198 + if(w->ifd != -1) close(w->ifd);
2199 + }
2200 + else {
2201 + if(w->ifd != -1) close(w->ifd);
2202 + if(w->ofd != -1) close(w->ifd);
2203 + }
2204 + w->ifd = -1;
2205 + w->ofd = -1;
2206 +
2207 w->obsolete = 1;
2195 - pthread_exit(NULL);
2208
2209 + pthread_exit(NULL);
2210 return NULL;
2211 }
src/web_server.c
+22 -15
@@ -202,6 +202,8 @@ int create_listen_socket(void) {
202 // 3. spawns a new pthread to serve the client (this is optimal for keep-alive clients)
203 // 4. cleans up old web_clients that their pthreads have been exited
204
205 +#define CLEANUP_EVERY_EVENTS 1000
206 +
207 void *socket_listen_main_multi_threaded(void *ptr) {
208 if(ptr) { ; }
209
@@ -210,7 +212,7 @@ void *socket_listen_main_multi_threaded(void *ptr) {
212
213 struct web_client *w;
214 struct timeval tv;
213 - int retval, failures = 0;
215 + int retval, failures = 0, counter = 0;
216
217 if(ptr) { ; }
218
@@ -227,7 +229,7 @@ void *socket_listen_main_multi_threaded(void *ptr) {
229
230 for(;;) {
231 tv.tv_sec = 0;
230 - tv.tv_usec = 200000;
232 + tv.tv_usec = 100000;
233
234 if(likely(listen_fd >= 0))
235 FD_SET(listen_fd, &ifds);
@@ -274,25 +276,30 @@ void *socket_listen_main_multi_threaded(void *ptr) {
276 else debug(D_WEB_CLIENT, "LISTENER: select() didn't do anything.");
277
278 }
277 - //else {
278 - // debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
279 - //}
280 -
281 - failures = 0;
279 + else {
280 + debug(D_WEB_CLIENT, "LISTENER: select() timeout.");
281 + counter = CLEANUP_EVERY_EVENTS;
282 + }
283
284 // cleanup unused clients
284 - for (w = web_clients; w; ) {
285 - if (w->obsolete) {
286 - debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
287 - // pthread_cancel(w->thread);
288 - // pthread_join(w->thread, NULL);
289 - w = web_client_free(w);
285 + counter++;
286 + if(counter > CLEANUP_EVERY_EVENTS) {
287 + counter = 0;
288 + for (w = web_clients; w;) {
289 + if (w->obsolete) {
290 + debug(D_WEB_CLIENT, "%llu: Removing client.", w->id);
291 + // pthread_cancel(w->thread);
292 + // pthread_join(w->thread, NULL);
293 + w = web_client_free(w);
294 #ifdef NETDATA_INTERNAL_CHECKS
291 - log_allocations();
295 + log_allocations();
296 #endif
297 + }
298 + else w = w->next;
299 }
294 - else w = w->next;
300 }
301 +
302 + failures = 0;
303 }
304
305 error("LISTENER: exit!");