Skip to content

Commit

Permalink
Fix: treat DBConnector timeout=0 as infinite timeout (sonic-net#408)
Browse files Browse the repository at this point in the history
Fixed a bug introduced by sonic-net#387
Fixed sonic-net/sonic-buildimage#5697
  • Loading branch information
qiluo-msft authored and kktheballer committed Dec 21, 2020
1 parent 1757b53 commit e06d97b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
67 changes: 37 additions & 30 deletions common/dbconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,39 +337,40 @@ RedisContext::RedisContext(const RedisContext &other)
const char *unixPath = octx->unix_sock.path;
if (unixPath)
{
initContext(unixPath, *octx->timeout);
initContext(unixPath, octx->timeout);
}
else
{
initContext(octx->tcp.host, octx->tcp.port, *octx->timeout);
initContext(octx->tcp.host, octx->tcp.port, octx->timeout);
}
}

RedisContext::RedisContext(const string& hostname, int port,
unsigned int timeout)
{
struct timeval tv = {0, (suseconds_t)timeout * 1000};
initContext(hostname.c_str(), port, tv);
}

RedisContext::RedisContext(const string& unixPath, unsigned int timeout)
{
struct timeval tv = {0, (suseconds_t)timeout * 1000};
initContext(unixPath.c_str(), tv);
}

void RedisContext::initContext(const char *host, int port, const timeval& tv)
void RedisContext::initContext(const char *host, int port, const timeval *tv)
{
m_conn = redisConnectWithTimeout(host, port, tv);
if (tv)
{
m_conn = redisConnectWithTimeout(host, port, *tv);
}
else
{
m_conn = redisConnect(host, port);
}

if (m_conn->err)
throw system_error(make_error_code(errc::address_not_available),
"Unable to connect to redis");
}

void RedisContext::initContext(const char *path, const timeval &tv)
void RedisContext::initContext(const char *path, const timeval *tv)
{
m_conn = redisConnectUnixWithTimeout(path, tv);
if (tv)
{
m_conn = redisConnectUnixWithTimeout(path, *tv);
}
else
{
m_conn = redisConnectUnix(path);
}

if (m_conn->err)
throw system_error(make_error_code(errc::address_not_available),
Expand Down Expand Up @@ -444,19 +445,25 @@ DBConnector::DBConnector(int dbId, const RedisContext& ctx)
}

DBConnector::DBConnector(int dbId, const string& hostname, int port,
unsigned int timeout) :
RedisContext(hostname, port, timeout),
m_dbId(dbId),
m_namespace(EMPTY_NAMESPACE)
unsigned int timeout)
: m_dbId(dbId)
, m_namespace(EMPTY_NAMESPACE)
{
struct timeval tv = {0, (suseconds_t)timeout * 1000};
struct timeval *ptv = timeout ? &tv : NULL;
initContext(hostname.c_str(), port, ptv);

select(this);
}

DBConnector::DBConnector(int dbId, const string& unixPath, unsigned int timeout) :
RedisContext(unixPath, timeout),
m_dbId(dbId),
m_namespace(EMPTY_NAMESPACE)
DBConnector::DBConnector(int dbId, const string& unixPath, unsigned int timeout)
: m_dbId(dbId)
, m_namespace(EMPTY_NAMESPACE)
{
struct timeval tv = {0, (suseconds_t)timeout * 1000};
struct timeval *ptv = timeout ? &tv : NULL;
initContext(unixPath.c_str(), ptv);

select(this);
}

Expand All @@ -466,14 +473,14 @@ DBConnector::DBConnector(const string& dbName, unsigned int timeout, bool isTcpC
, m_namespace(netns)
{
struct timeval tv = {0, (suseconds_t)timeout * 1000};

struct timeval *ptv = timeout ? &tv : NULL;
if (isTcpConn)
{
initContext(SonicDBConfig::getDbHostname(dbName, netns).c_str(), SonicDBConfig::getDbPort(dbName, netns), tv);
initContext(SonicDBConfig::getDbHostname(dbName, netns).c_str(), SonicDBConfig::getDbPort(dbName, netns), ptv);
}
else
{
initContext(SonicDBConfig::getDbSock(dbName, netns).c_str(), tv);
initContext(SonicDBConfig::getDbSock(dbName, netns).c_str(), ptv);
}

select(this);
Expand Down
6 changes: 2 additions & 4 deletions common/dbconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ class RedisContext
* Timeout - The time in milisecond until exception is been thrown. For
* infinite wait, set this value to 0
*/
RedisContext(const std::string &hostname, int port, unsigned int timeout);
RedisContext(const std::string &unixPath, unsigned int timeout);
RedisContext(const RedisContext &other);
RedisContext& operator=(const RedisContext&) = delete;

Expand All @@ -99,8 +97,8 @@ class RedisContext

protected:
RedisContext();
void initContext(const char *host, int port, const timeval& tv);
void initContext(const char *path, const timeval &tv);
void initContext(const char *host, int port, const timeval *tv);
void initContext(const char *path, const timeval *tv);
void setContext(redisContext *ctx);

private:
Expand Down

0 comments on commit e06d97b

Please sign in to comment.