高并发的epoll+线程池,epoll在线程池内

epoll是linux下高并发服务器的完美方案,因为是基于事件触发的,所以比select快的不只是一个数量级。
单线程epoll,触发量可达到15000,但是加上业务后,因为大多数业务都与数据库打交道,所以就会存在阻塞的情况,这个时候就必须用多线程来提速。
 
epoll在线程池内,测试结果2000个/s
增加了网络断线后的无效socket检测。
 
测试工具:stressmark
因为加了适用与ab的代码,所以也可以适用ab进行压力测试。
char buf[1000] = {0};
sprintf(buf,"HTTP/1.0 200 OK\r\nContent-type: text/plain\r\n\r\n%s","Hello world!\n");
send(socketfd,buf, strlen(buf),0);
 
 
 

#include <stdio.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>


//stl head

#include <ext/hash_map> //包含hash_map 的头文件

//#include <map> //stl的map

using namespace std; //std 命名空间

using namespace __gnu_cxx; //而hash_map是在__gnu_cxx的命名空间里的



int init_thread_pool(int threadNum);
void *epoll_loop(void* para);
void *check_connect_timeout(void* para);


struct sockStruct
{
    time_t time;

    unsigned int* recvBuf;
};

//hash-map

//hash_map<int, unsigned int>        sock_map;

hash_map<int, sockStruct>        sock_map;

 
#define MAXRECVBUF 4096
#define MAXBUF MAXRECVBUF+10 

int fd_Setnonblocking(int fd)
{
    int op;
 
    op=fcntl(fd,F_GETFL,0);
    fcntl(fd,F_SETFL,op|O_NONBLOCK);
 
    return op;
}
 
void on_sigint(int signal)
{
    exit(0);
}
 
/* 
handle_message - 处理每个 socket 上的消息收发 
*/
 
int handle_message(int new_fd) 
{ 
    char buf[MAXBUF + 1]; 
    char sendbuf[MAXBUF+1]; 
    int len; 
    /* 开始处理每个新连接上的数据收发 */ 
    bzero(buf, MAXBUF + 1); 
    /* 接收客户端的消息 */ 
    //len = recv(new_fd, buf, MAXBUF, 0);



    int nRecvBuf = MAXRECVBUF; //设置为32K 

    setsockopt(new_fd, SOL_SOCKET, SO_RCVBUF, ( const char* )&nRecvBuf, sizeof(int));
    len=recv(new_fd,&buf, MAXBUF,0);

    //--------------------------------------------------------------------------------------------

    //这块为了使用ab测试

    char bufSend[1000] = {0};
    sprintf(bufSend,"HTTP/1.0 200 OK\r\nContent-type: text/plain\r\n\r\n%s","Hello world!\n");
    send(new_fd,bufSend,strlen(buf),0);

    //--------------------------------------------------------------------------------------------


    if (len > 0){ 

        //printf ("%d接收消息成功:'%s',共%d个字节的数据\n", new_fd, buf, len); 


        //hash-map

        
        hash_map<int, sockStruct>::iterator it_find;
        it_find = sock_map.find(new_fd);
        if(it_find == sock_map.end()){
            //新的网络连接,申请新的接收缓冲区,并放入map中

            //printf("new socket %d\n", new_fd);


            sockStruct newSockStruct;
            newSockStruct.time = time((time_t*)0);
            newSockStruct.recvBuf = (unsigned int*)malloc(1000);
            memset(newSockStruct.recvBuf, 0, 1000);
            strcat((char*)newSockStruct.recvBuf, buf);
            sock_map.insert(pair<int,sockStruct>(new_fd, newSockStruct));
        }else{
            //网络连接已经存在,找到对应的数据缓冲区,将接收到的数据拼接到数据缓冲区中

            //printf("socket %d exist!\n", it_find->first);


            (it_find->second).time = time((time_t*)0);                //时间更改

            char* bufSockMap = (char*)(it_find->second).recvBuf;    //数据存储


            strcat(bufSockMap, buf);
            //printf("bufSockMap:%s\n", bufSockMap);

        }


    } 
    else { 
        if (len < 0) 
            printf ("消息接收失败!错误代码是%d,错误信息是'%s'\n", 
            errno, strerror(errno)); 
        else {
            //将socket从map中移除

            /*
            hash_map<int, sockStruct>::iterator it_find;
            it_find = sock_map.find(new_fd);
            sock_map.erase(it_find);
            */

            printf("client %d quit!\n",new_fd); 
        }
        //close(new_fd); 

        return -1; 
    } 
    /* 处理每个新连接上的数据收发结束 */ 

    //关闭socket的时候,要释放接收缓冲区。

    hash_map<int, sockStruct>::iterator it_find;
    it_find = sock_map.find(new_fd);
    free((it_find->second).recvBuf);
    sock_map.erase(it_find);

    close(new_fd);
    return len; 
} 


    int listenfd;
    int sock_op=1;
    struct sockaddr_in address;
    struct epoll_event event;
    struct epoll_event events[1024];
    int epfd;
    int n;
    int i;
    char buf[512];
    int off;
    int result;
    char *p;

