int init_pyrebloom(pyrebloomctxt * ctxt, unsigned char * key, uint32_t capacity, double error, char* host, uint32_t port) { // Counter uint32_t i; ctxt->capacity = capacity; ctxt->bits = - (uint32_t)((log(error) * capacity) / (log(2) * log(2))); ctxt->hashes = (uint32_t)(ceil(log(2) * ctxt->bits / capacity)); ctxt->error = error; ctxt->key = key;//(unsigned char *)(malloc(strlen(key))); //strcpy(ctxt->key, key); ctxt->seeds = (uint32_t *)(malloc(ctxt->hashes * sizeof(uint32_t))); // Generate all the seeds // Make sure we get the same seeds on subsequent calls. srand(0); for (i = 0; i < ctxt->hashes; ++i) { ctxt->seeds[i] = (uint32_t)(rand()); } // Now for the redis context struct timeval timeout = { 1, 500000 }; ctxt->ctxt = redisConnectWithTimeout(host, port, timeout); if (ctxt->ctxt->err) { // Some kind of error return 0; } return 1; }
int main(int argc, char** argv) { redisReply* reply; redisReply* reply1; redisReply* reply2; redisReply* reply3; struct timeval timeout = {1, 0}; char* meta_ip; unsigned short meta_port; char command[1024]; if (argc < 3) { fprintf(stderr, "Right usage: meta_tool meta_ip meta_port"); return -1; } meta_ip = argv[1]; meta_port = (unsigned short)strtoul(argv[2], (char**)NULL, 10); // meta_ip = "10.209.52.30"; // meta_port = 6000; context = redisConnectWithTimeout(meta_ip, meta_port, timeout); if (context == NULL) { FATAL("connect to meta server failed"); freeReplyObject(reply); return -1; } if (context->err) { FATAL("connect to meta failed: %s", context->errstr); freeReplyObject(reply); return -1; } reply = (redisReply*)redisCommand(context, "hgetall bae_apps_hash"); if (reply == NULL) { FATAL("hgetall bae_apps_hash failed: %s", context->errstr); freeReplyObject(reply); return -1; } else if (reply->type != REDIS_REPLY_ARRAY) { FATAL("invalid reply type:%d, REDIS_REPLY_ARRAY %u\n", reply->type, REDIS_REPLY_ARRAY); freeReplyObject(reply); return -1; } for (unsigned idx = 0; idx < reply->elements; idx += 2) { reply1 = reply->element[idx]; reply2 = reply->element[idx + 1]; //printf("%s:%s\n", reply1->str, reply2->str); sprintf(command, "hdel bae_apps_hash %s", reply1->str); reply3 = (redisReply*)redisCommand(context, command); freeReplyObject(reply3); parseAppsHash(reply2->str); } freeReplyObject(reply); redisFree(context); return 0; }
int ConfigCenter::Init(const char* pszHostName ,int port,int dbidx ,int timeout ) { m_ctx = NULL; m_reply = NULL; ///////////////////////////////// struct timeval tvout = { timeout/1000, (timeout%1000)*1000 }; m_ctx = redisConnectWithTimeout(pszHostName, port, tvout); if (m_ctx == NULL || m_ctx->err) { if (m_ctx) { LOG_ERROR("redis Connection error: %s\n", m_ctx->errstr); redisFree(m_ctx); } else { LOG_ERROR("redis Connection error: can't allocate redis context\n"); } return -1; } m_reply = (redisReply*)redisCommand(m_ctx,"SELECT %d",dbidx); freeReplyObject(m_reply); LOG_INFO("config center init ok !"); return HeartBeat(); }
bool RedisClient::Connect() { if (IsConnected()) { Quit(); } if (m_strName.IsEmpty()) return false; struct timeval timeout = { 5, 500000 }; // 1.5 seconds m_pClient = redisConnectWithTimeout((char*)m_strIP.c_str(), m_iPort, timeout); if (m_pClient->err) { printf("Connection error: %s\n", m_pClient->errstr); SetLastError(m_pClient->errstr); return false; } if (!m_strAuth.empty()) { redisReply* reply = Command("AUTH %s", m_strAuth.c_str()); if (!(reply && reply->type==REDIS_REPLY_STATUS)) { return false; } } m_isConnected = true; if (m_isConnected && DatabasesNum(m_Databases)) return true; return false; }
//redis,add,begin bool QTSServerInterface::ConRedis()//连接redis服务器 { if(fIfConSucess) return true; struct timeval timeout = { 0, 500000 }; // 0.5 seconds fRedisCon = redisConnectWithTimeout(fRedisIP.c_str(), fRedisPort, timeout);//test,redis的ip和端口应该在xml中指定 if (fRedisCon == NULL || fRedisCon->err) { if (fRedisCon) { qtss_printf("INFO:Connect redis failed,%s\n", fRedisCon->errstr); redisFree(fRedisCon); } else { qtss_printf("INFO:Connect redis failed,can't allocate redis context\n"); } fIfConSucess=false; } else { fIfConSucess=true; struct timeval timeoutEx = { 1, 0 }; // 1seconds,设置socket接收和发送超时 redisSetTimeout(fRedisCon,timeoutEx); RedisInit();//可能在这个函数的执行过程中,redis连接又失败了,所以真正的连接失败或者成功要看fIfConSucess qtss_printf("INFO:Connect redis sucess\n"); } return fIfConSucess; }
static struct redis *__redis_connect(struct redis *redis) { struct timeval timeout = {1, 500000}; // 1.5 seconds LM_INFO("Connecting to Redis at %s:%d\n", redis->ip, redis->port); if(redis->ctxt) redisFree(redis->ctxt); redis->ctxt = redisConnectWithTimeout(redis->ip, redis->port, timeout); if(redis->ctxt == NULL || redis->ctxt->err) { if(!redis->ctxt) LM_ERR("Connection error: can't allocate Redis context\n"); else { LM_ERR("Connection error: %s\n", redis->ctxt->errstr); redisFree(redis->ctxt); } return NULL; } if(!__redis_select_db(redis->ctxt, redis->db)) return NULL; return redis; }
static redisContext *init_redisclient(server_rec *s, const char *redis_ip, int redis_port, const char *redis_password) { redisContext *ctx; redisReply *reply; struct timeval timeout = { 1, 500000 }; // 1.5 seconds ctx = redisConnectWithTimeout(redis_ip, redis_port, timeout); if (ctx == NULL || ctx->err) { if (ctx) { free_redis_ctx(ctx, s); } ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "redis connection fail."); return NULL; } // AUTH reply = redisCommand(ctx,"AUTH %s", redis_password); if (reply == NULL) { // wrong redis password ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,"wrong redis password"); free_redis_ctx(ctx, s); return NULL; } // DEBUG //ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,"redis reply: AUTH, type[%d], str[%s]", reply->type, reply->str); freeReplyObject(reply); return ctx; }
static void rc_validate_connection(rconn_t *c, int optional) { if (!c->rc && (c->flags & RCF_RECONNECT)) { struct timeval tv; tv.tv_sec = (int) c->timeout; tv.tv_usec = (c->timeout - (double)tv.tv_sec) * 1000000.0; if (c->port < 1) c->rc = redisConnectUnixWithTimeout(c->host, tv); else c->rc = redisConnectWithTimeout(c->host, c->port, tv); if (!c->rc) { if (optional) return; Rf_error("disconnected connection and re-connect to redis failed (NULL context)"); } if (c->rc->err){ SEXP es = Rf_mkChar(c->rc->errstr); redisFree(c->rc); c->rc = 0; if (optional) return; Rf_error("disconnected connection and re-connect to redis failed: %s", CHAR(es)); } redisSetTimeout(c->rc, tv); /* re-connect succeeded */ } if (!c->rc && !optional) Rf_error("disconnected redis connection"); }
static redisContext* get_redis_context(ngx_http_request_t *r) { redisContext *context; repsheet_main_conf_t *conf; conf = ngx_http_get_module_main_conf(r, ngx_http_repsheet_module); struct timeval timeout = { 0, conf->redis.timeout }; context = redisConnectWithTimeout((char*)conf->redis.host.data, conf->redis.port, timeout); if (context == NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s %s Connection Error: can't allocate redis context", "[repsheet]"); return NULL; } if (context->err) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s Redis Connection Error: %s", "[repsheet]", context->errstr); redisFree(context); return NULL; } return context; }
redisContext* RedisConnPool::Connect() { struct timeval tv; tv.tv_sec = conn_timeout_ms_/1000; tv.tv_usec = (conn_timeout_ms_%1000)*1000; redisContext *c = redisConnectWithTimeout(host_.c_str(), port_, tv); if(c == NULL) { return NULL; } if(c->err) { log_error("connect redis failed. ip=[%s] port=[%d],c->strerr=%s", host_.c_str(), port_, c->errstr); redisFree(c); c = NULL; return NULL; } struct timeval rtv; rtv.tv_sec = recv_timeout_ms_/1000; rtv.tv_usec = (recv_timeout_ms_%1000)*1000; redisSetTimeout(c, rtv); return c; }
int32_t ClusterRedis::Init() { struct timeval timeout = { 1, 500000}; // 1.5 seconds _redisContext = redisConnectWithTimeout(_redisIP, _redisPort, timeout); if (!_redisContext || _redisContext->err) { if (_redisContext) { logg(ERROR, "fail to connect redis, ip: %s, port: %d, errorInfo: %s", _redisIP, _redisPort, _redisContext->errstr); } else { logg(ERROR, "fail to connect redis, ip: %s, port: %d", _redisIP, _redisPort); } return -2; } //test, connect Redis-server //if (this->Connect_Ping()) return -3; if (!is_master_) { _redisReply = (redisReply *)redisCommand(_redisContext, "readonly"); if (_redisReply) { if (_redisReply->type == REDIS_REPLY_STATUS && !strcmp(_redisReply->str, "OK")) { logg(DEBUG, "set to readonly"); readonly_ = true; return 0; } } return -2; } this->FreeSources(); return 0; }
redisContext* connect_to_redis() { redisContext* rc=NULL; struct timeval tv; tv.tv_sec=1000/1000; tv.tv_usec=1000*1000; rc=redisConnectWithTimeout(redis_host,redis_port,tv); return rc; }
p4ns_db_cxt_t p4ns_db_connect(char *ipv4, uint16_t port) { struct timeval timeout = { DB_CONNECT_TIMEOUT_SECS, 0 }; redisContext *c = redisConnectWithTimeout(ipv4, port, timeout); if (c == NULL || c->err) { return NULL; } return c; }
void init_redis_context(redisContext ** context,char * ip_port[]) { const char *hostname =ip_port[1]; int port = atoi(ip_port[2]); struct timeval timeout = { 1, 500000 }; // 1.5 seconds *context = redisConnectWithTimeout(hostname, port, timeout); }
int redisInit( void ) { struct timeval timeout = { 1, 500000 }; // 1.5 seconds redisC = redisConnectWithTimeout((char*)"127.0.0.1", 6379, timeout); if (redisC->err) { printf("Connection error: %s\n", redisC->errstr); return 1; } return 0; }
static int flapjack_push (char const *buffer, user_data_t *ud) /* {{{ */ { fj_node_t *node = ud->data; redisReply *rr; pthread_mutex_lock (&node->lock); if (node->conn == NULL) { node->conn = redisConnectWithTimeout ((char *)node->host, node->port, node->timeout); if (node->conn == NULL) { ERROR ("notify_flapjack plugin: Connecting to host \"%s\" (port %i) failed: Unkown reason", (node->host != NULL) ? node->host : "localhost", (node->port != 0) ? node->port : 6379); pthread_mutex_unlock (&node->lock); return (-1); } else if (node->conn->err) { ERROR ("notify_flapjack plugin: Connecting to host \"%s\" (port %i) failed: %s", (node->host != NULL) ? node->host : "localhost", (node->port != 0) ? node->port : 6379, node->conn->errstr); pthread_mutex_unlock (&node->lock); return (-1); } rr = redisCommand(node->conn, "SELECT %d", node->database); if (rr == NULL) WARNING("notify_flapjack plugin: %s:%d SELECT command error. database:%d message:%s", node->host, node->port, node->database, node->conn->errstr); else freeReplyObject (rr); } DEBUG("notify_flapjack plugin: %s:%d:%d LPUSH events %s", node->host, node->port, node->database, buffer); rr = redisCommand (node->conn, "LPUSH %s %s", "events", buffer); if (rr == NULL) WARNING("notify_flapjack plugin: %s:%d:%d LPUSH command error (events). message:%s", node->host, node->port, node->database, node->conn->errstr); else freeReplyObject (rr); if ( node->flapjack_version > 1) { rr = redisCommand (node->conn, "LPUSH events_actions +"); if (rr == NULL) WARNING("notify_flapjack plugin: %s:%d:%d LPUSH command error (events_actions). message:%s", node->host, node->port, node->database, node->conn->errstr); else freeReplyObject (rr); } pthread_mutex_unlock (&node->lock); return (0); } /* }}} int flpjack_push */
/** * Connects to redis service. Asserts on failure. */ static redisContext *connect_to_redis() { redisContext *c = NULL; c = redisConnectWithTimeout(REDIS_IP, REDIS_PORT, g_timeout); if (c == NULL) { zlog_err("%s:, failed to connect to redis", __func__); assert(0); } return(c); }
int mc_connect(mc_t *mc) { struct timeval timeout = { 1, 500000 }; // 1.5 seconds mc->mc = redisConnectWithTimeout(mc->ip, atoi(mc->port), timeout); if (mc->mc->err) { printf("Memcache connect error: %s\n", mc->mc->errstr); return -1; } return 0; }
bool RedisConnect() { if (sIfConSucess) { return true; } bool theRet = false; do { struct timeval timeout = { 2, 0 }; // 2 seconds redisContext_ = redisConnectWithTimeout(sRedis_IP, sRedisPort, timeout); if (!redisContext_ || redisContext_->err) { if (redisContext_) { printf("Redis context connect error \n"); } else { printf("Connection error: can't allocate redis context\n"); } theRet = false; break; } string auth = Format("auth %s", string(sRedisPassword)); redisReply* reply = static_cast<redisReply*>(redisCommand(redisContext_, auth.c_str())); RedisReplyObjectDeleter replyDeleter(reply); if (!reply || string(reply->str) != string("OK")) { printf("Redis auth error\n"); theRet = false; break; } theRet = true; sIfConSucess = true; printf("Connect Redis success\n"); } while (0); if (!theRet && redisContext_) { RedisErrorHandler(); } return theRet; }
int main(int argc, char* argv[]) { redisContext* g_rctx; redisReply *reply; struct timeval timeout = { 1, 500000 }; g_rctx = redisConnectWithTimeout("127.0.0.1", 6379, timeout); reply = redisCommand(g_rctx,"SET nfpid %d",getpid()); printf("%p\n",reply); while (1){ sleep(10); } }
int main(int argc, char* argv[]) { if(argc!=7) { fprintf(stderr,"wrong argument. please input arguments like '-h 127.0.0.1 -p 6522 -f libsina.so\r\n"); exit(-1); } parseOption(argc,argv); struct timeval timeout = { 1, 500 }; redis_c = redisConnectWithTimeout(ip, port, timeout); if (redis_c->err) { printf("Connection error: %s Port:%d\n", redis_c->errstr,port); exit(1); } char f_name[1024]; sprintf(f_name,"./lib%s.so",fm_name); FILE *f =fopen(f_name,"r"); char *p = malloc(FILEBUF); if(!p) { fprintf(stderr,"malloc failed.\r\n"); exit(1); } int fsize = get_file_size(f_name); int len=0; int i; int ch; for(i =0;i<fsize; i++) { ch = fgetc(f); len += sprintf(p+len,"%d,",ch); } /*char *cmd ="{\"time\":1349688926,\ \"ip\":\"127.0.0.1\",\ \"script\":\"test.php\",\ \"formula\":\"carsvm\",\ \"programmer\":\"shunli\",\ \"data\":{\ \"tag\":\"qiche\",\ \"parameter\":\"+1\t1:0.6\t2:0.3\"}}"; */ reply = redisCommand(redis_c,"load %s %s",fm_name,p); freeReplyObject(reply); /*reply = redisCommand(redis_c,"grun aaa %s",cmd); freeReplyObject(reply);*/ return 0; }
static void setupConnection() { struct timeval timeout = {1, 500000}; ctx=redisConnectWithTimeout(hostname,port,timeout); if (ctx==NULL ||ctx->err) { if (ctx) { printf("Connection error: %s\n",ctx->errstr); redisFree(ctx); } else { printf("Connection error: can't allocate redis context\n"); } exit(1); } printf("Connected to redis server\n"); }
static void test_invalid_timeout_errors(struct config config) { redisContext *c; test("Set error when an invalid timeout usec value is given to redisConnectWithTimeout: "); config.tcp.timeout.tv_sec = 0; config.tcp.timeout.tv_usec = 10000001; c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout); test_cond(c->err == REDIS_ERR_IO); redisFree(c); test("Set error when an invalid timeout sec value is given to redisConnectWithTimeout: "); config.tcp.timeout.tv_sec = (((LONG_MAX) - 999) / 1000) + 1; config.tcp.timeout.tv_usec = 0; c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout); test_cond(c->err == REDIS_ERR_IO); redisFree(c); }
bool Redis::Connect(string &host, int port, int timeout, string passwd, bool auth_enable) { this->host = host; this->port = port; this->timeout = timeout; this->passwd = passwd; this->auth_enable = auth_enable; struct timeval timeout_tv = {0, 0}; log4cpp::Category& _logger = log4cpp::Category::getInstance("MAIN_LOG"); timeout_tv.tv_sec = timeout; for(int i = 0; i < MAX_TRY_COUNT; i++){ connect_ = redisConnectWithTimeout(host.c_str(), port, timeout_tv); if(!connect_ || connect_->err){ continue; } //Maybe,the auth phase will be needed if(auth_enable){ redisReply* reply = NULL; redisSetTimeout(connect_, timeout_tv); reply = (redisReply*)redisCommand(connect_, "AUTH %s", passwd.c_str()); if (NULL == reply || REDIS_REPLY_ERROR == reply->type) { _logger.error("redis AUTH failed"); if (NULL != reply) { freeReplyObject(reply); } redisFree(connect_); connect_ = NULL; continue; } else { _logger.notice(" redis auth success"); freeReplyObject(reply); } } return RESULT_OK; } _logger.error("connect redis error"); return RESULT_ERROR; }
redisContext* redis_connect_from_conf(redisconf_t* c) { assert(c); struct timeval timeout; timeout.tv_sec = REDIS_TIMEOUT; timeout.tv_usec = 0; if (c->type == T_LOCAL) { return (redisContext*) redisConnectUnixWithTimeout(c->path, timeout); } else { return (redisContext*) redisConnectWithTimeout(c->server, c->port, timeout); } }
bool redis_init(int port) { log_info("Redis Initialized"); struct timeval timeout = { 3600, 0}; c=redisConnectWithTimeout((char*)"localhost", port, timeout); if(c->err) { fprintf(stderr, "Connection error: %s\n", c->errstr); return 0; } return 1; }
//连接Redis bool CRedisInstance::Open(const char* aszIP, int nPort) { struct timeval tv; //1.5分钟 tv.tv_sec = 1; tv.tv_usec = 500000; m_pRedisContext = redisConnectWithTimeout(aszIP, nPort, tv); if (m_pRedisContext ==NULL || 0 != m_pRedisContext->err) { return false; } m_enumConn = REDIS_CONN_RUNNING; m_i64LastTime = ::GetTickCount(); return true; }
RedisInterface::RedisInterface(std::string hostname, int port, unsigned int timeout) { struct timeval timeout_struct; timeout_struct.tv_sec = timeout / 1000;; timeout_struct.tv_usec = (timeout % 1000) * 1000; context_ = redisConnectWithTimeout(hostname.c_str(), port, timeout_struct); if (context_ == nullptr || context_->err) { if (context_) { std::string error = "Connection error: " + std::string(context_->errstr); throw std::runtime_error(error); } else { throw std::runtime_error("Connection error: cannot allocate redis context"); } } }
int main(void) { unsigned int i; redisContext *c; redisReply *reply; struct timeval timeout = { 1, 500000 }; // 1.5 seconds c = redisConnectWithTimeout((char*)"127.0.0.5", 22000, timeout); if (c->err) { printf("Connection error: %s\n", c->errstr); exit(1); } for(i=0; i<100*1000; i++){ reply = redisCommand(c,"SET %s %s", "foo", "hello world"); freeReplyObject(reply); } return 0; }
redisContext *vfs_connect() { const char *hostname = "127.0.0.1"; int port = 6379; struct timeval timeout = { 1, 500000 }; // 1.5 seconds redisContext *c = redisConnectWithTimeout(hostname, port, timeout); if (c == NULL || (c)->err) { if (c) { printf("Connection error: %s\n", (c)->errstr); redisFree(c); } else { printf("Connection error: can't allocate redis context\n"); } exit(1); } return c; }