Skip to content

Commit ab9a88e

Browse files
committed
added pconnect, has to be tested and checked
1 parent a35396e commit ab9a88e

File tree

6 files changed

+84
-47
lines changed

6 files changed

+84
-47
lines changed

common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ typedef struct {
137137
double timeout;
138138
int failed;
139139
int status;
140+
int persistent;
140141

141142
redis_mode mode;
142143
fold_item *head;

library.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis
696696
* redis_sock_create
697697
*/
698698
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port,
699-
double timeout)
699+
double timeout, int persistent)
700700
{
701701
RedisSock *redis_sock;
702702

@@ -705,6 +705,8 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
705705
redis_sock->stream = NULL;
706706
redis_sock->status = REDIS_SOCK_STATUS_DISCONNECTED;
707707

708+
redis_sock->persistent = persistent;
709+
708710
memcpy(redis_sock->host, host, host_len);
709711
redis_sock->host[host_len] = '\0';
710712

@@ -726,7 +728,7 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
726728
PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
727729
{
728730
struct timeval tv, *tv_ptr = NULL;
729-
char *host = NULL, *hash_key = NULL, *errstr = NULL;
731+
char *host = NULL, *persistent_id = NULL, *errstr = NULL;
730732
int host_len, err = 0;
731733

732734
if (redis_sock->stream != NULL) {
@@ -744,12 +746,21 @@ PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
744746
} else {
745747
host_len = spprintf(&host, 0, "%s:%d", redis_sock->host, redis_sock->port);
746748
}
749+
750+
if (redis_sock->persistent) {
751+
spprintf(&persistent_id, 0, "%s:%f", host, redis_sock->timeout);
752+
}
753+
747754
redis_sock->stream = php_stream_xport_create(host, host_len, ENFORCE_SAFE_MODE,
748755
STREAM_XPORT_CLIENT
749756
| STREAM_XPORT_CONNECT,
750-
hash_key, tv_ptr, NULL, &errstr, &err
757+
persistent_id, tv_ptr, NULL, &errstr, &err
751758
);
752759

760+
if (persistent_id) {
761+
efree(persistent_id);
762+
}
763+
753764
efree(host);
754765

755766
if (!redis_sock->stream) {
@@ -814,15 +825,17 @@ PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC)
814825
}
815826

816827
if (redis_sock->stream != NULL) {
817-
redis_sock_write(redis_sock, "QUIT", sizeof("QUIT") - 1 TSRMLS_CC);
828+
if (!redis_sock->persistent) {
829+
redis_sock_write(redis_sock, "QUIT", sizeof("QUIT") - 1 TSRMLS_CC);
830+
}
818831

819-
redis_sock->status = REDIS_SOCK_STATUS_DISCONNECTED;
820-
if(redis_sock->stream) { /* still valid after the write? */
821-
php_stream_close(redis_sock->stream);
822-
}
823-
redis_sock->stream = NULL;
832+
redis_sock->status = REDIS_SOCK_STATUS_DISCONNECTED;
833+
if(redis_sock->stream && !redis_sock->persistent) { /* still valid after the write? */
834+
php_stream_close(redis_sock->stream);
835+
}
836+
redis_sock->stream = NULL;
824837

825-
return 1;
838+
return 1;
826839
}
827840

828841
return 0;

library.h

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

php_redis.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
PHP_METHOD(Redis, __construct);
2727
PHP_METHOD(Redis, connect);
28+
PHP_METHOD(Redis, pconnect);
2829
PHP_METHOD(Redis, close);
2930
PHP_METHOD(Redis, ping);
3031
PHP_METHOD(Redis, get);
@@ -155,6 +156,7 @@ PHP_RINIT_FUNCTION(redis);
155156
PHP_RSHUTDOWN_FUNCTION(redis);
156157
PHP_MINFO_FUNCTION(redis);
157158

159+
PHPAPI int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent);
158160
PHPAPI void redis_atomic_increment(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int count);
159161
PHPAPI int generic_multiple_args_cmd(INTERNAL_FUNCTION_PARAMETERS, char *keyword, int keyword_len,
160162
int min_argc, RedisSock **redis_sock, int has_timeout);

