You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

14 KiB

32 | 自己动手写高性能HTTP服务器设计和思路

你好我是盛延敏这里是网络编程实战第32讲欢迎回来。

从这一讲开始我们进入实战篇开启一个高性能HTTP服务器的编写之旅。

在开始编写高性能HTTP服务器之前我们先要构建一个支持TCP的高性能网络编程框架完成这个TCP高性能网络框架之后再增加HTTP特性的支持就比较容易了这样就可以很快开发出一个高性能的HTTP服务器程序。

设计需求

在第三个模块性能篇中我们已经使用这个网络编程框架完成了多个应用程序的开发这也等于对网络编程框架提出了编程接口方面的需求。综合之前的使用经验TCP高性能网络框架需要满足的需求有以下三点。

第一采用reactor模型可以灵活使用poll/epoll作为事件分发实现。

第二必须支持多线程从而可以支持单线程单reactor模式也可以支持多线程主-从reactor模式。可以将套接字上的I/O事件分离到多个线程上。

第三封装读写操作到Buffer对象中。

按照这三个需求正好可以把整体设计思路分成三块来讲解分别包括反应堆模式设计、I/O模型和多线程模型设计、数据读写封装和buffer。今天我们主要讲一下主要的设计思路和数据结构以及反应堆模式设计。

主要设计思路

反应堆模式设计

反应堆模式,按照性能篇的讲解,主要是设计一个基于事件分发和回调的反应堆框架。这个框架里面的主要对象包括:

  • event_loop

你可以把event_loop这个对象理解成和一个线程绑定的无限事件循环你会在各种语言里看到event_loop这个抽象。这是什么意思呢简单来说它就是一个无限循环着的事件分发器一旦有事件发生它就会回调预先定义好的回调函数完成事件的处理。

具体来说event_loop使用poll或者epoll方法将一个线程阻塞等待各种I/O事件的发生。

  • channel

对各种注册到event_loop上的对象我们抽象成channel来表示例如注册到event_loop上的监听事件注册到event_loop上的套接字读写事件等。在各种语言的API里你都会看到channel这个对象大体上它们表达的意思跟我们这里的设计思路是比较一致的。

  • acceptor

acceptor对象表示的是服务器端监听器acceptor对象最终会作为一个channel对象注册到event_loop上以便进行连接完成的事件分发和检测。

  • event_dispatcher

event_dispatcher是对事件分发机制的一种抽象也就是说可以实现一个基于poll的poll_dispatcher也可以实现一个基于epoll的epoll_dispatcher。在这里我们统一设计一个event_dispatcher结构体来抽象这些行为。

  • channel_map

channel_map保存了描述字到channel的映射这样就可以在事件发生时根据事件类型对应的套接字快速找到channel对象里的事件处理函数。

I/O模型和多线程模型设计

I/O线程和多线程模型主要解决event_loop的线程运行问题以及事件分发和回调的线程执行问题。

  • thread_pool

thread_pool维护了一个sub-reactor的线程列表它可以提供给主reactor线程使用每次当有新的连接建立时可以从thread_pool里获取一个线程以便用它来完成对新连接套接字的read/write事件注册将I/O线程和主reactor线程分离。

  • event_loop_thread

event_loop_thread是reactor的线程实现连接套接字的read/write事件检测都是在这个线程里完成的。

Buffer和数据读写

  • buffer

buffer对象屏蔽了对套接字进行的写和读的操作如果没有buffer对象连接套接字的read/write事件都需要和字节流直接打交道这显然是不友好的。所以我们也提供了一个基本的buffer对象用来表示从连接套接字收取的数据以及应用程序即将需要发送出去的数据。

  • tcp_connection

tcp_connection这个对象描述的是已建立的TCP连接。它的属性包括接收缓冲区、发送缓冲区、channel对象等。这些都是一个TCP连接的天然属性。

tcp_connection是大部分应用程序和我们的高性能框架直接打交道的数据结构。我们不想把最下层的channel对象暴露给应用程序因为抽象的channel对象不仅仅可以表示tcp_connection前面提到的监听套接字也是一个channel对象后面提到的唤醒socketpair也是一个 channel对象。所以我们设计了tcp_connection这个对象希望可以提供给用户比较清晰的编程入口。

反应堆模式设计

概述

