Skip to content

Commit c561e89

Browse files
committed
Added support for user specified persistent connection id
1 parent a086b0e commit c561e89

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ typedef struct {
147147
int failed;
148148
int status;
149149
int persistent;
150+
char *persistent_id;
150151

151152
int serializer;
152153

library.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,8 @@ PHPAPI void redis_ping_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_s
754754
* redis_sock_create
755755
*/
756756
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port,
757-
double timeout, int persistent)
757+
double timeout, int persistent, char *persistent_id,
758+
int persistent_id_len)
758759
{
759760
RedisSock *redis_sock;
760761

@@ -765,6 +766,13 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
765766

766767
redis_sock->persistent = persistent;
767768

769+
if (persistent_id) {
770+
memcpy(redis_sock->persistent_id, persistent_id, persistent_id_len);
771+
redis_sock->persistent_id[persistent_id_len] = '\0';
772+
} else {
773+
redis_sock->persistent_id = NULL;
774+
}
775+
768776
memcpy(redis_sock->host, host, host_len);
769777
redis_sock->host[host_len] = '\0';
770778

@@ -807,7 +815,11 @@ PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
807815
}
808816

809817
if (redis_sock->persistent) {
810-
spprintf(&persistent_id, 0, "%s:%f", host, redis_sock->timeout);
818+
if (redis_sock->persistent_id) {
819+
spprintf(&persistent_id, 0, "%s:%s", host, redis_sock->persistent_id);
820+
} else {
821+
spprintf(&persistent_id, 0, "%s:%f", host, redis_sock->timeout);
822+
}
811823
}
812824

813825
redis_sock->stream = php_stream_xport_create(host, host_len, ENFORCE_SAFE_MODE,

library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis
1414
PHPAPI void redis_ping_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
1515
PHPAPI void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
1616
PHPAPI void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
17-
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout, int persistent);
17+
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout, int persistent, char *persistent_id, int persistent_id_len);
1818
PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC);
1919
PHPAPI int redis_sock_server_open(RedisSock *redis_sock, int force_connect TSRMLS_DC);
2020
PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC);

redis.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ PHPAPI int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) {
412412
char *host = NULL;
413413
long port = -1;
414414

415+
char *persistent_id = NULL;
416+
int persistent_id_len = -1;
417+
415418
#ifdef ZTS
416419
/* not sure how in threaded mode this works so disabled persistents at first */
417420
persistent = 0;
@@ -420,9 +423,9 @@ PHPAPI int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) {
420423
double timeout = 0.0;
421424
RedisSock *redis_sock = NULL;
422425

423-
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|ld",
424-
&object, redis_ce, &host, &host_len, &port,
425-
&timeout) == FAILURE) {
426+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|lds",
427+
&object, redis_ce, &host, &host_len, &port
428+
&timeout, &persistent_id, &persistent_id_len) == FAILURE) {
426429
return FAILURE;
427430
}
428431

@@ -445,7 +448,7 @@ PHPAPI int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) {
445448
}
446449
}
447450

448-
redis_sock = redis_sock_create(host, host_len, port, timeout, persistent);
451+
redis_sock = redis_sock_create(host, host_len, port, timeout, persistent, persistent_id, persistent_id_len);
449452

450453
if (redis_sock_server_open(redis_sock, 1 TSRMLS_CC) < 0) {
451454
redis_free_socket(redis_sock);

0 commit comments

Comments
 (0)