redis.c

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ZEND_DECLARE_MODULE_GLOBALS(redis)
4949
static zend_function_entry redis_functions[] = {
5050
PHP_ME(Redis, __construct, NULL, ZEND_ACC_PUBLIC)
5151
PHP_ME(Redis, connect, NULL, ZEND_ACC_PUBLIC)
52+
PHP_ME(Redis, pconnect, NULL, ZEND_ACC_PUBLIC)
5253
PHP_ME(Redis, close, NULL, ZEND_ACC_PUBLIC)
5354
PHP_ME(Redis, ping, NULL, ZEND_ACC_PUBLIC)
5455
PHP_ME(Redis, get, NULL, ZEND_ACC_PUBLIC)
@@ -348,29 +349,50 @@ PHP_METHOD(Redis, __construct)
348349
*/
349350
PHP_METHOD(Redis, connect)
350351
{
351-
zval *object;
352+
if (redis_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) == FAILURE) {
353+
RETURN_FALSE;
354+
} else {
355+
RETURN_TRUE;
356+
}
357+
}
358+
/* }}} */
359+
360+
/* {{{ proto boolean Redis::pconnect(string host, int port [, double timeout])
361+
*/
362+
PHP_METHOD(Redis, pconnect)
363+
{
364+
if (redis_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1) == FAILURE) {
365+
RETURN_FALSE;
366+
} else {
367+
RETURN_TRUE;
368+
}
369+
}
370+
/* }}} */
371+
372+
PHPAPI int redis_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) {
373+
zval *object;
352374
zval **socket;
353-
int host_len, id;
354-
char *host = NULL;
355-
long port = -1;
375+
int host_len, id;
376+
char *host = NULL;
377+
long port = -1;
356378

357-
double timeout = 0.0;
358-
RedisSock *redis_sock = NULL;
379+
double timeout = 0.0;
380+
RedisSock *redis_sock = NULL;
359381

360-
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|ld",
361-
&object, redis_ce, &host, &host_len, &port,
362-
&timeout) == FAILURE) {
363-
RETURN_FALSE;
364-
}
382+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|ld",
383+
&object, redis_ce, &host, &host_len, &port,
384+
&timeout) == FAILURE) {
385+
return FAILURE;
386+
}
365387

366-
if (timeout < 0L || timeout > INT_MAX) {
367-
zend_throw_exception(redis_exception_ce, "Invalid timeout", 0 TSRMLS_CC);
368-
RETURN_FALSE;
369-
}
388+
if (timeout < 0L || timeout > INT_MAX) {
389+
zend_throw_exception(redis_exception_ce, "Invalid timeout", 0 TSRMLS_CC);
390+
return FAILURE;
391+
}
370392

371-
if(port == -1 && host_len && host[0] != '/') { /* not unix socket, set to default value */
372-
port = 6379;
373-
}
393+
if(port == -1 && host_len && host[0] != '/') { /* not unix socket, set to default value */
394+
port = 6379;
395+
}
374396

375397
/* if there is a redis sock already we have to remove it from the list */
376398
if (redis_sock_get(object, &redis_sock TSRMLS_CC) > 0) {
@@ -382,26 +404,25 @@ PHP_METHOD(Redis, connect)
382404
}
383405
}
384406

385-
redis_sock = redis_sock_create(host, host_len, port, timeout);
386-
387-
if (redis_sock_server_open(redis_sock, 1 TSRMLS_CC) < 0) {
388-
redis_free_socket(redis_sock);
389-
zend_throw_exception_ex(
390-
redis_exception_ce,
391-
0 TSRMLS_CC,
392-
"Can't connect to %s:%d",
393-
host,
394-
port
395-
);
396-
RETURN_FALSE;
397-
}
407+
redis_sock = redis_sock_create(host, host_len, port, timeout, persistent);
408+
409+
if (redis_sock_server_open(redis_sock, 1 TSRMLS_CC) < 0) {
410+
redis_free_socket(redis_sock);
411+
zend_throw_exception_ex(
412+
redis_exception_ce,
413+
0 TSRMLS_CC,
414+
"Can't connect to %s:%d",
415+
host,
416+
port
417+
);
418+
return FAILURE;
419+
}
398420

399-
id = zend_list_insert(redis_sock, le_redis_sock);
400-
add_property_resource(object, "socket", id);
421+
id = zend_list_insert(redis_sock, le_redis_sock);
422+
add_property_resource(object, "socket", id);
401423

402-
RETURN_TRUE;
424+
return SUCCESS;
403425
}
404-
/* }}} */
405426

406427
/* {{{ proto boolean Redis::close()
407428
*/

redis_session.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ PS_OPEN_FUNC(redis)
194194
return FAILURE;
195195
}
196196

197-
RedisSock *redis_sock = redis_sock_create(url->host, strlen(url->host), url->port, timeout);
197+
RedisSock *redis_sock = redis_sock_create(url->host, strlen(url->host), url->port, timeout, 0);
198198
redis_pool_add(pool, redis_sock, weight TSRMLS_CC);
199199

200200
php_url_free(url);

0 commit comments

Comments
 (0)