Skip to content

Commit 44f048c

Browse files
committed
Added support for UNIX Domain Sockets.
1 parent 1268120 commit 44f048c

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

README.markdown

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ Connects to a Redis instance.
7272

7373
##### *Parameters*
7474

75-
*host*: string
76-
*port*: int
75+
*host*: string. can be a host, or the path to a unix domain socket
76+
*port*: int, optional
7777
*timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
7878

7979
##### *Return Value*
@@ -83,7 +83,9 @@ Connects to a Redis instance.
8383
##### *Example*
8484

8585
$redis->connect('127.0.0.1', 6379);
86+
$redis->connect('127.0.0.1'); // port 6379 by default
8687
$redis->connect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout.
88+
$redis->connect('/tmp/redis.sock'); // unix domain socket.
8789

8890
## ping
8991
##### *Description*

common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ typedef struct request_item {
138138
typedef struct {
139139
php_stream *stream;
140140
char *host;
141-
unsigned short port;
141+
short port;
142142
double timeout;
143143
int failed;
144144
int status;

library.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -722,17 +722,20 @@ PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)
722722

723723
tv.tv_sec = (time_t)redis_sock->timeout;
724724
tv.tv_usec = (int)((redis_sock->timeout - tv.tv_sec) * 1000000);
725-
726-
host_len = spprintf(&host, 0, "%s:%d", redis_sock->host, redis_sock->port);
727-
728725
if(tv.tv_sec != 0 || tv.tv_usec != 0) {
729-
tv_ptr = &tv;
726+
tv_ptr = &tv;
727+
}
728+
729+
if(redis_sock->host[0] == '/' && redis_sock->port < 1) {
730+
host_len = spprintf(&host, 0, "unix://%s", redis_sock->host);
731+
} else {
732+
host_len = spprintf(&host, 0, "%s:%d", redis_sock->host, redis_sock->port);
730733
}
731734
redis_sock->stream = php_stream_xport_create(host, host_len, ENFORCE_SAFE_MODE,
732-
STREAM_XPORT_CLIENT
733-
| STREAM_XPORT_CONNECT,
734-
hash_key, tv_ptr, NULL, &errstr, &err
735-
);
735+
STREAM_XPORT_CLIENT
736+
| STREAM_XPORT_CONNECT,
737+
hash_key, tv_ptr, NULL, &errstr, &err
738+
);
736739

737740
efree(host);
738741

redis.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,12 @@ PHP_METHOD(Redis, connect)
357357
zval *object;
358358
int host_len, id;
359359
char *host = NULL;
360-
long port;
360+
long port = -1;
361361

362362
double timeout = 0.0;
363363
RedisSock *redis_sock = NULL;
364364

365-
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osl|d",
365+
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|ld",
366366
&object, redis_ce, &host, &host_len, &port,
367367
&timeout) == FAILURE) {
368368
RETURN_FALSE;
@@ -373,6 +373,10 @@ PHP_METHOD(Redis, connect)
373373
RETURN_FALSE;
374374
}
375375

376+
if(port == -1 && host_len && host[0] != '/') { /* not unix socket, set to default value */
377+
port = 6379;
378+
}
379+
376380
redis_sock = redis_sock_create(host, host_len, port, timeout);
377381

378382
if (redis_sock_server_open(redis_sock, 1 TSRMLS_CC) < 0) {

0 commit comments

Comments
 (0)