@@ -337,39 +337,40 @@ RedisContext::RedisContext(const RedisContext &other)
337337 const char *unixPath = octx->unix_sock .path ;
338338 if (unixPath)
339339 {
340- initContext (unixPath, * octx->timeout );
340+ initContext (unixPath, octx->timeout );
341341 }
342342 else
343343 {
344- initContext (octx->tcp .host , octx->tcp .port , * octx->timeout );
344+ initContext (octx->tcp .host , octx->tcp .port , octx->timeout );
345345 }
346346}
347347
348- RedisContext::RedisContext (const string& hostname, int port,
349- unsigned int timeout)
350- {
351- struct timeval tv = {0 , (suseconds_t )timeout * 1000 };
352- initContext (hostname.c_str (), port, tv);
353- }
354-
355- RedisContext::RedisContext (const string& unixPath, unsigned int timeout)
356- {
357- struct timeval tv = {0 , (suseconds_t )timeout * 1000 };
358- initContext (unixPath.c_str (), tv);
359- }
360-
361- void RedisContext::initContext (const char *host, int port, const timeval& tv)
348+ void RedisContext::initContext (const char *host, int port, const timeval *tv)
362349{
363- m_conn = redisConnectWithTimeout (host, port, tv);
350+ if (tv)
351+ {
352+ m_conn = redisConnectWithTimeout (host, port, *tv);
353+ }
354+ else
355+ {
356+ m_conn = redisConnect (host, port);
357+ }
364358
365359 if (m_conn->err )
366360 throw system_error (make_error_code (errc::address_not_available),
367361 " Unable to connect to redis" );
368362}
369363
370- void RedisContext::initContext (const char *path, const timeval & tv)
364+ void RedisContext::initContext (const char *path, const timeval * tv)
371365{
372- m_conn = redisConnectUnixWithTimeout (path, tv);
366+ if (tv)
367+ {
368+ m_conn = redisConnectUnixWithTimeout (path, *tv);
369+ }
370+ else
371+ {
372+ m_conn = redisConnectUnix (path);
373+ }
373374
374375 if (m_conn->err )
375376 throw system_error (make_error_code (errc::address_not_available),
@@ -444,19 +445,25 @@ DBConnector::DBConnector(int dbId, const RedisContext& ctx)
444445}
445446
446447DBConnector::DBConnector (int dbId, const string& hostname, int port,
447- unsigned int timeout) :
448- RedisContext(hostname, port, timeout),
449- m_dbId(dbId),
450- m_namespace(EMPTY_NAMESPACE )
448+ unsigned int timeout)
449+ : m_dbId(dbId)
450+ , m_namespace(EMPTY_NAMESPACE )
451451{
452+ struct timeval tv = {0 , (suseconds_t )timeout * 1000 };
453+ struct timeval *ptv = timeout ? &tv : NULL ;
454+ initContext (hostname.c_str (), port, ptv);
455+
452456 select (this );
453457}
454458
455- DBConnector::DBConnector (int dbId, const string& unixPath, unsigned int timeout) :
456- RedisContext(unixPath, timeout),
457- m_dbId(dbId),
458- m_namespace(EMPTY_NAMESPACE )
459+ DBConnector::DBConnector (int dbId, const string& unixPath, unsigned int timeout)
460+ : m_dbId(dbId)
461+ , m_namespace(EMPTY_NAMESPACE )
459462{
463+ struct timeval tv = {0 , (suseconds_t )timeout * 1000 };
464+ struct timeval *ptv = timeout ? &tv : NULL ;
465+ initContext (unixPath.c_str (), ptv);
466+
460467 select (this );
461468}
462469
@@ -466,14 +473,14 @@ DBConnector::DBConnector(const string& dbName, unsigned int timeout, bool isTcpC
466473 , m_namespace(netns)
467474{
468475 struct timeval tv = {0 , (suseconds_t )timeout * 1000 };
469-
476+ struct timeval *ptv = timeout ? &tv : NULL ;
470477 if (isTcpConn)
471478 {
472- initContext (SonicDBConfig::getDbHostname (dbName, netns).c_str (), SonicDBConfig::getDbPort (dbName, netns), tv );
479+ initContext (SonicDBConfig::getDbHostname (dbName, netns).c_str (), SonicDBConfig::getDbPort (dbName, netns), ptv );
473480 }
474481 else
475482 {
476- initContext (SonicDBConfig::getDbSock (dbName, netns).c_str (), tv );
483+ initContext (SonicDBConfig::getDbSock (dbName, netns).c_str (), ptv );
477484 }
478485
479486 select (this );
0 commit comments