libevent带负载均衡的多线程使用示例
- eventtest : eventtest.c
- gcc -Wall -g -levent -lpthread -o eventtest eventtest.c
- .PHONY : clean
- clean :
- rm eventtest -f
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <event.h>
- typedef struct {
- pthread_t tid;
- struct event_base *base;
- struct event event;
- int read_fd;
- int write_fd;
- }LIBEVENT_THREAD;
- typedef struct {
- pthread_t tid;
- struct event_base *base;
- }DISPATCHER_THREAD;
- const int thread_num = 10;
- LIBEVENT_THREAD *threads;
- DISPATCHER_THREAD dispatcher_thread;
- int last_thread = 0;
- static void
- thread_libevent_process(int fd, short which, void *arg)
- {
- int ret;
- char buf[128];
- LIBEVENT_THREAD *me = arg;
- if (fd != me->read_fd) {
- printf("thread_libevent_process error : fd != me->read_fd\n");
- exit(1);
- }
- ret = read(fd, buf, 128);
- if (ret > 0) {
- buf[ret] = '\0';
- printf("thread %llu receive message : %s\n", (unsigned long long)me->tid, buf);
- }
- return;
- }
- static void *
- worker_thread(void *arg)
- {
- LIBEVENT_THREAD *me = arg;
- me->tid = pthread_self();
- event_base_loop(me->base, 0);
- return NULL;
- }
- static void
- timeout_cb(int fd, short event, void *arg)
- {
- struct timeval tv;
- struct event *timeout = arg;
- int tid = (last_thread + 1) % thread_num; //memcached中线程负载均衡算法
- LIBEVENT_THREAD *thread = threads + tid;
- last_thread = tid;
- write(thread->write_fd, "Hello world!", sizeof("Hello world!") - 1);
- evutil_timerclear(&tv);
- tv.tv_sec = 1;
- event_add(timeout, &tv);
- }
- int
- main (int argc, char *argv[])
- {
- int ret;
- int i;
- int fd[2];
- struct event timeout;
- struct timeval tv;
- pthread_t tid;
- dispatcher_thread.base = event_init();
- if (dispatcher_thread.base == NULL) {
- perror("event_init( base )");
- return 1;
- }
- dispatcher_thread.tid = pthread_self();
- threads = calloc(thread_num, sizeof(LIBEVENT_THREAD));
- if (threads == NULL) {
- perror("calloc");
- return 1;
- }
- for (i = 0; i < thread_num; i++) {
- ret = socketpair(AF_LOCAL, SOCK_STREAM, 0, fd);
- if (ret == -1) {
- perror("socketpair()");
- return 1;
- }
- threads[i].read_fd = fd[1];
- threads[i].write_fd = fd[0];
- threads[i].base = event_init();
- if (threads[i].base == NULL) {
- perror("event_init()");
- return 1;
- }
- event_set(&threads[i].event, threads[i].read_fd, EV_READ | EV_PERSIST, thread_libevent_process, &threads[i]);
- event_base_set(threads[i].base, &threads[i].event);
- if (event_add(&threads[i].event, 0) == -1) {
- perror("event_add()");
- return 1;
- }
- }
- for (i = 0; i < thread_num; i++) {
- pthread_create(&tid, NULL, worker_thread, &threads[i]);
- }
- evtimer_set(&timeout, timeout_cb, &timeout);
- event_base_set(dispatcher_thread.base, &timeout);
- evutil_timerclear(&tv);
- tv.tv_sec = 1;
- event_add(&timeout, &tv);
- event_base_loop(dispatcher_thread.base, 0);
- return 0;
- }
libevent带负载均衡的多线程使用示例的更多相关文章
- Ribbon自带负载均衡策略比较
Ribbon自带负载均衡策略比较 策略名 策略声明 策略描述 实现说明 BestAvailableRule public class BestAvailableRule extends ClientC ...
- Ribbon自带负载均衡策略
IRule这是所有负载均衡策略的父接口,里边的核心方法就是choose方法,用来选择一个服务实例. AbstractLoadBalancerRuleAbstractLoadBalancerRule是一 ...
- HAProxy(二):HAProxy的ACL规则实现智能负载均衡详解与示例
一.HAProxy的ACL的功能 ACL(Access Control List)访问控制列表,HAProxy中的ACL的匹配条件和控制条件有许多种,功能很强大,可以通过源地址.源端口.目标地址.目标 ...
- apache、mod_jk负载均衡与tomcat集群
最近需要搭建apache和tomcat的集群,实现静态网站直接通过apache访问,动态网站转交给tomcat处理,实现负载均衡和tomcat集群配置. apache安装 wget http://ap ...
- nginx负载均衡(5种方式)、rewrite重写规则及多server反代配置梳理
Nginx除了可以用作web服务器外,他还可以用来做高性能的反向代理服务器,它能提供稳定高效的负载均衡解决方案.nginx可以用轮询.IP哈希.URL哈希等方式调度后端服务器,同时也能提供健康检查功能 ...
- 客户端负载均衡Ribbon之一:Spring Cloud Netflix负载均衡组件Ribbon介绍
Netflix:['netfliːks] ribbon:英[ˈrɪbən]美[ˈrɪbən]n. 带; 绶带; (打印机的) 色带; 带状物;v. 把…撕成条带; 用缎带装饰; 形成带状; L ...
- Dubbo学习(二) Dubbo 集群容错模式-负载均衡模式
Dubbo是Alibaba开源的分布式服务框架,我们可以非常容易地通过Dubbo来构建分布式服务,并根据自己实际业务应用场景来选择合适的集群容错模式,这个对于很多应用都是迫切希望的,只需要通过简单的配 ...
- (转)nginx负载均衡(5种方式)、rewrite重写规则及多server反代配置梳理
Nginx除了可以用作web服务器外,他还可以用来做高性能的反向代理服务器,它能提供稳定高效的负载均衡解决方案.nginx可以用轮询.IP哈希.URL哈希等方式调度后端服务器,同时也能提供健康检查功能 ...
- spring-cloud-starter-ribbon提供客户端的软件负载均衡算法
Ribbon是什么? Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接超时 ...
随机推荐
- VirtualBox 运行失败
运行 VirtualBox --help 安装 VirtualBox 后 运行 报错内核没加载问题 需要设置环境变量 内核加载的环境变量 export KERN_DIR=/usr/src/kernel ...
- Lucene IndexReader,IndexWriter,IndexSearcher 缓存应用
1.IndexManager类,用于提供IndexReader,IndexWriter,IndexSearcher获取接口 import java.io.File; import java.io.IO ...
- uva 11440 - Help Tomisu(欧拉功能)
题目链接:uva 11440 - Help Tomisu 题目大意:给定n和m,求从2~n.中的数x.要求x的质因子均大于m.问说x有多少个.答案模上1e9+7. 解题思路: (1)n!=k∗m!(n ...
- 好用的log
Log.getStackTraceString(new Throwable())
- 各种语言HMAC SHA256实现
语言包含: Javascript ,PHP,Java,Groovy,C#,Objective C,Go,Ruby,Python,Perl,Dart,Swift,Rust,Powershell. Jav ...
- Echarts使用随笔(2)-Echarts中mapType and data
本文出处:http://blog.csdn.net/chenxiaodan_danny/article/details/39081071 series : [ { ...
- GridView中某一列值的总和(web)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.R ...
- while read line无法循环read文件
while read line 与for循环的区别 例子:要从一个ip列表中获取ip.port,然后ssh ip 到目标机器进行特定的command操作ssh -o StrictHostKeyChec ...
- awk中split函数的用法
time='12:34:56' echo $time | awk '{split($0,a,":" ); print a[1]}' 12 echo $time | awk '{sp ...
- xcode中如何安装多个版本的模拟器
在xcode里面,安装的时间默认自带的有模拟器,有时间为了调试需要使用个多个版本的模拟器 在xcode -> preference 里面 选择download,这里你可下载你需要的模拟器