gosexy / redis

Redis client for Go that maps the full redis command list into equivalent Go functions.
MIT License
167 stars 44 forks source link

ConnectNonBlock does not properly return an error when connections fails #19

Closed briankassouf closed 10 years ago

briankassouf commented 10 years ago

ConnectNonBlock returns nil even if the connection to the redis server fails.

Steps to reproduce:

  1. Shut down redis server.
  2. attempt to connect with ConnectNonBlock
  3. return value will be nil
xiam commented 10 years ago

ConnectNonBlock provides a non-blocking connection, things are very different on non-blocking connections than on normal blocking connections, you won't receive an error unless the host or port have invalid values, for example, try to connect to 0.0.0.1, if host and port are OK the expected behavior is not to return an error and continue program execution without blocking.

If you try to write or read from the connection and the socket is not ready yet you'll in fact receive an error (this could be improved, adding connect and disconnect callbacks or channels for non-blocking connections).

Look at this example: https://github.com/redis/hiredis/blob/master/examples/example-libevent.c

We can run the above code like this:

git clone https://github.com/redis/hiredis.git
cd hiredis
make
cd examples
gcc -o main example-libevent.c -I../ -levent ../libhiredis.so 
LD_PRELOAD=../libhiredis.so ./main

If redis-server is down, the above program should fail like this:

Error: Connection refused

Note that you'll receive this error only after trying to write something to the redis-server and not when trying to open the async connection.

Change the line 42 to printf something else if ac->err is not nil, compile again and run, the modified line won't be printed, unless something really odd happens, like trying to connect to 0.0.0.1