Redis的Client設計
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指標到外層函數實現超出連結數判斷,顯得有些畸形。
同學們,你們覺得呢?
Ubuntu 14.04下Redis安裝及簡單測試
Redis叢集明細文檔
Ubuntu 12.10下安裝Redis(圖文詳解)+ Jedis串連Redis
Redis系列-安裝部署維護篇
CentOS 6.3安裝Redis
Redis安裝部署學習筆記
Redis設定檔redis.conf 詳解
Redis 的詳細介紹:請點這裡
Redis 的:請點這裡
本文永久更新連結地址: