cauthenticationredishiredis

hiredis async connect with password


I am using hiredis C library to connect to my redis instance. I am considering changing my redis.conf to enable requirepass option. I know that for redisConnect() I simply connect to host/port without authentication, and then send the AUTH mypassword command using the redisCommand() and context. However, how does one do it for redisAsyncConnect()? Looking at the source code, the redisAsyncCommand() function would fail when asked to send an AUTH mypassword command.


Solution

  • I was incorrect in my analysis of redisAsyncConnect() function operation. The following code works, with the only caveat that you don't know whether the AUTH command succeeded:

      redisAsyncContext *async_context = redisAsyncConnect(redis_info.host, redis_info.port);
      if (async_context->err)
      {
        throw std::runtime_error("Error creating async_context: " + async_context->errstr);
      }
      if (0 != strlen(redis_info.passwd))
      {
        if (REDIS_OK != redisAsyncCommand(async_context, NULL, NULL, "AUTH %s", redis_info.passwd))
        {   
          throw std::runtime_error("Error sending AUTH in async_context");
        }   
      }