Redis源码分析(二十二)--- networking网络协议传输(三)

2015-01-27 14:07:48 · 作者: · 浏览: 128
分为Master和Slave2种情况作不同的处理 */ void freeClient(redisClient *c) { listNode *ln; /* If this is marked as current client unset it */ if (server.current_client == c) server.current_client = NULL; /* If it is our master that's beging disconnected we should make sure * to cache the state to try a partial resynchronization later. * * Note that before doing this we make sure that the client is not in * some unexpected state, by checking its flags. */ if (server.master && c->flags & REDIS_MASTER) { redisLog(REDIS_WARNING,"Connection with master lost."); if (!(c->flags & (REDIS_CLOSE_AFTER_REPLY| REDIS_CLOSE_ASAP| REDIS_BLOCKED| REDIS_UNBLOCKED))) { //如果是Master客户端,需要做缓存Client的处理,可以迅速重新启用 replicationCacheMaster(c); return; } } ...后面代码略去了

当Client中的输出buffer数据渐渐变多了的时候就要准备持久化到磁盘文件了,要调用下面这个方法了,

/* Helper function used by freeMemoryIfNeeded() in order to flush slave
 * output buffers without returning control to the event loop. */
/* 从方法将会在freeMemoryIfNeeded(),释放内存空间函数,将存在内存中数据操作结果刷新到磁盘中 */
void flushSlavesOutputBuffers(void) {
    listIter li;
    listNode *ln;

    listRewind(server.slaves,&li);
    while((ln = listNext(&li))) {
        redisClient *slave = listNodeva lue(ln);
        int events;

        events = aeGetFileEvents(server.el,slave->fd);
        if (events & AE_WRITABLE &&
            slave->replstate == REDIS_REPL_ONLINE &&
            listLength(slave->reply))
        {
        	//在这里调用了write的方法
            sendReplyToClient(server.el,slave->fd,slave,0);
        }
    }
}
这个方法的核心调用又在sendReplyToClient()方法,就是把Client的reply内容和buf内容存入文件。以上就是我的理解了,代码量有点大,的确看的我头有点大。