Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
f25a5267
Commit
f25a5267
authored
Nov 03, 2010
by
Pieter Noordhuis
Browse files
Allow to connect using a unix socket from hiredis
parent
0ccb2c8d
Changes
4
Hide whitespace changes
Inline
Side-by-side
async.c
View file @
f25a5267
...
@@ -58,6 +58,13 @@ redisAsyncContext *redisAsyncConnect(const char *ip, int port) {
...
@@ -58,6 +58,13 @@ redisAsyncContext *redisAsyncConnect(const char *ip, int port) {
return
ac
;
return
ac
;
}
}
redisAsyncContext
*
redisAsyncConnectUnix
(
const
char
*
path
)
{
redisContext
*
c
=
redisConnectUnixNonBlock
(
path
);
redisAsyncContext
*
ac
=
redisAsyncInitialize
(
c
);
__redisAsyncCopyError
(
ac
);
return
ac
;
}
int
redisAsyncSetReplyObjectFunctions
(
redisAsyncContext
*
ac
,
redisReplyObjectFunctions
*
fn
)
{
int
redisAsyncSetReplyObjectFunctions
(
redisAsyncContext
*
ac
,
redisReplyObjectFunctions
*
fn
)
{
redisContext
*
c
=
&
(
ac
->
c
);
redisContext
*
c
=
&
(
ac
->
c
);
return
redisSetReplyObjectFunctions
(
c
,
fn
);
return
redisSetReplyObjectFunctions
(
c
,
fn
);
...
...
hiredis.c
View file @
f25a5267
...
@@ -667,6 +667,22 @@ redisContext *redisConnectNonBlock(const char *ip, int port) {
...
@@ -667,6 +667,22 @@ redisContext *redisConnectNonBlock(const char *ip, int port) {
return
c
;
return
c
;
}
}
redisContext
*
redisConnectUnix
(
const
char
*
path
)
{
redisContext
*
c
=
redisContextInit
();
c
->
flags
|=
REDIS_BLOCK
;
c
->
flags
|=
REDIS_CONNECTED
;
redisContextConnectUnix
(
c
,
path
);
return
c
;
}
redisContext
*
redisConnectUnixNonBlock
(
const
char
*
path
)
{
redisContext
*
c
=
redisContextInit
();
c
->
flags
&=
~
REDIS_BLOCK
;
c
->
flags
|=
REDIS_CONNECTED
;
redisContextConnectUnix
(
c
,
path
);
return
c
;
}
/* Set the replyObjectFunctions to use. Returns REDIS_ERR when the reader
/* Set the replyObjectFunctions to use. Returns REDIS_ERR when the reader
* was already initialized and the function set could not be re-set.
* was already initialized and the function set could not be re-set.
* Return REDIS_OK when they could be set. */
* Return REDIS_OK when they could be set. */
...
...
hiredis.h
View file @
f25a5267
...
@@ -120,6 +120,8 @@ int redisFormatCommandArgv(char **target, int argc, const char **argv, const siz
...
@@ -120,6 +120,8 @@ int redisFormatCommandArgv(char **target, int argc, const char **argv, const siz
redisContext
*
redisConnect
(
const
char
*
ip
,
int
port
);
redisContext
*
redisConnect
(
const
char
*
ip
,
int
port
);
redisContext
*
redisConnectNonBlock
(
const
char
*
ip
,
int
port
);
redisContext
*
redisConnectNonBlock
(
const
char
*
ip
,
int
port
);
redisContext
*
redisConnectUnix
(
const
char
*
path
);
redisContext
*
redisConnectUnixNonBlock
(
const
char
*
path
);
int
redisSetReplyObjectFunctions
(
redisContext
*
c
,
redisReplyObjectFunctions
*
fn
);
int
redisSetReplyObjectFunctions
(
redisContext
*
c
,
redisReplyObjectFunctions
*
fn
);
void
redisFree
(
redisContext
*
c
);
void
redisFree
(
redisContext
*
c
);
int
redisBufferRead
(
redisContext
*
c
);
int
redisBufferRead
(
redisContext
*
c
);
...
...
test.c
View file @
f25a5267
...
@@ -19,9 +19,11 @@ static long long usec(void) {
...
@@ -19,9 +19,11 @@ static long long usec(void) {
return
(((
long
long
)
tv
.
tv_sec
)
*
1000000
)
+
tv
.
tv_usec
;
return
(((
long
long
)
tv
.
tv_sec
)
*
1000000
)
+
tv
.
tv_usec
;
}
}
static
int
use_unix
=
0
;
static
redisContext
*
blocking_context
=
NULL
;
static
redisContext
*
blocking_context
=
NULL
;
static
void
__connect
(
redisContext
**
target
)
{
static
void
__connect
(
redisContext
**
target
)
{
*
target
=
blocking_context
=
redisConnect
((
char
*
)
"127.0.0.1"
,
6379
);
*
target
=
blocking_context
=
(
use_unix
?
redisConnectUnix
(
"/tmp/redis.sock"
)
:
redisConnect
((
char
*
)
"127.0.0.1"
,
6379
));
if
(
blocking_context
->
err
)
{
if
(
blocking_context
->
err
)
{
printf
(
"Connection error: %s
\n
"
,
blocking_context
->
errstr
);
printf
(
"Connection error: %s
\n
"
,
blocking_context
->
errstr
);
exit
(
1
);
exit
(
1
);
...
@@ -77,10 +79,22 @@ static void test_blocking_connection() {
...
@@ -77,10 +79,22 @@ static void test_blocking_connection() {
__connect
(
&
c
);
__connect
(
&
c
);
test
(
"Returns I/O error when the connection is lost: "
);
test
(
"Returns I/O error when the connection is lost: "
);
reply
=
redisCommand
(
c
,
"QUIT"
);
reply
=
redisCommand
(
c
,
"QUIT"
);
test_cond
(
strcasecmp
(
reply
->
str
,
"OK"
)
==
0
&&
test_cond
(
strcasecmp
(
reply
->
str
,
"OK"
)
==
0
&&
redisCommand
(
c
,
"PING"
)
==
NULL
);
redisCommand
(
c
,
"PING"
)
==
NULL
&&
c
->
err
==
REDIS_ERR_EOF
&&
/* Two conditions may happen, depending on the type of connection.
strcmp
(
c
->
errstr
,
"Server closed the connection"
)
==
0
);
* When connected via TCP, the socket will not yet be aware of the closed
* connection and the write(2) call will succeed, but the read(2) will
* result in an EOF. When connected via Unix sockets, the socket will be
* immediately aware that it was closed and fail on the write(2) call. */
if
(
use_unix
)
{
fprintf
(
stderr
,
"Error: %s
\n
"
,
c
->
errstr
);
assert
(
c
->
err
==
REDIS_ERR_IO
&&
strcmp
(
c
->
errstr
,
"Broken pipe"
)
==
0
);
}
else
{
fprintf
(
stderr
,
"Error: %s
\n
"
,
c
->
errstr
);
assert
(
c
->
err
==
REDIS_ERR_EOF
&&
strcmp
(
c
->
errstr
,
"Server closed the connection"
)
==
0
);
}
freeReplyObject
(
reply
);
freeReplyObject
(
reply
);
redisFree
(
c
);
redisFree
(
c
);
...
@@ -210,7 +224,7 @@ static void test_throughput() {
...
@@ -210,7 +224,7 @@ static void test_throughput() {
replies
=
malloc
(
sizeof
(
redisReply
*
)
*
1000
);
replies
=
malloc
(
sizeof
(
redisReply
*
)
*
1000
);
t1
=
usec
();
t1
=
usec
();
for
(
i
=
0
;
i
<
1000
;
i
++
)
replies
[
i
]
=
redisCommand
(
c
,
"PING"
);
for
(
i
=
0
;
i
<
1000
;
i
++
)
assert
((
replies
[
i
]
=
redisCommand
(
c
,
"PING"
)
)
!=
NULL
)
;
t2
=
usec
();
t2
=
usec
();
for
(
i
=
0
;
i
<
1000
;
i
++
)
freeReplyObject
(
replies
[
i
]);
for
(
i
=
0
;
i
<
1000
;
i
++
)
freeReplyObject
(
replies
[
i
]);
free
(
replies
);
free
(
replies
);
...
@@ -218,7 +232,7 @@ static void test_throughput() {
...
@@ -218,7 +232,7 @@ static void test_throughput() {
replies
=
malloc
(
sizeof
(
redisReply
*
)
*
1000
);
replies
=
malloc
(
sizeof
(
redisReply
*
)
*
1000
);
t1
=
usec
();
t1
=
usec
();
for
(
i
=
0
;
i
<
1000
;
i
++
)
replies
[
i
]
=
redisCommand
(
c
,
"LRANGE mylist 0 499"
);
for
(
i
=
0
;
i
<
1000
;
i
++
)
assert
((
replies
[
i
]
=
redisCommand
(
c
,
"LRANGE mylist 0 499"
)
)
!=
NULL
)
;
t2
=
usec
();
t2
=
usec
();
for
(
i
=
0
;
i
<
1000
;
i
++
)
freeReplyObject
(
replies
[
i
]);
for
(
i
=
0
;
i
<
1000
;
i
++
)
freeReplyObject
(
replies
[
i
]);
free
(
replies
);
free
(
replies
);
...
@@ -337,6 +351,7 @@ static void cleanup() {
...
@@ -337,6 +351,7 @@ static void cleanup() {
// }
// }
int
main
(
void
)
{
int
main
(
void
)
{
signal
(
SIGPIPE
,
SIG_IGN
);
test_format_commands
();
test_format_commands
();
test_blocking_connection
();
test_blocking_connection
();
test_reply_reader
();
test_reply_reader
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment