1 #include "../../uwsgi.h"
2 
3 /*
4 
5 	This plugins is meant as an example for custom implementations.
6 
7         -- Copy of Cheaper,  backlog algorithm (supported only on Linux) --
8 
9         increse the number of workers when the listen queue is higher than uwsgi.cheaper_overload.
10         Decrese when lower.
11 
12 */
13 
14 extern struct uwsgi_server uwsgi;
15 
cheaper_backlog2_algo(int can_spawn)16 int cheaper_backlog2_algo(int can_spawn) {
17 
18         int i;
19 #ifdef __linux__
20         int backlog = uwsgi.socket_timeout;
21 #else
22         int backlog = 0;
23 #endif
24 
25         // if can_spawn == 0 we cannot spawn any new worker
26         // this is set to 1 if --cheaper-rss-limit-* options are used and running workers are exceeding resources limit
27         if (can_spawn && backlog > (int)uwsgi.cheaper_overload) {
28                 // activate the first available worker (taking step into account)
29                 int decheaped = 0;
30                 // search for cheaped workers
31                 for (i = 1; i <= uwsgi.numproc; i++) {
32                         if (uwsgi.workers[i].cheaped == 1 && uwsgi.workers[i].pid == 0) {
33                                 decheaped++;
34                                 if (decheaped >= uwsgi.cheaper_step)
35                                         break;
36                         }
37                 }
38                 // return the maximum number of workers to spawn
39                 return decheaped;
40 
41         }
42         else if (backlog < (int) uwsgi.cheaper_overload) {
43 		// count active workers
44                 int active_workers = 0;
45                 for (i = 1; i <= uwsgi.numproc; i++) {
46                         if (uwsgi.workers[i].cheaped == 0 && uwsgi.workers[i].pid > 0) {
47                                 active_workers++;
48                         }
49                 }
50 
51 		// cheap a worker if too much are running
52                 if (active_workers > uwsgi.cheaper_count) {
53                         return -1;
54                 }
55         }
56 
57         return 0;
58 }
59 
60 
61 
62 // registration hook
uwsgi_cheaper_register_backlog2(void)63 void uwsgi_cheaper_register_backlog2(void) {
64 	uwsgi_register_cheaper_algo("backlog2", cheaper_backlog2_algo);
65 }
66 
67 struct uwsgi_plugin cheaper_backlog2_plugin = {
68 
69 	.name = "cheaper_backlog2",
70         .on_load = uwsgi_cheaper_register_backlog2,
71 
72 };
73