main.c 3.2 KB

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