• <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麻豆| 99久久精品午夜一区二区| 99精品久久久久久久婷婷| 久久精品亚洲精品国产欧美| 狠狠色噜噜狠狠狠狠狠色综合久久| 久久本道久久综合伊人| 久久国产视屏| 久久久久亚洲AV成人网人人网站 | 亚洲AV无一区二区三区久久| 久久国产精品视频| 亚洲色欲久久久久综合网| 久久亚洲精品无码观看不卡| 人妻丰满?V无码久久不卡| 99久久精品免费看国产免费| 欧美激情精品久久久久久| 久久精品成人| 国产精品久久成人影院| 久久精品无码av| 久久精品国产亚洲av日韩| 人妻系列无码专区久久五月天| 亚洲va久久久噜噜噜久久天堂| 免费观看久久精彩视频| 婷婷久久综合九色综合九七| 97久久久久人妻精品专区 | 人妻无码αv中文字幕久久琪琪布| 人妻精品久久无码区| 性做久久久久久免费观看| 精品久久久久久无码中文字幕一区| 久久亚洲高清综合| 中文字幕亚洲综合久久2| 伊人久久综合无码成人网| 热RE99久久精品国产66热| 精品国产热久久久福利| 日本免费久久久久久久网站|