Skip to content

Commit 675d127

Browse files
yatsukhnenkomichael-grunder
authored andcommitted
Refactor connection pooling code.
Move logic of searching+creating connection pool resource to `redis_sock_get_connection_pool`. Don't close streams in ZEND_RSRC_DTOR_FUNC because all of them stored in `EG(persistent_list)` and will be destroyed by PHP engine.
1 parent c6519dd commit 675d127

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

library.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ extern zend_class_entry *redis_exception_ce;
4545

4646
extern int le_redis_pconnect;
4747

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+
4865
/* Helper to reselect the proper DB number when we reconnect */
4966
static int reselect_db(RedisSock *redis_sock TSRMLS_DC) {
5067
char *cmd, *response;
@@ -1701,20 +1718,17 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
17011718

17021719
if (redis_sock->persistent) {
17031720
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);
17091725
/* Check socket liveness using 0 second timeout */
17101726
if (php_stream_set_option(redis_sock->stream, PHP_STREAM_OPTION_CHECK_LIVENESS, 0, NULL) == PHP_STREAM_OPTION_RETURN_OK) {
17111727
redis_sock->status = REDIS_SOCK_STATUS_CONNECTED;
1712-
zend_string_release(persistent_id);
17131728
return SUCCESS;
17141729
}
17151730
php_stream_pclose(redis_sock->stream);
17161731
}
1717-
zend_string_release(persistent_id);
17181732

17191733
gettimeofday(&tv, NULL);
17201734
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)
18081822
if (force) {
18091823
php_stream_pclose(redis_sock->stream);
18101824
} 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);
18231827
}
18241828
} else {
18251829
php_stream_close(redis_sock->stream);

redis.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -716,20 +716,13 @@ static void add_class_constants(zend_class_entry *ce, int is_cluster TSRMLS_DC)
716716
zend_declare_class_constant_stringl(ce, "BEFORE", 6, "before", 6 TSRMLS_CC);
717717
}
718718

719-
static void
720-
redis_pconnect_dtor(void *ptr TSRMLS_DC)
721-
{
722-
php_stream_pclose(*(php_stream **)ptr);
723-
}
724-
725719
static ZEND_RSRC_DTOR_FUNC(redis_connections_pool_dtor)
726720
{
727721
#if (PHP_MAJOR_VERSION < 7)
728722
zend_resource *res = rsrc;
729723
#endif
730724

731725
if (res->ptr) {
732-
zend_llist_apply(res->ptr, redis_pconnect_dtor TSRMLS_CC);
733726
zend_llist_destroy(res->ptr);
734727
pefree(res->ptr, 1);
735728
}

0 commit comments

Comments
 (0)