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
0f745d1a
Commit
0f745d1a
authored
Oct 19, 2010
by
Pieter Noordhuis
Browse files
Run pending callbacks with NULL reply on redisDisconnect()
parent
bbe007a7
Changes
4
Hide whitespace changes
Inline
Side-by-side
hiredis.c
View file @
0f745d1a
...
...
@@ -587,6 +587,21 @@ static redisContext *redisContextInit(redisReplyFunctions *fn) {
}
void
redisDisconnect
(
redisContext
*
c
)
{
int
i
;
redisCallback
cb
;
/* Non-blocking context: call pending callbacks with the NULL reply */
if
(
!
(
c
->
flags
&
REDIS_BLOCK
))
{
for
(
i
=
0
;
i
<
c
->
cpos
;
i
++
)
{
cb
=
c
->
callbacks
[
i
];
if
(
cb
.
fn
!=
NULL
)
{
cb
.
fn
(
c
,
NULL
,
cb
.
privdata
);
}
}
/* Reset callback index */
c
->
cpos
=
0
;
}
if
(
c
->
cbDisconnect
.
fn
!=
NULL
)
c
->
cbDisconnect
.
fn
(
c
,
c
->
cbDisconnect
.
privdata
);
close
(
c
->
fd
);
...
...
libev-example.c
View file @
0f745d1a
...
...
@@ -5,6 +5,7 @@
#include <signal.h>
void
getCallback
(
redisContext
*
c
,
redisReply
*
reply
,
void
*
privdata
)
{
if
(
reply
==
NULL
)
return
;
/* Error */
printf
(
"argv[%s]: %s
\n
"
,
(
char
*
)
privdata
,
reply
->
reply
);
/* Disconnect after receiving the reply to GET */
...
...
libevent-example.c
View file @
0f745d1a
...
...
@@ -5,6 +5,7 @@
#include <signal.h>
void
getCallback
(
redisContext
*
c
,
redisReply
*
reply
,
void
*
privdata
)
{
if
(
reply
==
NULL
)
return
;
/* Error */
printf
(
"argv[%s]: %s
\n
"
,
(
const
char
*
)
privdata
,
reply
->
reply
);
/* Disconnect after receiving the reply to GET */
...
...
test.c
View file @
0f745d1a
...
...
@@ -202,18 +202,17 @@ static void __test_callback(redisContext *c, void *privdata) {
__test_callback_flags
|=
(
long
)
privdata
;
}
static
long
__test_reply_callback_flags
=
0
;
static
void
__test_reply_callback
(
redisContext
*
c
,
redisReply
*
reply
,
void
*
privdata
)
{
((
void
)
c
);
/* Shift to detect execution order */
__test_
reply_
callback_flags
<<=
8
;
__test_
reply_
callback_flags
|=
(
long
)
privdata
;
freeReplyObject
(
reply
);
__test_callback_flags
<<=
8
;
__test_callback_flags
|=
(
long
)
privdata
;
if
(
reply
)
freeReplyObject
(
reply
);
}
static
redisContext
*
__connect_nonblock
()
{
/* Reset callback flags */
__test_callback_flags
=
__test_reply_callback_flags
=
0
;
__test_callback_flags
=
0
;
return
redisConnectNonBlock
(
"127.0.0.1"
,
6379
,
NULL
);
}
...
...
@@ -278,11 +277,19 @@ static void test_nonblocking_connection() {
/* Read until at least one callback is executed (the 3 replies will
* arrive in a single packet, causing all callbacks to be executed in
* a single pass). */
while
(
__test_
reply_
callback_flags
==
0
)
{
while
(
__test_callback_flags
==
0
)
{
assert
(
redisBufferRead
(
c
)
==
REDIS_OK
);
redisProcessCallbacks
(
c
);
}
test_cond
(
__test_reply_callback_flags
==
0x010203
);
test_cond
(
__test_callback_flags
==
0x010203
);
redisFree
(
c
);
test
(
"redisDisconnect executes pending callbacks with NULL reply: "
);
c
=
__connect_nonblock
();
redisSetDisconnectCallback
(
c
,
__test_callback
,(
void
*
)
1
);
redisCommandWithCallback
(
c
,
__test_reply_callback
,(
void
*
)
2
,
"PING"
);
redisDisconnect
(
c
);
test_cond
(
__test_callback_flags
==
0x0201
);
redisFree
(
c
);
}
...
...
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