• <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
            国产亚洲精久久久久久无码| 一本大道加勒比久久综合| 久久午夜无码鲁丝片秋霞| 国内精品久久久久久久久电影网| 久久96国产精品久久久| 欧美伊人久久大香线蕉综合69| 亚洲欧美日韩中文久久| 久久这里只精品国产99热| 久久精品极品盛宴观看| 精品午夜久久福利大片| 一级做a爰片久久毛片看看| 国产美女久久久| 伊人色综合久久天天人守人婷| 97久久天天综合色天天综合色hd| 日韩AV毛片精品久久久| 97久久久久人妻精品专区| 久久毛片一区二区| 品成人欧美大片久久国产欧美| 亚洲色大成网站www久久九| 99热成人精品热久久669| 伊人久久无码精品中文字幕| 99久久国产综合精品五月天喷水| 久久人人爽人人爽人人片AV高清| 国产成人无码精品久久久久免费| 久久久久亚洲AV片无码下载蜜桃| 久久久久国产成人精品亚洲午夜| 久久久久99精品成人片欧美 | 亚洲AV无码久久精品成人 | 亚洲国产精品18久久久久久| 久久99精品久久久久久野外| 久久99热精品| 国内精品久久久久影院日本| 色88久久久久高潮综合影院| 欧美成人免费观看久久| 亚洲国产天堂久久综合| 久久久久亚洲AV成人网人人网站| 久久不射电影网| 久久这里只有精品久久| 亚洲国产精品婷婷久久| 久久99久久成人免费播放| 久久久精品视频免费观看|