Redis源码分析系列十三::readQueryFromClient

苦命的码农,牺牲了早睡时间起来看代码,不容易啊!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

上次说到了,从客户连接过来的socket里读取数据。继续分析后面的代码。

 if (nread)
 {
        sdsIncrLen(c->querybuf,nread);
        c->lastinteraction = server.unixtime;
        if (c->flags & REDIS_MASTER) c->reploff += nread;
    }
 else
 {
        server.current_client = NULL;
        return;
    }

我们来分析 sdsIncrLen的作用。

sdsIncrLen(c->querybuf,nread);
void sdsIncrLen(sds s, int incr)
{
 //自定义检查点: 1 2 3
    struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
 //取出指针

    assert(sh->free >= incr);
 //判断指针
 
    sh->len += incr; 
    sh->free -= incr;
 //修改已读长度和剩余长度
 
    assert(sh->free >= 0);
 
    s[sh->len] = '\0';

//在字符的最后写上\0
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

代码继续执行。

后面几行没有啥说的。

然后对读取的长度进行判断。

 

if (sdslen(c->querybuf) > server.client_max_querybuf_len)
 {
        sds ci = getClientInfoString(c), bytes = sdsempty();

        bytes = sdscatrepr(bytes,c->querybuf,64);
        redisLog(REDIS_WARNING,"Closing client that reached max query buffer length: %s (qbuf initial      bytes: %s)", ci, bytes);
        sdsfree(ci);
        sdsfree(bytes);
        freeClient(c);
        return;

}

这里是对读取的数据长度进行判断。不能超过1G.

 

你可能感兴趣的:(redis,redis,qiangzigege,强子哥哥)