refactoring

This commit is contained in:
Pavlo Yatsukhnenko
2016-12-13 15:47:35 +02:00
parent 75ff50236e
commit 37d8bfde06
2 changed files with 35 additions and 43 deletions
+20 -26
View File
@@ -432,35 +432,29 @@ static zend_always_inline int
redis_sock_get_instance(zval *id, RedisSock **redis_sock TSRMLS_DC, int no_throw)
{
zval *socket;
int resource_type = 0;
if (Z_TYPE_P(id) != IS_OBJECT || (socket = zend_hash_str_find(Z_OBJPROP_P(id),
"socket", sizeof("socket") - 1)) == NULL) {
// Throw an exception unless we've been requested not to
if(!no_throw) {
zend_throw_exception(redis_exception_ce, "Redis server went away",
0 TSRMLS_CC);
}
return -1;
}
#if (PHP_MAJOR_VERSION < 7)
int resource_type;
*redis_sock = (RedisSock *)zend_list_find(Z_LVAL_P(socket), &resource_type);
if (!*redis_sock || resource_type != le_redis_sock) {
#else
if (Z_RES_P(socket) == NULL ||
!(*redis_sock = (RedisSock *)Z_RES_P(socket)->ptr) ||
Z_RES_P(socket)->type != le_redis_sock
if (Z_TYPE_P(id) == IS_OBJECT &&
(socket = zend_hash_str_find(Z_OBJPROP_P(id), "socket", sizeof("socket") - 1)) != NULL
) {
#endif
// Throw an exception unless we've been requested not to
if(!no_throw) {
zend_throw_exception(redis_exception_ce, "Redis server went away",
0 TSRMLS_CC);
#if (PHP_MAJOR_VERSION < 7)
*redis_sock = (RedisSock *)zend_list_find(Z_LVAL_P(socket), &resource_type);
#else
*redis_sock = NULL;
if (Z_RES_P(socket) != NULL) {
*redis_sock = (RedisSock *)Z_RES_P(socket)->ptr;
resource_type = Z_RES_P(socket)->type;
}
#endif
if (*redis_sock && resource_type == le_redis_sock) {
return 0;
}
return -1;
}
return 0;
// Throw an exception unless we've been requested not to
if (!no_throw) {
zend_throw_exception(redis_exception_ce, "Redis server went away", 0 TSRMLS_CC);
}
return -1;
}
/**
@@ -781,7 +775,7 @@ PHP_REDIS_API int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) {
}
/* if there is a redis sock already we have to remove it from the list */
if (redis_sock_get(object, &redis_sock TSRMLS_CC, 1) > 0) {
if (redis_sock_get(object, &redis_sock TSRMLS_CC, 1) >= 0) {
if ((socket = zend_hash_str_find(Z_OBJPROP_P(object), "socket", sizeof("socket") - 1)) == NULL)
{
/* maybe there is a socket but the id isn't known.. what to do? */
+15 -17
View File
@@ -329,14 +329,10 @@ PHP_METHOD(RedisArray, __construct)
static void
ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, int cmd_len, zval *z_args, zval *z_new_target) {
zval *zp_tmp, z_tmp;
zval z_tmp, z_fun, *redis_inst, *z_callargs, *zp_tmp;
char *key = NULL; /* set to avoid "unused-but-set-variable" */
int i, key_len = 0;
zval *redis_inst;
zval z_fun, *z_callargs;
int i, key_len = 0, argc;
HashTable *h_args;
int argc;
zend_bool b_write_cmd = 0;
h_args = Z_ARRVAL_P(z_args);
@@ -400,19 +396,21 @@ ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, i
} else { /* call directly through. */
call_user_function(&redis_ce->function_table, redis_inst, &z_fun, return_value, argc, z_callargs);
/* check if we have an error. */
if(RA_CALL_FAILED(return_value,cmd) && ra->prev && !b_write_cmd) { /* there was an error reading, try with prev ring. */
/* Free previous return value */
zval_dtor(return_value);
if (!b_write_cmd) {
/* check if we have an error. */
if (ra->prev && RA_CALL_FAILED(return_value, cmd)) { /* there was an error reading, try with prev ring. */
/* Free previous return value */
zval_dtor(return_value);
/* ERROR, FALLBACK TO PREVIOUS RING and forward a reference to the first redis instance we were looking at. */
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra->prev, cmd, cmd_len, z_args, z_new_target?z_new_target:redis_inst);
}
/* ERROR, FALLBACK TO PREVIOUS RING and forward a reference to the first redis instance we were looking at. */
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra->prev, cmd, cmd_len, z_args, z_new_target ? z_new_target : redis_inst);
}
/* Autorehash if the key was found on the previous node if this is a read command and auto rehashing is on */
if(!RA_CALL_FAILED(return_value,cmd) && !b_write_cmd && z_new_target && ra->auto_rehash) { /* move key from old ring to new ring */
ra_move_key(key, key_len, redis_inst, z_new_target TSRMLS_CC);
}
/* Autorehash if the key was found on the previous node if this is a read command and auto rehashing is on */
if (ra->auto_rehash && z_new_target && !RA_CALL_FAILED(return_value, cmd)) { /* move key from old ring to new ring */
ra_move_key(key, key_len, redis_inst, z_new_target TSRMLS_CC);
}
}
}
/* cleanup */