Redis的client设计如下:
/* With multiplexing we need to take per-clinet state.
?* Clients are taken in a liked list. */
typedef struct redisClient {
? ? int fd;
? ? redisDb *db;
? ? int dictid;
? ? sds querybuf;
? ? robj **argv, **mbargv;
? ? int argc, mbargc;
? ? int bulklen;? ? ? ? ? ? /* bulk read len. -1 if not in bulk read mode */
? ? int multibulk;? ? ? ? ? /* multi bulk command format active */
? ? list *reply;
? ? int sentlen;
? ? time_t lastinteraction; /* time of the last interaction, used for timeout */
? ? int flags;? ? ? ? ? ? ? /* REDIS_CLOSE | REDIS_SLAVE | REDIS_MONITOR */
? ? int slaveseldb;? ? ? ? /* slave selected db, if this client is a slave */
? ? int authenticated;? ? ? /* when requirepass is non-NULL */
? ? int replstate;? ? ? ? ? /* replication state if this is a slave */
? ? int repldbfd;? ? ? ? ? /* replication DB file descriptor */
? ? long repldboff;? ? ? ? ? /* replication DB file offset */
? ? off_t repldbsize;? ? ? /* replication DB file size */
} redisClient;
在accpet后,开始创建一个client。
代码如下:
static client createClient(void) {
? ? client c = zmalloc(sizeof(struct _client));
? ? char err[ANET_ERR_LEN];
? ? c->fd = anetTcpNonBlockConnect(err,config.hostip,config.hostport);
? ? if (c->fd == ANET_ERR) {
? ? ? ? zfree(c);
? ? ? ? fprintf(stderr,"Connect: %s\n",err);
? ? ? ? return NULL;
? ? }
? ? anetTcpNoDelay(NULL,c->fd);
? ? c->obuf = sdsempty();
? ? c->ibuf = sdsempty();
? ? c->mbulk = -1;
? ? c->readlen = 0;
? ? c->written = 0;
? ? c->totreceived = 0;
? ? c->state = CLIENT_CONNECTING;
? ? aeCreateFileEvent(config.el, c->fd, AE_WRITABLE, writeHandler, c);
? ? config.liveclients++;
? ? listAddNodeTail(config.clients,c);
? ? return c;
}
窃以为,以下代码写的有点那个。
? listAddNodeTail(config.clients,c);
这个函数既然命名为createclient就该做create的事,而把listaddnodetail放到外层函数里面实现。
既然在createclient函数里实现了add功能,又再返回一个c指针到外层函数实现超出链接数判断,显得有些畸形。
同学们,你们觉得呢?