Skip to content

Commit

Permalink
Added support for UNIX Domain Sockets.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasff committed Dec 7, 2010
1 parent 1268120 commit 44f048c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
6 changes: 4 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ Connects to a Redis instance.

##### *Parameters*

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

##### *Return Value*
Expand All @@ -83,7 +83,9 @@ Connects to a Redis instance.
##### *Example*

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

## ping
##### *Description*
Expand Down
2 changes: 1 addition & 1 deletion common.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ typedef struct request_item {
typedef struct {
php_stream *stream;
char *host;
unsigned short port;
short port;
double timeout;
int failed;
int status;
Expand Down
19 changes: 11 additions & 8 deletions library.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,17 +722,20 @@ PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC)

tv.tv_sec = (time_t)redis_sock->timeout;
tv.tv_usec = (int)((redis_sock->timeout - tv.tv_sec) * 1000000);

host_len = spprintf(&host, 0, "%s:%d", redis_sock->host, redis_sock->port);

if(tv.tv_sec != 0 || tv.tv_usec != 0) {
tv_ptr = &tv;
tv_ptr = &tv;
}

if(redis_sock->host[0] == '/' && redis_sock->port < 1) {
host_len = spprintf(&host, 0, "unix://%s", redis_sock->host);
} else {
host_len = spprintf(&host, 0, "%s:%d", redis_sock->host, redis_sock->port);
}
redis_sock->stream = php_stream_xport_create(host, host_len, ENFORCE_SAFE_MODE,
STREAM_XPORT_CLIENT
| STREAM_XPORT_CONNECT,
hash_key, tv_ptr, NULL, &errstr, &err
);
STREAM_XPORT_CLIENT
| STREAM_XPORT_CONNECT,
hash_key, tv_ptr, NULL, &errstr, &err
);

efree(host);

Expand Down
8 changes: 6 additions & 2 deletions redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,12 @@ PHP_METHOD(Redis, connect)
zval *object;
int host_len, id;
char *host = NULL;
long port;
long port = -1;

double timeout = 0.0;
RedisSock *redis_sock = NULL;

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

if(port == -1 && host_len && host[0] != '/') { /* not unix socket, set to default value */
port = 6379;
}

redis_sock = redis_sock_create(host, host_len, port, timeout);

if (redis_sock_server_open(redis_sock, 1 TSRMLS_CC) < 0) {
Expand Down

0 comments on commit 44f048c

Please sign in to comment.