8000 pool entry wrapper · phpredis/phpredis@e403d25 · GitHub
[go: up one dir, main page]

Skip to content

Commit e403d25

Browse files
committed
pool entry wrapper
1 parent a53bf38 commit e403d25

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

library.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ void redis_register_persistent_resource(zend_string *id, void *ptr, int le_id) {
103103
zend_register_persistent_resource(ZSTR_VAL(id), ZSTR_LEN(id), ptr, le_id);
104104
}
105105

106+
typedef struct {
107+
php_stream *stream;
108+
redis_sock_status status;
109+
} PoolEntry;
110+
111+
static void
112+
pool_entry_dtor(void *ptr)
113+
{
114+
if (ptr != NULL) {
115+
PoolEntry *pe = ptr;
116+
if (pe->stream != NULL) {
117+
php_stream_pclose(pe->stream);
118+
}
119+
}
120+
}
121+
106122
static ConnectionPool *
107123
redis_sock_get_connection_pool(RedisSock *redis_sock)
108124
{
@@ -121,7 +137,7 @@ redis_sock_get_connection_pool(RedisSock *redis_sock)
121137

122138
/* Create the pool and store it in our persistent list */
123139
pool = pecalloc(1, sizeof(*pool), 1);
124-
zend_llist_init(&pool->list, sizeof(php_stream *), NULL, 1);
140+
zend_llist_init(&pool->list, sizeof(PoolEntry), pool_entry_dtor, 1);
125141
redis_register_persistent_resource(persistent_id, pool, le_redis_pconnect);
126142

127143
zend_string_release(persistent_id);
@@ -2975,7 +2991,7 @@ static int
29752991
redis_sock_check_liveness(RedisSock *redis_sock)
29762992
{
29772993
char id[64], inbuf[4096];
2978-
int idlen, auth;
2994+
int idlen, auth = 0;
29792995
smart_string cmd = {0};
29802996
size_t len;
29812997

@@ -2993,8 +3009,10 @@ redis_sock_check_liveness(RedisSock *redis_sock)
29933009
return SUCCESS;
29943010
}
29953011

2996-
/* AUTH (if we need it) */
2997-
auth = redis_sock_append_auth(redis_sock, &cmd);
3012+
if (redis_sock->status != REDIS_SOCK_STATUS_READY) {
3013+
/* AUTH (if we need it) */
3014+
auth = redis_sock_append_auth(redis_sock, &cmd);
3015+
}
29983016

29993017
/* ECHO challenge/response */
30003018
idlen = redis_uniqid(id, sizeof(id));
@@ -3105,7 +3123,12 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock)
31053123
if (INI_INT("redis.pconnect.pooling_enabled")) {
31063124
p = redis_sock_get_connection_pool(redis_sock);
31073125
if (zend_llist_count(&p->list) > 0) {
3108-
redis_sock->stream = *(php_stream **)zend_llist_get_last(&p->list);
3126+
PoolEntry *pe = zend_llist_get_last(&p->list);
3127+
redis_sock->status = pe->status;
3128+
/* Move pointer */
3129+
redis_sock->stream = pe->stream;
3130+
pe->stream = NULL;
3131+
31093132
zend_llist_remove_tail(&p->list);
31103133

31113134
if (redis_sock_check_liveness(redis_sock) == SUCCESS) {
@@ -3237,7 +3260,8 @@ redis_sock_disconnect(RedisSock *redis_sock, int force, int is_reset_mode)
32373260
free_reply_callbacks(redis_sock);
32383261
if (p) p->nb_active--;
32393262
} else if (p) {
3240-
zend_llist_prepend_element(&p->list, &redis_sock->stream);
3263+
PoolEntry pe = { redis_sock->stream, redis_sock->status };
3264+
zend_llist_prepend_element(&p->list, &pe);
32413265
}
32423266
} else {
32433267
php_stream_close(redis_sock->stream);

0 commit comments

Comments
 (0)
0