/* Internal methods */ static redisContext* redis_connection(struct redis_handler *handle) { redisContext* context = NULL; struct timeval timeout = { 1, 500000 }; // 1.5 seconds context = redisConnectWithTimeout((char*)handle->redis_ip, handle->redis_port, timeout); if (context == NULL || context->err) { if (context != NULL) { redisFree(context); } return NULL; } /* make keepalive connection */ if (redisEnableKeepAlive(context) != REDIS_OK){ if (context != NULL) { redisFree(context); } return NULL; } return context; }
/* Internal host connect - Sync or Async */ static int _host_connect( host_t *h, eredis_reader_t *r ) { redisContext *c; if (r) { /* Sync - not in EV context */ c = (h->port) ? redisConnect( h->target, h->port ) : redisConnectUnix( h->target ); if (! c) { fprintf(stderr, "eredis: error: connect sync %s NULL\n", h->target); return 0; } if (c->err) { #if EREDIS_VERBOSE>0 printf( "eredis: error: connect sync %s %d\n", h->target, c->err); #endif redisFree( c ); return 0; } r->ctx = c; r->host = h; } else { redisAsyncContext *ac; /* ASync - in EV context */ ac = (h->port) ? redisAsyncConnect( h->target, h->port ) : redisAsyncConnectUnix( h->target ); if (! ac) { printf( "eredis: error: connect async %s undef\n", h->target); return 0; } if (ac->err) { #if EREDIS_VERBOSE>0 printf( "eredis: error: connect async %s %d\n", h->target, ac->err); #endif redisAsyncFree( ac ); return 0; } h->async_ctx = ac; /* data for _redis_*_cb */ ac->data = h; /* Order is important here */ /* attach */ redisLibevAttach( h->e->loop, ac ); /* set callbacks */ redisAsyncSetDisconnectCallback( ac, _redis_disconnect_cb ); redisAsyncSetConnectCallback( ac, _redis_connect_cb ); c = (redisContext*) ac; } /* Apply keep-alive */ #ifdef HOST_TCP_KEEPALIVE if (h->port) { redisEnableKeepAlive( c ); if (r && (h->e->sync_to.tv_sec||h->e->sync_to.tv_usec)) { redisSetTimeout( c, h->e->sync_to ); } } #endif /* Override the maxbuf */ c->reader->maxbuf = EREDIS_READER_MAX_BUF; return 1; }