• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <inttypes.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <uv.h>
6 
7 uv_loop_t *loop;
8 
9 struct child_worker {
10     uv_process_t req;
11     uv_process_options_t options;
12     uv_pipe_t pipe;
13 } *workers;
14 
15 int round_robin_counter;
16 int child_worker_count;
17 
18 uv_buf_t dummy_buf;
19 char worker_path[500];
20 
close_process_handle(uv_process_t * req,int64_t exit_status,int term_signal)21 void close_process_handle(uv_process_t *req, int64_t exit_status, int term_signal) {
22     fprintf(stderr, "Process exited with status %" PRId64 ", signal %d\n", exit_status, term_signal);
23     uv_close((uv_handle_t*) req, NULL);
24 }
25 
alloc_buffer(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)26 void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
27   buf->base = malloc(suggested_size);
28   buf->len = suggested_size;
29 }
30 
on_new_connection(uv_stream_t * server,int status)31 void on_new_connection(uv_stream_t *server, int status) {
32     if (status == -1) {
33         // error!
34         return;
35     }
36 
37     uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
38     uv_tcp_init(loop, client);
39     if (uv_accept(server, (uv_stream_t*) client) == 0) {
40         uv_write_t *write_req = (uv_write_t*) malloc(sizeof(uv_write_t));
41         dummy_buf = uv_buf_init("a", 1);
42         struct child_worker *worker = &workers[round_robin_counter];
43         uv_write2(write_req, (uv_stream_t*) &worker->pipe, &dummy_buf, 1, (uv_stream_t*) client, NULL);
44         round_robin_counter = (round_robin_counter + 1) % child_worker_count;
45     }
46     else {
47         uv_close((uv_handle_t*) client, NULL);
48     }
49 }
50 
setup_workers()51 void setup_workers() {
52     size_t path_size = 500;
53     uv_exepath(worker_path, &path_size);
54     strcpy(worker_path + (strlen(worker_path) - strlen("multi-echo-server")), "worker");
55     fprintf(stderr, "Worker path: %s\n", worker_path);
56 
57     char* args[2];
58     args[0] = worker_path;
59     args[1] = NULL;
60 
61     round_robin_counter = 0;
62 
63     // ...
64 
65     // launch same number of workers as number of CPUs
66     uv_cpu_info_t *info;
67     int cpu_count;
68     uv_cpu_info(&info, &cpu_count);
69     uv_free_cpu_info(info, cpu_count);
70 
71     child_worker_count = cpu_count;
72 
73     workers = calloc(cpu_count, sizeof(struct child_worker));
74     while (cpu_count--) {
75         struct child_worker *worker = &workers[cpu_count];
76         uv_pipe_init(loop, &worker->pipe, 1);
77 
78         uv_stdio_container_t child_stdio[3];
79         child_stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
80         child_stdio[0].data.stream = (uv_stream_t*) &worker->pipe;
81         child_stdio[1].flags = UV_IGNORE;
82         child_stdio[2].flags = UV_INHERIT_FD;
83         child_stdio[2].data.fd = 2;
84 
85         worker->options.stdio = child_stdio;
86         worker->options.stdio_count = 3;
87 
88         worker->options.exit_cb = close_process_handle;
89         worker->options.file = args[0];
90         worker->options.args = args;
91 
92         uv_spawn(loop, &worker->req, &worker->options);
93         fprintf(stderr, "Started worker %d\n", worker->req.pid);
94     }
95 }
96 
main()97 int main() {
98     loop = uv_default_loop();
99 
100     setup_workers();
101 
102     uv_tcp_t server;
103     uv_tcp_init(loop, &server);
104 
105     struct sockaddr_in bind_addr;
106     uv_ip4_addr("0.0.0.0", 7000, &bind_addr);
107     uv_tcp_bind(&server, (const struct sockaddr *)&bind_addr, 0);
108     int r;
109     if ((r = uv_listen((uv_stream_t*) &server, 128, on_new_connection))) {
110         fprintf(stderr, "Listen error %s\n", uv_err_name(r));
111         return 2;
112     }
113     return uv_run(loop, UV_RUN_DEFAULT);
114 }
115