int main(int argc,char* argv[])
{

    init_thread_pool(1);

    signal(SIGPIPE,SIG_IGN);
    signal(SIGCHLD,SIG_IGN);
    signal(SIGINT,&on_sigint);
    listenfd=socket(AF_INET,SOCK_STREAM,0);
    setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&sock_op,sizeof(sock_op));
 
    memset(&address,0,sizeof(address));
    address.sin_addr.s_addr=htonl(INADDR_ANY);
    address.sin_port=htons(8006);
    bind(listenfd,(struct sockaddr*)&address,sizeof(address));
    listen(listenfd,1024);
    fd_Setnonblocking(listenfd);
 
    epfd=epoll_create(65535);
    memset(&event,0,sizeof(event));
    event.data.fd=listenfd;
    event.events=EPOLLIN|EPOLLET;
    epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&event);

    while(1){
        sleep(1000);
    }
    return 0;
}

/*************************************************
* Function: * init_thread_pool
* Description: * 初始化线程
* Input: * threadNum:用于处理epoll的线程数
* Output: * 
* Others: * 此函数为静态static函数,
*************************************************/

int init_thread_pool(int threadNum)
{
    int i,ret;

    pthread_t threadId;

    //初始化epoll线程池

    for ( i = 0; i < threadNum; i++)
    {

        ret = pthread_create(&threadId, 0, epoll_loop, (void *)0);
        if (ret != 0)
        {
            printf("pthread create failed!\n");
            return(-1);
        }
    }

    ret = pthread_create(&threadId, 0, check_connect_timeout, (void *)0);

    return(0);
}
/*************************************************
* Function: * epoll_loop
* Description: * epoll检测循环
* Input: * 
* Output: * 
* Others: * 
*************************************************/

