Browse Source

Modification of workers making them always exist busy

Adds a global flag for worker indicating their status (add checks for
each semaphore change)
The pool handler will no longer have to decrease the semaphore value when
a child exit.
Yann Weber 5 years ago
parent
commit
5466d9c575
1 changed files with 23 additions and 6 deletions
  1. 23
    6
      src/pyworker.c

+ 23
- 6
src/pyworker.c View File

20
 #include "pyworker.h"
20
 #include "pyworker.h"
21
 
21
 
22
 
22
 
23
+/**@brief 1 if worker idle else 0 */
24
+static short _worker_idle;
23
 /**@brief Indicate that a worker is idle */
25
 /**@brief Indicate that a worker is idle */
24
 static inline void worker_set_idle();
26
 static inline void worker_set_idle();
25
 /**@brief Indicate that a worker is busy */
27
 /**@brief Indicate that a worker is busy */
61
 	pyfcgi_log(LOG_INFO, "Waiting request with %s.%s()",
63
 	pyfcgi_log(LOG_INFO, "Waiting request with %s.%s()",
62
 		PyFCGI_conf.py_entrymod, PyFCGI_conf.py_entryfun);
64
 		PyFCGI_conf.py_entrymod, PyFCGI_conf.py_entryfun);
63
 
65
 
64
-	worker_set_idle(); //before failing on import
65
-
66
 	if(!entry_fun) //but exit if import failed
66
 	if(!entry_fun) //but exit if import failed
67
 	{
67
 	{
68
 		pyfcgi_log(LOG_ALERT, "Unable to import entrypoint");
68
 		pyfcgi_log(LOG_ALERT, "Unable to import entrypoint");
69
 		exit(PYFCGI_FATAL);
69
 		exit(PYFCGI_FATAL);
70
-
71
 	}
70
 	}
72
 
71
 
73
 	start_response = get_start_response();
72
 	start_response = get_start_response();
74
 
73
 
74
+	_worker_idle = 0;
75
+	worker_set_idle();
75
 	// requests accepting loop
76
 	// requests accepting loop
76
 	count = 0;
77
 	count = 0;
77
 	while ((!count || count != max_reqs) &&
78
 	while ((!count || count != max_reqs) &&
121
 		FCGX_FClose(in_stream);
122
 		FCGX_FClose(in_stream);
122
 		FCGX_FClose(err_stream);
123
 		FCGX_FClose(err_stream);
123
 		FCGI_Finish();
124
 		FCGI_Finish();
124
-		worker_set_idle();
125
+
125
 		gettimeofday(&stop, NULL);
126
 		gettimeofday(&stop, NULL);
126
 		stop.tv_sec = stop.tv_sec - start.tv_sec;
127
 		stop.tv_sec = stop.tv_sec - start.tv_sec;
127
 		stop.tv_usec = stop.tv_usec - start.tv_usec;
128
 		stop.tv_usec = stop.tv_usec - start.tv_usec;
133
 		pyfcgi_wd_pause();
134
 		pyfcgi_wd_pause();
134
 		pyfcgi_log(LOG_DEBUG, "Worker[%d] request %d END [OK] %lu bytes in %ld.%06lds",
135
 		pyfcgi_log(LOG_DEBUG, "Worker[%d] request %d END [OK] %lu bytes in %ld.%06lds",
135
 			wrk_id, count, libpyfcgi.rep_sz, stop.tv_sec, stop.tv_usec);
136
 			wrk_id, count, libpyfcgi.rep_sz, stop.tv_sec, stop.tv_usec);
137
+
138
+		worker_set_idle();
136
 	}
139
 	}
140
+	worker_set_busy();
137
 	return 0;
141
 	return 0;
138
 }
142
 }
139
 
143
 
207
 		PyFCGI_conf.py_entrymod, PyFCGI_conf.py_entryfun);
211
 		PyFCGI_conf.py_entrymod, PyFCGI_conf.py_entryfun);
208
 
212
 
209
 
213
 
210
-	worker_set_idle(); //before failing on import
211
 
214
 
212
 	if(!entry_fun) //but exit if import failed
215
 	if(!entry_fun) //but exit if import failed
