注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

记录创意的火花

天天学习,好好向上

 
 
 

日志

 
 

libuv 编写带负载均衡的多进程echo-server(源码)(二)  

2013-11-01 20:04:50|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
///////////////////////////////echo-uv.c

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include "uv.h"
#include "task.h"
#include <string.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "psapi.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "libuv.lib")




uv_loop_t *loop;

struct child_worker {
   uv_process_t req;
   uv_process_options_t options;
   uv_pipe_t pipe;
  
} *workers;

int round_robin_counter;
int child_worker_count;

uv_buf_t dummy_buf;
char worker_path[500];

void close_process_handle(uv_process_t *req, int exit_status, int term_signal) {
   fprintf(stderr, "Process exited with status %d, signal %d,pid  %d\n", exit_status, term_signal,req->pid);

   for(int i=0;i<child_worker_count;i++){
    if(workers[i].req.pid==req->pid){
uv_close((uv_handle_t*) req, NULL);
uv_close((uv_handle_t*)&workers[i].pipe,NULL);
   
  
  ///////////////////////////////////restart 
  
   memset(&workers[i],0,sizeof(struct child_worker));
size_t path_size = 500;
   uv_exepath(worker_path, &path_size);
   strcpy(worker_path + (strlen(worker_path) - strlen("echo-uv.exe")), "worker.exe");
 
   char* args[2];
   args[0] = worker_path;
   args[1] = NULL;
 uv_pipe_init(loop, &workers[i].pipe, 1);
  uv_stdio_container_t child_stdio[3];
      child_stdio[0].flags =(uv_stdio_flags)( UV_CREATE_PIPE | UV_READABLE_PIPE);
      child_stdio[0].data.stream = (uv_stream_t*) &workers[i].pipe;
      child_stdio[1].flags = UV_IGNORE;
      child_stdio[2].flags = UV_INHERIT_FD;
      child_stdio[2].data.fd = 2;

      workers[i].options.stdio = child_stdio;
      workers[i].options.stdio_count = 3;

      workers[i].options.exit_cb = close_process_handle;
      workers[i].options.file = args[0];
      workers[i].options.args = args;

     uv_spawn(loop, &workers[i].req, workers[i].options); 
     fprintf(stderr, "Restarted worker %d\n", workers[i].req.pid);
     
   }
   }
}

uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size) {
   return uv_buf_init((char*) calloc(suggested_size, 1), suggested_size);
}
static void write2_cb(uv_write_t* req, int status) {
  ASSERT(status == 0);
 
  free(req);
}

void on_new_connection(uv_stream_t *server, int status) {
   if (status == -1) {
      // error!
      return;
   }

   uv_tcp_t client ;
   uv_tcp_init(loop, &client);
   if (uv_accept(server, (uv_stream_t*) &client) == 0) {
      uv_write_t *write_req = (uv_write_t*) malloc(sizeof(uv_write_t));
      dummy_buf = uv_buf_init(".", 1);
      struct child_worker *worker = &workers[round_robin_counter];
     uv_write2(write_req, (uv_stream_t*) &worker->pipe, &dummy_buf, 1, (uv_stream_t*) &client, write2_cb);
      round_robin_counter = (round_robin_counter + 1) % child_worker_count;//负载均衡
     
 
   }
   else {
      uv_close((uv_handle_t*) &client, NULL);
   }
}

void setup_workers() {
   size_t path_size = 500;
   uv_exepath(worker_path, &path_size);
   strcpy(worker_path + (strlen(worker_path) - strlen("echo-uv.exe")), "worker.exe");
   fprintf(stderr, "Worker path: %s\n", worker_path);

   char* args[2];
   args[0] = worker_path;
   args[1] = NULL;

   round_robin_counter = 0;

   // ...

   // launch same number of workers as number of CPUs
   uv_cpu_info_t *info;
   int cpu_count;
   uv_cpu_info(&info, &cpu_count);
   uv_free_cpu_info(info, cpu_count);

   child_worker_count = cpu_count;

   workers = (child_worker *)calloc(sizeof(struct child_worker), cpu_count);
   while (cpu_count--) {
      struct child_worker *worker = &workers[cpu_count];
      uv_pipe_init(loop, &worker->pipe, 1);

      uv_stdio_container_t child_stdio[3];
      child_stdio[0].flags =(uv_stdio_flags)( UV_CREATE_PIPE | UV_READABLE_PIPE);
      child_stdio[0].data.stream = (uv_stream_t*) &worker->pipe;
      child_stdio[1].flags = UV_IGNORE;
      child_stdio[2].flags = UV_INHERIT_FD;
      child_stdio[2].data.fd = 2;

      worker->options.stdio = child_stdio;
      worker->options.stdio_count = 3;

      worker->options.exit_cb = close_process_handle;
      worker->options.file = args[0];
      worker->options.args = args;
 
      uv_spawn(loop, &worker->req, worker->options); 
      fprintf(stderr, "Started worker %d\n", worker->req.pid);

   }
}

int main() {
   loop = uv_default_loop();

   setup_workers();

   uv_tcp_t server;
   uv_tcp_init(loop, &server);

   struct sockaddr_in bind_addr = uv_ip4_addr("0.0.0.0", 5555);
   uv_tcp_bind(&server, bind_addr);
   if (uv_listen((uv_stream_t*) &server, 128, on_new_connection)) {
      fprintf(stderr, "Listen error %s\n", uv_err_name(uv_last_error(loop)));
      return 2;
   }
    uv_run(loop, UV_RUN_DEFAULT);
if(workers!=NULL)
 free(workers);
}
/////////////////////////////////////////////////////////////
与第一版相比的改进:
1,增加了子进程管理,在子进程有退出时自动重启一个。
2,增加了write2_cb函数,修正了内存泄漏问题。
3,本版稳定性有很大提高,后续可长期使用。
  评论这张
 
阅读(1238)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017