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
814be4f5
Commit
814be4f5
authored
Jan 22, 2013
by
Henri Doreau
Browse files
Made connect functions return NULL on alloc failures.
Updated documentation and examples accordingly.
parent
3c46b13a
Changes
5
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
814be4f5
...
...
@@ -44,7 +44,7 @@ After trying to connect to Redis using `redisConnect` you should
check the
`err`
field to see if establishing the connection was successful:
redisContext *c = redisConnect("127.0.0.1", 6379);
if (c->err) {
if (c
!= NULL && c
->err) {
printf("Error: %s\n", c->errstr);
// handle error
}
...
...
async.c
View file @
814be4f5
...
...
@@ -142,15 +142,27 @@ static void __redisAsyncCopyError(redisAsyncContext *ac) {
}
redisAsyncContext
*
redisAsyncConnect
(
const
char
*
ip
,
int
port
)
{
redisContext
*
c
=
redisConnectNonBlock
(
ip
,
port
);
redisAsyncContext
*
ac
=
redisAsyncInitialize
(
c
);
redisContext
*
c
;
redisAsyncContext
*
ac
;
c
=
redisConnectNonBlock
(
ip
,
port
);
if
(
c
==
NULL
)
return
NULL
;
ac
=
redisAsyncInitialize
(
c
);
__redisAsyncCopyError
(
ac
);
return
ac
;
}
redisAsyncContext
*
redisAsyncConnectUnix
(
const
char
*
path
)
{
redisContext
*
c
=
redisConnectUnixNonBlock
(
path
);
redisAsyncContext
*
ac
=
redisAsyncInitialize
(
c
);
redisContext
*
c
;
redisAsyncContext
*
ac
;
c
=
redisConnectUnixNonBlock
(
path
);
if
(
c
==
NULL
)
return
NULL
;
ac
=
redisAsyncInitialize
(
c
);
__redisAsyncCopyError
(
ac
);
return
ac
;
}
...
...
example.c
View file @
814be4f5
...
...
@@ -11,8 +11,13 @@ int main(void) {
struct
timeval
timeout
=
{
1
,
500000
};
// 1.5 seconds
c
=
redisConnectWithTimeout
((
char
*
)
"127.0.0.1"
,
6379
,
timeout
);
if
(
c
->
err
)
{
printf
(
"Connection error: %s
\n
"
,
c
->
errstr
);
if
(
c
==
NULL
||
c
->
err
)
{
if
(
c
)
{
printf
(
"Connection error: %s
\n
"
,
c
->
errstr
);
redisFree
(
c
);
}
else
{
printf
(
"Connection error: can't allocate redis context
\n
"
);
}
exit
(
1
);
}
...
...
hiredis.c
View file @
814be4f5
...
...
@@ -1014,42 +1014,72 @@ void redisFree(redisContext *c) {
* context will be set to the return value of the error function.
* When no set of reply functions is given, the default set will be used. */
redisContext
*
redisConnect
(
const
char
*
ip
,
int
port
)
{
redisContext
*
c
=
redisContextInit
();
redisContext
*
c
;
c
=
redisContextInit
();
if
(
c
==
NULL
)
return
NULL
;
c
->
flags
|=
REDIS_BLOCK
;
redisContextConnectTcp
(
c
,
ip
,
port
,
NULL
);
return
c
;
}
redisContext
*
redisConnectWithTimeout
(
const
char
*
ip
,
int
port
,
struct
timeval
tv
)
{
redisContext
*
c
=
redisContextInit
();
redisContext
*
c
;
c
=
redisContextInit
();
if
(
c
==
NULL
)
return
NULL
;
c
->
flags
|=
REDIS_BLOCK
;
redisContextConnectTcp
(
c
,
ip
,
port
,
&
tv
);
return
c
;
}
redisContext
*
redisConnectNonBlock
(
const
char
*
ip
,
int
port
)
{
redisContext
*
c
=
redisContextInit
();
redisContext
*
c
;
c
=
redisContextInit
();
if
(
c
==
NULL
)
return
NULL
;
c
->
flags
&=
~
REDIS_BLOCK
;
redisContextConnectTcp
(
c
,
ip
,
port
,
NULL
);
return
c
;
}
redisContext
*
redisConnectUnix
(
const
char
*
path
)
{
redisContext
*
c
=
redisContextInit
();
redisContext
*
c
;
c
=
redisContextInit
();
if
(
c
==
NULL
)
return
NULL
;
c
->
flags
|=
REDIS_BLOCK
;
redisContextConnectUnix
(
c
,
path
,
NULL
);
return
c
;
}
redisContext
*
redisConnectUnixWithTimeout
(
const
char
*
path
,
struct
timeval
tv
)
{
redisContext
*
c
=
redisContextInit
();
redisContext
*
c
;
c
=
redisContextInit
();
if
(
c
==
NULL
)
return
NULL
;
c
->
flags
|=
REDIS_BLOCK
;
redisContextConnectUnix
(
c
,
path
,
&
tv
);
return
c
;
}
redisContext
*
redisConnectUnixNonBlock
(
const
char
*
path
)
{
redisContext
*
c
=
redisContextInit
();
redisContext
*
c
;
c
=
redisContextInit
();
if
(
c
==
NULL
)
return
NULL
;
c
->
flags
&=
~
REDIS_BLOCK
;
redisContextConnectUnix
(
c
,
path
,
NULL
);
return
c
;
...
...
test.c
View file @
814be4f5
...
...
@@ -88,7 +88,10 @@ static redisContext *connect(struct config config) {
assert
(
NULL
);
}
if
(
c
->
err
)
{
if
(
c
==
NULL
)
{
printf
(
"Connection error: can't allocate redis context
\n
"
);
exit
(
1
);
}
else
if
(
c
->
err
)
{
printf
(
"Connection error: %s
\n
"
,
c
->
errstr
);
exit
(
1
);
}
...
...
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