epoll / kqueue 在 Redis 中的封装实现(上) 一、概述 Redis 作为高性能的内存数据库,其网络通信层采用了 I/O 多路复用技术来处理大量并发连接。为了适配不同操作系统,Redis 对多种 I/O 多路复用机制进行了统一封装,包括:
机制 操作系统 性能 epoll Linux 高 kqueue BSD/macOS 高 evport Solaris 高 select 跨平台 低(兜底方案) 本文将深入剖析 Redis 对 epoll 的封装实现,下一篇文章将分析 kqueue 的封装。
二、多路复用机制的选择策略 2.1 编译时选择 Redis 在编译时根据操作系统自动选择最优的多路复用机制:
1// src/ae.c:47-61 2/* Include the best multiplexing layer supported by this system. 3 * The following should be ordered by performances, descending. */ 4#ifdef HAVE_EVPORT 5#include "ae_evport.c" 6#else 7 #ifdef HAVE_EPOLL 8 #include "ae_epoll.