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

记录创意的火花

天天学习,好好向上

 
 
 

日志

 
 

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

2013-07-23 13:39:02|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
// 直接写代码稍后解释。

///////////////////////////////echo-uv.c

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include "uv.h"
#include "task.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")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


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\n", exit_status, term_signal);
   uv_close((uv_handle_t*) req, NULL);
}

uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size) {
   return uv_buf_init((char*) calloc(suggested_size, 1), suggested_size);
}

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

   uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
   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, NULL);
      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;
   }
   return uv_run(loop, UV_RUN_DEFAULT);
}
//////////////////////////////////////////////////////////////////////////worker.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;
uv_pipe_t queue;




uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size) {
    return uv_buf_init((char*) calloc(suggested_size, 1), suggested_size);
}

void echo_write(uv_write_t *req, int status) {
    if (status == -1) {
        fprintf(stderr, "Write error %s\n", uv_err_name(uv_last_error(loop)));
    
    char *base = (char*) req->data;
    free(base);
    free(req);
}
}

void echo_read(uv_stream_t *client, ssize_t nread, uv_buf_t buf) {
    if (nread == -1) {
        if (uv_last_error(loop).code != UV_EOF)
            fprintf(stderr, "Read error %s\n", uv_err_name(uv_last_error(loop)));
        uv_close((uv_handle_t*) client, NULL);
        return;
    }
   uv_write_t *req = (uv_write_t *) malloc(sizeof(uv_write_t));
   req->data = (void*) buf.base;
    buf.len = nread;
    uv_write(req, client, &buf, 1, echo_write);
}

void on_new_connection(uv_pipe_t *q, ssize_t nread, uv_buf_t buf, uv_handle_type pending) {
    if (pending == UV_UNKNOWN_HANDLE) {
        // error!
        return;
    }

    uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
    uv_tcp_init(loop, client);
    if (uv_accept((uv_stream_t*) q, (uv_stream_t*) client) == 0) {
        fprintf(stderr, "Worker %d: Accepted a socket \n", getpid());
        uv_read_start((uv_stream_t*) client, alloc_buffer, echo_read);
    }
    else {
        uv_close((uv_handle_t*) client, NULL);
free(client);
    }
}

int main() {
    loop = uv_default_loop();

    uv_pipe_init(loop, &queue, 1);
    uv_pipe_open(&queue, 0);
    uv_read2_start((uv_stream_t*)&queue, alloc_buffer, on_new_connection);
    return uv_run(loop, UV_RUN_DEFAULT);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
代码说明:
 1.本代码是在windows下运行的,分主、从两部分,主进程代码 echo-uv.c  子进程代码work.c
 2.主进程中代码 strcpy(worker_path + (strlen(worker_path) - strlen("echo-uv.exe")), "worker.exe");表明两个进程放在同一文件夹下,名字分别是echo-uv.exe,和worker.exe
3.基本原理,主进行接受到的连接,通过 uv_write2函数把接受到的连接发给子进程,子进程个数由CUP个数来决定,每一连接发给哪个进程,由主进程来调度
4,关于性能,cup和内存使用比例非常少,上万个连接没有压力,目前超过其他任何用例
5,在连接分配上实现负载均衡
  评论这张
 
阅读(2197)| 评论(1)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

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

页脚

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