• <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>
            隨筆-159  評論-223  文章-30  trackbacks-0
            主要思路
             1. 首次連接時調用redisConnectWithTimeout或redisConnectUnixWithTimeout連接Redis服務端,若成功則保存返回的redisContext,假設為ctx
             2. 發送命令數據后獲取響應,如果是pipeling模式則調用redisGetReply獲取響應,再檢查redisContext中的錯誤碼,如果為網絡出錯或關閉,則不置位ctx REDIS_CONNECTED標志
             3. 在下次發送數據時,先檢查ctx否置位了REDIS_CONNECTED標志,若沒有則調用redisReconnect重連Redis服務端

            實現代碼
             自動連接
             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 }
             
             發送時檢查錯誤碼
             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 春秋十二月 閱讀(6439) 評論(0)  編輯 收藏 引用 所屬分類: Network
            久久久久AV综合网成人 | 人妻丰满AV无码久久不卡| 中文无码久久精品| 久久综合九色综合精品| 久久无码av三级| 看全色黄大色大片免费久久久| 热99RE久久精品这里都是精品免费 | 久久综合九色综合久99| 久久人人爽人爽人人爽av| 麻豆AV一区二区三区久久| 精品国产婷婷久久久| 色8久久人人97超碰香蕉987| 久久精品中文字幕第23页| 久久久久人妻精品一区| 久久综合偷偷噜噜噜色| 久久发布国产伦子伦精品| 久久这里都是精品| 国产精品成人无码久久久久久 | 日本一区精品久久久久影院| 一本一本久久a久久综合精品蜜桃| 青青热久久综合网伊人| 久久综合给合久久狠狠狠97色69| 亚洲а∨天堂久久精品9966| 国产精品久久久久久搜索| 亚洲精品无码专区久久久 | 亚洲国产另类久久久精品黑人| 精品久久久久久无码人妻热| 久久精品嫩草影院| 97久久精品人妻人人搡人人玩| 伊人久久大香线焦AV综合影院| 久久亚洲国产最新网站| 人妻无码久久精品| 久久强奷乱码老熟女网站| 精品一久久香蕉国产线看播放| 国产激情久久久久影院小草| 93精91精品国产综合久久香蕉| 久久国产乱子伦精品免费强| 久久亚洲国产中v天仙www| 久久se精品一区精品二区| 亚洲国产精品久久久久婷婷老年| 久久青草国产手机看片福利盒子 |