static int count111 = 0;
static time_t oldtime = 0, nowtime = 0;
void *epoll_loop(void* para)
{
        while(1)
    {
        n=epoll_wait(epfd,events,4096,-1);
        //printf("n = %d\n", n);

        if(n>0)
        {
            for(i=0;i<n;++i)
            {
                if(events[i].data.fd==listenfd)
                {
                    while(1)
                    {
                        event.data.fd=accept(listenfd,NULL,NULL);
                        if(event.data.fd>0)
                        {
                            fd_Setnonblocking(event.data.fd);
                            event.events=EPOLLIN|EPOLLET;
                            epoll_ctl(epfd,EPOLL_CTL_ADD,event.data.fd,&event);
                        }
                        else
                        {
                            if(errno==EAGAIN)
                            break;
                        }
                    }
                }
                else
                {
                    if(events[i].events&EPOLLIN)
                    {
                        //handle_message(events[i].data.fd);


                        char recvBuf[1024] = {0}; 

                        int ret = 999;

                        int rs = 1;


                        while(rs)
                        {
                            ret = recv(events[n].data.fd,recvBuf,1024,0);// 接受客户端消息

                            if(ret < 0)
                            {
                                //由于是非阻塞的模式,所以当errno为EAGAIN时,表示当前缓冲区已无数据可//读在这里就当作是该次事件已处理过。

                                if(errno == EAGAIN)
                                {
                                    printf("EAGAIN\n");
                                    break;
                                }
                                else{
                                    printf("recv error!\n");
                                    epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, &event);
                                    close(events[i].data.fd);
                                    break;
                                }
                            }
                            else if(ret == 0)
                            {
                                // 这里表示对端的socket已正常关闭. 

                                rs = 0;
                            }
                            if(ret == sizeof(recvBuf))
                                rs = 1; // 需要再次读取

                            else
                                rs = 0;
                        }




                        if(ret>0){

                            count111 ++;



                            struct tm *today;
                            time_t ltime;
                            time( &nowtime );

                            if(nowtime != oldtime){
                                printf("%d\n", count111);
                                oldtime = nowtime;
                                count111 = 0;
                            }


                            char buf[1000] = {0};
                            sprintf(buf,"HTTP/1.0 200 OK\r\nContent-type: text/plain\r\n\r\n%s","Hello world!\n");
                            send(events[i].data.fd,buf,strlen(buf),0);


                            //    CGelsServer Gelsserver;

                            //    Gelsserver.handle_message(events[i].data.fd);

                        }


                        epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, &event);
                        close(events[i].data.fd);

                    }
                    else if(events[i].events&EPOLLOUT)
                    {
                        sprintf(buf,"HTTP/1.0 200 OK\r\nContent-type: text/plain\r\n\r\n%s","Hello world!\n");
                        send(events[i].data.fd,buf,strlen(buf),0);
                        /*
                        if(p!=NULL)
                        {
                            free(p);
                            p=NULL;
                        }
                        */

                        close(events[i].data.fd);
                    }
                    else
                    {
                        close(events[i].data.fd);
                    }
                }
            }
        }
    }

}
/*************************************************
* Function: * check_connect_timeout
* Description: * 检测长时间没反应的网络连接,并关闭删除
* Input: * 
* Output: * 
* Others: * 
*************************************************/

void *check_connect_timeout(void* para)
{
    hash_map<int, sockStruct>::iterator it_find;
    for(it_find = sock_map.begin(); it_find!=sock_map.end(); ++it_find){
        if( time((time_t*)0) - (it_find->second).time > 120){                //时间更改


            free((it_find->second).recvBuf);
            sock_map.erase(it_find);

            close(it_find->first);
        }
    }

}


此文章来自于【http://blog.chinaunix.net/uid-311680-id-2439723.html】

展开阅读全文

epoll

03-09

