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
8d5bc445
Commit
8d5bc445
authored
Apr 19, 2013
by
Allen.Dou
Browse files
SetKeepAlive
Keep client alive even though no command was sent to server for a long time.
parent
63ce20dd
Changes
2
Hide whitespace changes
Inline
Side-by-side
hiredis.h
View file @
8d5bc445
...
...
@@ -88,6 +88,8 @@
#define REDIS_READER_MAX_BUF (1024*16)
/* Default max unused reader buffer. */
#define REDIS_KEEPALIVE_INTERVAL 15
/* seconds */
#ifdef __cplusplus
extern
"C"
{
#endif
...
...
net.c
View file @
8d5bc445
...
...
@@ -113,6 +113,38 @@ static int redisSetBlocking(redisContext *c, int fd, int blocking) {
return
REDIS_OK
;
}
static
int
redisKeepAlive
(
redisContext
*
c
,
int
fd
,
int
interval
)
{
int
val
=
1
;
if
(
setsockopt
(
fd
,
SOL_SOCKET
,
SO_KEEPALIVE
,
&
val
,
sizeof
(
val
))
==
-
1
){
__redisSetError
(
c
,
REDIS_ERR_OTHER
,
strerror
(
errno
));
return
REDIS_ERR
;
}
val
=
interval
;
if
(
setsockopt
(
fd
,
IPPROTO_TCP
,
TCP_KEEPIDLE
,
&
val
,
sizeof
(
val
))
<
0
)
{
__redisSetError
(
c
,
REDIS_ERR_OTHER
,
strerror
(
errno
));
return
REDIS_ERR
;
}
val
=
interval
/
3
;
if
(
val
==
0
)
val
=
1
;
if
(
setsockopt
(
fd
,
IPPROTO_TCP
,
TCP_KEEPINTVL
,
&
val
,
sizeof
(
val
))
<
0
)
{
__redisSetError
(
c
,
REDIS_ERR_OTHER
,
strerror
(
errno
));
return
REDIS_ERR
;
}
val
=
3
;
if
(
setsockopt
(
fd
,
IPPROTO_TCP
,
TCP_KEEPCNT
,
&
val
,
sizeof
(
val
))
<
0
)
{
__redisSetError
(
c
,
REDIS_ERR_OTHER
,
strerror
(
errno
));
return
REDIS_ERR
;
}
return
REDIS_OK
;
}
static
int
redisSetTcpNoDelay
(
redisContext
*
c
,
int
fd
)
{
int
yes
=
1
;
if
(
setsockopt
(
fd
,
IPPROTO_TCP
,
TCP_NODELAY
,
&
yes
,
sizeof
(
yes
))
==
-
1
)
{
...
...
@@ -241,6 +273,8 @@ int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct t
goto
error
;
if
(
redisSetTcpNoDelay
(
c
,
s
)
!=
REDIS_OK
)
goto
error
;
if
(
redisKeepAlive
(
c
,
s
,
REDIS_KEEPALIVE_INTERVAL
)
!=
REDIS_OK
)
goto
error
;
c
->
fd
=
s
;
c
->
flags
|=
REDIS_CONNECTED
;
...
...
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