213
 	{
216
 	{
226
 	}
229
 	}
227
 	piper_args.ctl_pipe = pipe_ctl[1];
230
 	piper_args.ctl_pipe = pipe_ctl[1];
228
 
231
 
232
+	_worker_idle = 0;
233
+	worker_set_idle();
234
+
229
 	count = 0;
235
 	count = 0;
230
 	while ((!count || count != max_reqs) && FCGX_Accept(&in_stream, &out_stream, &err_stream, &envp) >= 0)
236
 	while ((!count || count != max_reqs) && FCGX_Accept(&in_stream, &out_stream, &err_stream, &envp) >= 0)
231
 	{
237
 	{
310
 		pyfcgi_log(LOG_DEBUG, "Worker[%d] request %d END [OK] %lu bytes in %ld.%06lds",
316
 		pyfcgi_log(LOG_DEBUG, "Worker[%d] request %d END [OK] %lu bytes in %ld.%06lds",
311
 			wrk_id, count, rep_sz, stop.tv_sec, stop.tv_usec);
317
 			wrk_id, count, rep_sz, stop.tv_sec, stop.tv_usec);
312
 	}
318
 	}
319
+	worker_set_busy();
313
 	free(piper_stack);
320
 	free(piper_stack);
314
 	Py_Exit(count == max_reqs ?0:EXIT_PYERR);
321
 	Py_Exit(count == max_reqs ?0:EXIT_PYERR);
315
 }
322
 }
572
 	int err;
579
 	int err;
573
 	struct sembuf sop;
580
 	struct sembuf sop;
574
 
581
 
582
+	if(_worker_idle) { return; }
583
+
575
 	sop.sem_num = 0;
584
 	sop.sem_num = 0;
576
 	sop.sem_flg = 0;
585
 	sop.sem_flg = 0;
577
 	sop.sem_op = 1;
586
 	sop.sem_op = 1;
581
 		err = errno;
590
 		err = errno;
582
 		pyfcgi_log(LOG_ERR, "error incrementing the semaphore : %s",
591
 		pyfcgi_log(LOG_ERR, "error incrementing the semaphore : %s",
583
 			strerror(err));
592
 			strerror(err));
593
+		return;
584
 	}
594
 	}
595
+	_worker_idle = 1;
585
 }
596
 }
586
 
597
 
587
 static void worker_set_busy()
598
 static void worker_set_busy()
589
 	int err;
600
 	int err;
590
 	struct sembuf sop;
601
 	struct sembuf sop;
591
 
602
 
603
+	if(!_worker_idle) { return; }
604
+	
592
 	sop.sem_num = 0;
605
 	sop.sem_num = 0;
593
 	sop.sem_flg = 0;
606
 	sop.sem_flg = 0;
594
 	sop.sem_op = -1;
607
 	sop.sem_op = -1;
598
 		err = errno;
611
 		err = errno;
599
 		pyfcgi_log(LOG_ERR, "error incrementing the semaphore : %s",
612
 		pyfcgi_log(LOG_ERR, "error incrementing the semaphore : %s",
600
 			strerror(err));
613
 			strerror(err));
614
+		return;
601
 	}
615
 	}
616
+	_worker_idle = 0;
602
 }
617
 }
603
 
618
 
604
 void worker_sighandler(int signum)
619
 void worker_sighandler(int signum)
605
 {
620
 {
606
 	pyfcgi_log(LOG_INFO, "%s signal received, exiting...", strsignal(signum));
621
 	pyfcgi_log(LOG_INFO, "%s signal received, exiting...", strsignal(signum));
622
+	worker_set_busy();
607
 	exit(0);
623
 	exit(0);
608
 }
624
 }
609
 
625
 
610
 void worker_sigalrmhandler(int signum)
626
 void worker_sigalrmhandler(int signum)
611
 {
627
 {
612
-	pyfcgi_log(LOG_WARNING, "Timeout, exiting...");
628
+	//pyfcgi_log(LOG_WARNING, "Timeout, exiting...");
629
+	worker_set_busy();
613
 	exit(PYFCGI_TIMEOUT);
630
 	exit(PYFCGI_TIMEOUT);
614
 }
631
 }

Loading…
Cancel
Save