Skip to content

Commit 32eb1c5

Browse files
author
Vitaliy Stepanyuk
committed
Reduce number of connections to one cluster node by shuffling nodes.
1 parent 31b0531 commit 32eb1c5

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

cluster_library.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -856,14 +856,21 @@ cluster_init_seeds(redisCluster *cluster, HashTable *ht_seeds) {
856856
char *str, *psep, key[1024];
857857
int key_len;
858858
zval **z_seed;
859+
int *seeds;
860+
size_t i, count;
861+
862+
count = zend_hash_num_elements(ht_seeds);
863+
seeds = emalloc(sizeof(int) * count);
864+
865+
for (i = 0; i < count; i++) seeds[i] = i;
866+
fyshuffle(seeds, count);
859867

860868
// Iterate our seeds array
861-
for(zend_hash_internal_pointer_reset(ht_seeds);
862-
zend_hash_has_more_elements(ht_seeds)==SUCCESS;
863-
zend_hash_move_forward(ht_seeds))
864-
{
869+
for (i = 0; i < count; i++) {
865870
// Grab seed string
866-
zend_hash_get_current_data(ht_seeds, (void**)&z_seed);
871+
if (zend_hash_index_find(ht_seeds, seeds[i], (void**)&z_seed) != SUCCESS) {
872+
continue;
873+
}
867874

868875
// Skip anything that isn't a string
869876
if(Z_TYPE_PP(z_seed)!=IS_STRING)
@@ -890,6 +897,8 @@ cluster_init_seeds(redisCluster *cluster, HashTable *ht_seeds) {
890897
sizeof(RedisSock*),NULL);
891898
}
892899

900+
efree(seeds);
901+
893902
// Success if at least one seed seems valid
894903
return zend_hash_num_elements(cluster->seeds) > 0 ? 0 : -1;
895904
}

cluster_library.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ int mbulk_resp_loop_zipdbl(RedisSock *redis_sock, zval *z_result,
458458
int mbulk_resp_loop_assoc(RedisSock *redis_sock, zval *z_result,
459459
long long count, void *ctx TSRMLS_DC);
460460

461+
static void fyshuffle(int *array, size_t len);
462+
461463
#endif
462464

463465
/* vim: set tabstop=4 softtabstops=4 noexpandtab shiftwidth=4: */

0 commit comments

Comments
 (0)