@@ -45,6 +45,23 @@ extern zend_class_entry *redis_exception_ce;
45
45
46
46
extern int le_redis_pconnect ;
47
47
48
+ static zend_llist *
49
+ redis_sock_get_connection_pool (RedisSock * redis_sock )
50
+ {
51
+ zend_string * persistent_id = strpprintf (0 , "phpredis_%s:%d" , ZSTR_VAL (redis_sock -> host ), redis_sock -> port );
52
+ zend_resource * le = zend_hash_find_ptr (& EG (persistent_list ), persistent_id );
53
+ if (!le ) {
54
+ zend_llist * list = pecalloc (1 , sizeof (* list ) + sizeof (* le ), 1 );
55
+ zend_llist_init (list , sizeof (php_stream * ), NULL , 1 );
56
+ le = (zend_resource * )((char * )list + sizeof (* list ));
57
+ le -> type = le_redis_pconnect ;
58
+ le -> ptr = list ;
59
+ zend_hash_str_update_mem (& EG (persistent_list ), ZSTR_VAL (persistent_id ), ZSTR_LEN (persistent_id ), le , sizeof (* le ));
60
+ }
61
+ zend_string_release (persistent_id );
62
+ return le -> ptr ;
63
+ }
64
+
48
65
/* Helper to reselect the proper DB number when we reconnect */
49
66
static int reselect_db (RedisSock * redis_sock TSRMLS_DC ) {
50
67
char * cmd , * response ;
@@ -1701,20 +1718,17 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
1701
1718
1702
1719
if (redis_sock -> persistent ) {
1703
1720
if (INI_INT ("redis.pconnect.pooling_enabled" )) {
1704
- persistent_id = strpprintf (0 , "phpredis_%s:%d" , ZSTR_VAL (redis_sock -> host ), redis_sock -> port );
1705
- zend_resource * le = zend_hash_find_ptr (& EG (persistent_list ), persistent_id );
1706
- if (le && zend_llist_count (le -> ptr ) > 0 ) {
1707
- redis_sock -> stream = * (php_stream * * )zend_llist_get_last (le -> ptr );
1708
- zend_llist_remove_tail (le -> ptr );
1721
+ zend_llist * list = redis_sock_get_connection_pool (redis_sock );
1722
+ if (zend_llist_count (list ) > 0 ) {
1723
+ redis_sock -> stream = * (php_stream * * )zend_llist_get_last (list );
1724
+ zend_llist_remove_tail (list );
1709
1725
/* Check socket liveness using 0 second timeout */
1710
1726
if (php_stream_set_option (redis_sock -> stream , PHP_STREAM_OPTION_CHECK_LIVENESS , 0 , NULL ) == PHP_STREAM_OPTION_RETURN_OK ) {
1711
1727
redis_sock -> status = REDIS_SOCK_STATUS_CONNECTED ;
1712
- zend_string_release (persistent_id );
1713
1728
return SUCCESS ;
1714
1729
}
1715
1730
php_stream_pclose (redis_sock -> stream );
1716
1731
}
1717
- zend_string_release (persistent_id );
1718
1732
1719
1733
gettimeofday (& tv , NULL );
1720
1734
persistent_id = strpprintf (0 , "phpredis_%d%d" , tv .tv_sec , tv .tv_usec );
@@ -1808,18 +1822,8 @@ redis_sock_disconnect(RedisSock *redis_sock, int force TSRMLS_DC)
1808
1822
if (force ) {
1809
1823
php_stream_pclose (redis_sock -> stream );
1810
1824
} else if (INI_INT ("redis.pconnect.pooling_enabled" )) {
1811
- zend_string * persistent_id = strpprintf (0 , "phpredis_%s:%d" , ZSTR_VAL (redis_sock -> host ), redis_sock -> port );
1812
- zend_resource * le = zend_hash_find_ptr (& EG (persistent_list ), persistent_id );
1813
- if (!le ) {
1814
- zend_llist * l = pecalloc (1 , sizeof (* l ) + sizeof (* le ), 1 );
1815
- zend_llist_init (l , sizeof (php_stream * ), NULL , 1 );
1816
- le = (zend_resource * )((char * )l + sizeof (* l ));
1817
- le -> type = le_redis_pconnect ;
1818
- le -> ptr = l ;
1819
- zend_hash_str_update_mem (& EG (persistent_list ), ZSTR_VAL (persistent_id ), ZSTR_LEN (persistent_id ), le , sizeof (* le ));
1820
- }
1821
- zend_llist_prepend_element (le -> ptr , & redis_sock -> stream );
1822
- zend_string_release (persistent_id );
1825
+ zend_llist * list = redis_sock_get_connection_pool (redis_sock );
1826
+ zend_llist_prepend_element (list , & redis_sock -> stream );
1823
1827
}
1824
1828
} else {
1825
1829
php_stream_close (redis_sock -> stream );
0 commit comments