• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-161  評論-223  文章-30  trackbacks-0
            主要思路
             1. 首次連接時(shí)調(diào)用redisConnectWithTimeout或redisConnectUnixWithTimeout連接Redis服務(wù)端,若成功則保存返回的redisContext,假設(shè)為ctx
             2. 發(fā)送命令數(shù)據(jù)后獲取響應(yīng),如果是pipeling模式則調(diào)用redisGetReply獲取響應(yīng),再檢查redisContext中的錯(cuò)誤碼,如果為網(wǎng)絡(luò)出錯(cuò)或關(guān)閉,則不置位ctx REDIS_CONNECTED標(biāo)志
             3. 在下次發(fā)送數(shù)據(jù)時(shí),先檢查ctx否置位了REDIS_CONNECTED標(biāo)志,若沒有則調(diào)用redisReconnect重連Redis服務(wù)端

            實(shí)現(xiàn)代碼
             自動連接
             1 int redis_auto_connect(CBED_REDIS *redis)
             2 {
             3     if(NULL==redis->ctx){
             4         redisContext *ctx;
             5         if(redis->type == CONN_TCP)
             6             ctx = redisConnectWithTimeout(redis->conn.tcp.ip, redis->conn.tcp.port, redis->timeout_conn);
             7         else
             8             ctx = redisConnectUnixWithTimeout(redis->conn.unix.path, redis->timeout_conn);
             9         
            10         if(NULL==ctx){
            11             zlog_fatal(c_redis, "redis allocate context fail");
            12             return -1;
            13             
            14         }else if(ctx->err){
            15             zlog_fatal(c_redis, "redis connection %s:%d error: %s", 
            16                         redis->type==CONN_TCP?redis->conn.tcp.ip:redis->conn.unix.path, 
            17                         redis->type==CONN_TCP?redis->conn.tcp.port:0, ctx->errstr);
            18             redisFree(ctx);
            19             return -1;
            20         }
            21         
            22         if(REDIS_ERR==redisSetTimeout(ctx, redis->timeout_rw)){
            23             zlog_fatal(c_redis, "redis set rw timeout error: %s", ctx->errstr);
            24             redisFree(ctx);
            25             return -1;
            26         }
            27         
            28         redis->ctx = ctx;
            29         if(redis_auth(redis)){
            30             redisFree(ctx);
            31             redis->ctx = NULL;
            32             return -1;
            33         }
            34         
            35     }    else if(!(redis->ctx->flags & REDIS_CONNECTED)){
            36         int retry = redis->reconn_max, n = 0;
            37         do {
            38             if(REDIS_OK==redisReconnect(redis->ctx)){
            39                 return redis_auth(redis);
            40             }
            41             
            42             zlog_warn(c_redis, "redis reconnect %d error: %s", ++n, redis->ctx->errstr);    
            43             sleep(redis->reconn_interval); //reconn_interval default is 3 seconds
            44             
            45         }while(--retry > 0);
            46         
            47         zlog_error(c_redis, "redis reconnect exceed max num %d", redis->reconn_max);
            48         return -1;
            49     }
            50 
            51     return 0;
            52 }
             
             發(fā)送時(shí)檢查錯(cuò)誤碼
             1 static int redis_bulk_get_reply(CBED_REDIS *redis)
             2 {
             3     redisReply *r;
             4     int i = 0;        
             5     int num = redis->cmd_num;
             6     redis->cmd_num = 0;
             7 
             8     for(; i<num; ++i){
             9         if(REDIS_OK==redisGetReply(redis->ctx, (void**)&r)){
            10             if(r->type == REDIS_REPLY_ERROR){
            11                 zlog_error(c_redis, "redis get reply error: %.*s", r->len, r->str);
            12                 freeReplyObject(r);
            13                 return -1;
            14             }
            15             freeReplyObject(r);
            16         
            17         }else{
            18             if(redis->ctx->err==REDIS_ERR_IO||redis->ctx->err==REDIS_ERR_EOF)
            19                 redis->ctx->flags &= ~REDIS_CONNECTED;
            20             zlog_fatal(c_redis, "redis get reply fail: %s", redis->ctx->errstr);
            21             return -1;
            22         }
            23     }
            24 
            25     return 0;
            26 }
            27 
            28 int redis_send(CBED_REDIS *redis, unsigned char *data, unsigned int size, int force)
            29 {
            30     if(redis_auto_connect(redis))
            31         return -1;
            32 
            33     int i;
            34     
            35     if(redis->max_cmd_num > 1){ //pipelining
            36         for(i=0; i<redis->queue_num; ++i){
            37             if(REDIS_ERR == redisAppendCommand(redis->ctx, "RPUSH %s %b", redis->queue[i], data, size)) {
            38                 zlog_fatal(c_redis, "redis append command rpush %s len %u fail: %s", redis->queue[i], size, redis->ctx->errstr);
            39                 return -1;
            40             }
            41             
            42             ++redis->cmd_num;
            43             if((!force && redis->cmd_num==redis->max_cmd_num) || force){
            44                 if(redis_bulk_get_reply(redis))
            45                     return -1;
            46             }
            47         }
            48         
            49     }else{
            50         for(i=0; i<redis->queue_num; ++i){
            51             redisReply *r = redisCommand(redis->ctx, "RPUSH %s %b", redis->queue[i], data, size);
            52             if(NULL==r){
            53                 if(redis->ctx->err==REDIS_ERR_IO||redis->ctx->err==REDIS_ERR_EOF)
            54                     redis->ctx->flags &= ~REDIS_CONNECTED;
            55                 zlog_fatal(c_redis, "redis command rpush %s len %u fail: %s", redis->queue[i], size, redis->ctx->errstr);
            56                 return -1;
            57             }
            58             
            59             if(r->type == REDIS_REPLY_ERROR){
            60                 zlog_error(c_redis, "redis reply rpush %s len %u error: %.*s", redis->queue[i], size, r->len, r->str);
            61                 freeReplyObject(r);
            62                 return -1;
            63             }
            64 
            65             freeReplyObject(r);
            66         }
            67     }
            68 
            69     return 0;
            70 }
            posted on 2021-02-25 15:51 春秋十二月 閱讀(6481) 評論(0)  編輯 收藏 引用 所屬分類: Network
            久久亚洲国产午夜精品理论片| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 久久国产精品99精品国产987| 久久国产午夜精品一区二区三区| 色天使久久综合网天天| 久久亚洲中文字幕精品有坂深雪| 久久精品无码一区二区日韩AV | 久久99精品国产麻豆宅宅| 99久久国产免费福利| 国产精品久久久久影视不卡| 久久精品中文无码资源站| 中文国产成人精品久久不卡| 青草久久久国产线免观| 性做久久久久久久久老女人| 狠狠色丁香久久综合婷婷| 久久亚洲精品国产精品婷婷 | 久久久国产精品网站| 思思久久99热只有频精品66| 天天综合久久久网| 久久丫精品国产亚洲av不卡| 国产精品久久久久久久久久影院| 国产成人久久精品麻豆一区| 久久99久久无码毛片一区二区 | 99精品伊人久久久大香线蕉| 久久发布国产伦子伦精品 | 国产三级观看久久| 91久久精品国产免费直播| 久久精品中文闷骚内射| 久久国产免费直播| 久久99精品久久久久久久不卡 | 国内精品久久久久久不卡影院| 久久久久亚洲精品天堂| 少妇高潮惨叫久久久久久| 日本免费一区二区久久人人澡| 熟妇人妻久久中文字幕| 久久亚洲国产成人影院| 久久久久久精品成人免费图片| 尹人香蕉久久99天天拍| 国产∨亚洲V天堂无码久久久| 久久亚洲精品中文字幕| 久久夜色精品国产欧美乱|