#include rn#include rn#include rn#include rn#include rn#include rn#include rn#include rn#include rn#include rn#include rn#include rn#include rn#include rn#include rn#include rn#define MAXBUF 1024rn#define MAXEPOLLSIZE 10000rn/*rn setnonblocking - 设置句柄为非阻塞方式rn */rnint setnonblocking(int sockfd)rnrn if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK) == -1)rn rn return -1;rn rn return 0;rnrn/*rn handle_message - 处理每个 socket 上的消息收发rn */rnint handle_message(int new_fd)rnrn char buf[MAXBUF + 1];rn int len;rn /* 开始处理每个新连接上的数据收发 */rn bzero(buf, MAXBUF + 1);rn /* 接收客户端的消息 */rn len = recv(new_fd, buf, MAXBUF, 0);rn if (len > 0)rn rn printf("%d接收消息成功:'%s',共%d个字节的数据\n",rn new_fd, buf, len);rn rn elsern rn if (len < 0)rn printfrn ("消息接收失败!错误代码是%d,错误信息是'%s'\n",rn errno, strerror(errno));rn close(new_fd);rn return -1;rn rn /* 处理每个新连接上的数据收发结束 */rn return len;rnrnint main(int argc, char **argv)rnrn int listener, new_fd, kdpfd, nfds, n, ret, curfds;rn socklen_t len;rn struct sockaddr_in my_addr, their_addr;rn unsigned int myport, lisnum;rn struct epoll_event ev;rn struct epoll_event events[MAXEPOLLSIZE];rn struct rlimit rt;rn myport = 5000;rn lisnum = 2; rn /* 设置每个进程允许打开的最大文件数 */rn rt.rlim_max = rt.rlim_cur = MAXEPOLLSIZE;rn if (setrlimit(RLIMIT_NOFILE, &rt) == -1) rn rn perror("setrlimit");rn exit(1);rn rn else rn rn printf("设置系统资源参数成功!\n");rn rn /* 开启 socket 监听 */rn if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1)rn rn perror("socket");rn exit(1);rn rn elsern rn printf("socket 创建成功!\n");rn rn setnonblocking(listener);rn bzero(&my_addr, sizeof(my_addr));rn my_addr.sin_family = PF_INET;rn my_addr.sin_port = htons(myport);rn my_addr.sin_addr.s_addr = INADDR_ANY;rn if (bind(listener, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1) rn rn perror("bind");rn exit(1);rn rn elsern rn printf("IP 地址和端口绑定成功\n");rn rn if (listen(listener, lisnum) == -1) rn rn perror("listen");rn exit(1);rn rn elsern rn printf("开启服务成功!\n");rn rn /* 创建 epoll 句柄,把监听 socket 加入到 epoll 集合里 */rn kdpfd = epoll_create(MAXEPOLLSIZE);rn len = sizeof(struct sockaddr_in);rn ev.events = EPOLLIN | EPOLLET;rn ev.data.fd = listener;rn if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, listener, &ev) < 0) rn rn fprintf(stderr, "epoll set insertion error: fd=%d\n", listener);rn return -1;rn rn elsern rn printf("监听 socket 加入 epoll 成功!\n");rn rn curfds = 1;rn while (1) rn rn /* 等待有事件发生 */rn nfds = epoll_wait(kdpfd, events, curfds, -1);rn if (nfds == -1)rn rn perror("epoll_wait");rn break;rn rn /* 处理所有事件 */rn for (n = 0; n < nfds; ++n)rn rn if (events[n].data.fd == listener) rn rn new_fd = accept(listener, (struct sockaddr *) &their_addr,&len);rn if (new_fd < 0) rn rn perror("accept");rn continue;rn rn elsern rn printf("有连接来自于: %d:%d, 分配的 socket 为:%d\n",rn inet_ntoa(their_addr.sin_addr), ntohs(their_addr.sin_port), new_fd);rn rn setnonblocking(new_fd);rn ev.events = EPOLLIN | EPOLLET;rn ev.data.fd = new_fd;rn if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, new_fd, &ev) < 0)rn rn fprintf(stderr, "把 socket '%d' 加入 epoll 失败!%s\n",rn new_fd, strerror(errno));rn return -1;rn rn curfds++;rn rn elsern rn ret = handle_message(events[n].data.fd);rn if (ret < 1 && errno != 11)rn rn epoll_ctl(kdpfd, EPOLL_CTL_DEL, events[n].data.fd,&ev);rn curfds--;rn rn rn rn rn close(listener);rn return 0;rnrnrn这是epoll基本模型rnrn如果先epoll_ctl(kdpfd, EPOLL_CTL_DEL, events[n].data.fd,&ev);rn再ret = handle_message(events[n].data.fd); 一样可以处理事件rn如果不if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, new_fd, &ev) < 0)确不能接收消息了rnrnEPOLL_CTL_DEL注册和删除事件,到底起什么作用啊?影响是什么 论坛

没有更多推荐了,返回首页