下面我们详细讲解一下以event_loop为核心的反应堆模式设计。这里有一张event_loop的运行详图你可以对照这张图来理解。

当event_loop_run完成之后线程进入循环首先执行dispatch事件分发一旦有事件发生就会调用channel_event_activate函数在这个函数中完成事件回调函数eventReadcallback和eventWritecallback的调用最后再进行event_loop_handle_pending_channel用来修改当前监听的事件列表完成这个部分之后又进入了事件分发循环。

event_loop分析

说event_loop是整个反应堆模式设计的核心一点也不为过。先看一下event_loop的数据结构。

在这个数据结构中最重要的莫过于event_dispatcher对象了。你可以简单地把event_dispatcher理解为poll或者epoll它可以让我们的线程挂起等待事件的发生。

这里有一个小技巧就是event_dispatcher_data它被定义为一个void *类型可以按照我们的需求任意放置一个我们需要的对象指针。这样针对不同的实现例如poll或者epoll都可以根据需求放置不同的数据对象。

event_loop中还保留了几个跟多线程有关的对象如owner_thread_id是保留了每个event loop的线程IDmutex和con是用来进行线程同步的。

socketPair是父线程用来通知子线程有新的事件需要处理。pending_head和pending_tail是保留在子线程内的需要处理的新事件。

struct event_loop {
    int quit;
    const struct event_dispatcher *eventDispatcher;

    /** 对应的event_dispatcher的数据. */
    void *event_dispatcher_data;
    struct channel_map *channelMap;

    int is_handle_pending;
    struct channel_element *pending_head;
    struct channel_element *pending_tail;

    pthread_t owner_thread_id;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    int socketPair[2];
    char *thread_name;
};

下面我们看一下event_loop最主要的方法event_loop_run方法前面提到过event_loop就是一个无限while循环不断地在分发事件。

/**
 *
 * 1.参数验证
 * 2.调用dispatcher来进行事件分发,分发完回调事件处理函数
 */
int event_loop_run(struct event_loop *eventLoop) {
    assert(eventLoop != NULL);

    struct event_dispatcher *dispatcher = eventLoop->eventDispatcher;

    if (eventLoop->owner_thread_id != pthread_self()) {
        exit(1);
    }

    yolanda_msgx("event loop run, %s", eventLoop->thread_name);
    struct timeval timeval;
    timeval.tv_sec = 1;

    while (!eventLoop->quit) {
        //block here to wait I/O event, and get active channels
        dispatcher->dispatch(eventLoop, &timeval);

        //handle the pending channel
        event_loop_handle_pending_channel(eventLoop);
    }

    yolanda_msgx("event loop end, %s", eventLoop->thread_name);
    return 0;
}

代码很明显地反映了这一点这里我们在event_loop不退出的情况下一直在循环循环体中调用了dispatcher对象的dispatch方法来等待事件的发生。

event_dispacher分析

为了实现不同的事件分发机制这里把poll、epoll等抽象成了一个event_dispatcher结构。event_dispatcher的具体实现有poll_dispatcher和epoll_dispatcher两种实现的方法和性能篇2122讲类似,这里就不再赘述,你如果有兴趣的话,可以直接研读代码。

/** 抽象的event_dispatcher结构体对应的实现如select,poll,epoll等I/O复用. */
struct event_dispatcher {
    /**  对应实现 */
    const char *name;

    /**  初始化函数 */
    void *(*init)(struct event_loop * eventLoop);

    /** 通知dispatcher新增一个channel事件*/
    int (*add)(struct event_loop * eventLoop, struct channel * channel);

    /** 通知dispatcher删除一个channel事件*/
    int (*del)(struct event_loop * eventLoop, struct channel * channel);

    /** 通知dispatcher更新channel对应的事件*/
    int (*update)(struct event_loop * eventLoop, struct channel * channel);

    /** 实现事件分发然后调用event_loop的event_activate方法执行callback*/
    int (*dispatch)(struct event_loop * eventLoop, struct timeval *);

    /** 清除数据 */
    void (*clear)(struct event_loop * eventLoop);
};

channel对象分析

channel对象是用来和event_dispather进行交互的最主要的结构体它抽象了事件分发。一个channel对应一个描述字描述字上可以有READ可读事件也可以有WRITE可写事件。channel对象绑定了事件处理函数event_read_callback和event_write_callback。

typedef int (*event_read_callback)(void *data);

