Redis Cluster 的实现 - 初始化(2)
// 设置每次回调时最多处理 1000 个连接#define MAX_CLUSTER_ACCEPTS_PER_CALL 1000
// fd 为 监听套接字
// privdata 为在调用 aeCreateFileEvent() 时设置的 clientdata 参数,这里为 NULL
void clusterAcceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
int cport, cfd;
int max = MAX_CLUSTER_ACCEPTS_PER_CALL;
char cip;
clusterLink *link;
REDIS_NOTUSED(el);
REDIS_NOTUSED(mask);
REDIS_NOTUSED(privdata);
/* If the server is starting up, don't accept cluster connections:
* UPDATE messages may interact with the database content. */
if (server.masterhost == NULL && server.loading) return;
// 循环 MAX_CLUSTER_ACCEPTS_PER_CALL 次来接受新的连接
while(max--) {
// 客户端连接的描述符为 cfg
// cip 为客户端地址
// cport 为已连接 socket 的本地端口
cfd = anetTcpAccept(server.neterr, fd, cip, sizeof(cip), &cport);
if (cfd == ANET_ERR) {
if (errno != EWOULDBLOCK)
redisLog(REDIS_VERBOSE,
"Accepting cluster node: %s", server.neterr);
return;
}
// 这里同样设置 连接 socket 为 非阻塞
anetNonBlock(NULL,cfd);
// 并且打开 socket 选项 TCP_NODELAY(禁止 TCP 的 Nagle 算法)
anetEnableTcpNoDelay(NULL,cfd);
/* Use non-blocking I/O for cluster messages. */
redisLog(REDIS_VERBOSE,"Accepted cluster node %s:%d", cip, cport);
/* Create a link object we use to handle the connection.
* It gets passed to the readable handler when data is available.
* Initiallly the link->node pointer is set to NULL as we don't know
* which node is, but the right node is references once we know the
* node identity. */
link = createClusterLink(NULL);
link->fd = cfd;
aeCreateFileEvent(server.el,cfd,AE_READABLE,clusterReadHandler,link);
}
}
页:
[1]