typedef int (*event_write_callback)(void *data);

struct channel {
    int fd;
    int events;   //表示event类型

    event_read_callback eventReadCallback;
    event_write_callback eventWriteCallback;
    void *data; //callback data, 可能是event_loop也可能是tcp_server或者tcp_connection
};

channel_map对象分析

event_dispatcher在获得活动事件列表之后需要通过文件描述字找到对应的channel从而回调channel上的事件处理函数event_read_callback和event_write_callback为此设计了channel_map对象。

/**
 * channel映射表, key为对应的socket描述字
 */
struct channel_map {
    void **entries;

    /* The number of entries available in entries */
    int nentries;
};

channel_map对象是一个数组数组的下标即为描述字数组的元素为channel对象的地址。

比如描述字3对应的channel就可以这样直接得到。

struct chanenl * channel = map->entries[3];

这样当event_dispatcher需要回调channel上的读、写函数时调用channel_event_activate就可以下面是channel_event_activate的实现在找到了对应的channel对象之后根据事件类型回调了读函数或者写函数。注意这里使用了EVENT_READ和EVENT_WRITE来抽象了poll和epoll的所有读写事件类型。

int channel_event_activate(struct event_loop *eventLoop, int fd, int revents) {
    struct channel_map *map = eventLoop->channelMap;
    yolanda_msgx("activate channel fd == %d, revents=%d, %s", fd, revents, eventLoop->thread_name);

    if (fd < 0)
        return 0;

    if (fd >= map->nentries)return (-1);

    struct channel *channel = map->entries[fd];
    assert(fd == channel->fd);

    if (revents & (EVENT_READ)) {
        if (channel->eventReadCallback) channel->eventReadCallback(channel->data);
    }
    if (revents & (EVENT_WRITE)) {
        if (channel->eventWriteCallback) channel->eventWriteCallback(channel->data);
    }

    return 0;
}

增加、删除、修改channel event

那么如何增加新的channel event事件呢下面这几个函数是用来增加、删除和修改channel event事件的。

int event_loop_add_channel_event(struct event_loop *eventLoop, int fd, struct channel *channel1);

int event_loop_remove_channel_event(struct event_loop *eventLoop, int fd, struct channel *channel1);

int event_loop_update_channel_event(struct event_loop *eventLoop, int fd, struct channel *channel1);

前面三个函数提供了入口能力,而真正的实现则落在这三个函数上:

int event_loop_handle_pending_add(struct event_loop *eventLoop, int fd, struct channel *channel);

int event_loop_handle_pending_remove(struct event_loop *eventLoop, int fd, struct channel *channel);

int event_loop_handle_pending_update(struct event_loop *eventLoop, int fd, struct channel *channel);

我们看一下其中的一个实现event_loop_handle_pending_add在当前event_loop的channel_map里增加一个新的key-value对key是文件描述字value是channel对象的地址。之后调用event_dispatcher对象的add方法增加channel event事件。注意这个方法总在当前的I/O线程中执行。

// in the i/o thread
int event_loop_handle_pending_add(struct event_loop *eventLoop, int fd, struct channel *channel) {
    yolanda_msgx("add channel fd == %d, %s", fd, eventLoop->thread_name);
    struct channel_map *map = eventLoop->channelMap;

    if (fd < 0)
        return 0;

    if (fd >= map->nentries) {
        if (map_make_space(map, fd, sizeof(struct channel *)) == -1)
            return (-1);
    }

    //第一次创建,增加
    if ((map)->entries[fd] == NULL) {
        map->entries[fd] = channel;
        //add channel
        struct event_dispatcher *eventDispatcher = eventLoop->eventDispatcher;
        eventDispatcher->add(eventLoop, channel);
        return 1;
    }

    return 0;
}

总结

在这一讲里我们介绍了高性能网络编程框架的主要设计思路和基本数据结构以及反应堆设计相关的具体做法。在接下来的章节中我们将继续编写高性能网络编程框架的线程模型以及读写Buffer部分。

思考题

和往常一样,给你留两道思考题:

第一道如果你有兴趣不妨实现一个select_dispatcher对象用select方法实现定义好的event_dispatcher接口

第二道仔细研读channel_map实现中的map_make_space部分说说你的理解。

欢迎你在评论区写下你的思考,也欢迎把这篇文章分享给你的朋友或者同事,一起交流一下。