Skip to content

Commit

Permalink
Support keeping alive with interval (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
sewenew authored Apr 5, 2023
1 parent de46804 commit 96d0653
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,28 @@ else()
endif()
endif()

# Check hiredis features
message(STATUS "redis-plus-plus check hiredis features")
if(hiredis_FOUND)
set(HIREDIS_FEATURE_TEST_INCLUDE ${hiredis_INCLUDE_DIRS})
set(HIREDIS_FEATURE_TEST_LIB ${REDIS_PLUS_PLUS_HIREDIS_LIBS})
else()
set(HIREDIS_FEATURE_TEST_INCLUDE ${HIREDIS_HEADER})
set(HIREDIS_FEATURE_TEST_LIB ${HIREDIS_LIB})
endif()
set(HIREDIS_FEATURE_TEST_HEADER "${HIREDIS_FEATURE_TEST_INCLUDE}/hiredis/hiredis.h")

include(CheckSymbolExists)
set(CMAKE_REQUIRED_LIBRARIES_BACK ${CMAKE_REQUIRED_LIBRARIES})
set(CMAKE_REQUIRED_LIBRARIES ${HIREDIS_FEATURE_TEST_LIB})

CHECK_SYMBOL_EXISTS(redisEnableKeepAliveWithInterval ${HIREDIS_FEATURE_TEST_HEADER} REDIS_PLUS_PLUS_HAS_redisEnableKeepAliveWithInterval)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/hiredis_features.h.in ${CMAKE_CURRENT_SOURCE_DIR}/${REDIS_PLUS_PLUS_SOURCE_DIR}/hiredis_features.h)

# Restore CMAKE_REQUIRED_LIBRARIES
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES_BACK})

# Build static library
option(REDIS_PLUS_PLUS_BUILD_STATIC "Build static library" ON)
message(STATUS "redis-plus-plus build static library: ${REDIS_PLUS_PLUS_BUILD_STATIC}")
Expand Down
1 change: 1 addition & 0 deletions hiredis_features.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#cmakedefine REDIS_PLUS_PLUS_HAS_redisEnableKeepAliveWithInterval
10 changes: 10 additions & 0 deletions src/sw/redis++/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ void Connection::Connector::_set_socket_timeout(redisContext &ctx) const {
}

void Connection::Connector::_enable_keep_alive(redisContext &ctx) const {
#ifdef REDIS_PLUS_PLUS_HAS_redisEnableKeepAliveWithInterval
if (_opts.keep_alive_s > std::chrono::seconds{0}) {
if (redisEnableKeepAliveWithInterval(&ctx, _opts.keep_alive_s.count()) != REDIS_OK) {
throw_error(ctx, "Failed to enable keep alive option");
}

return;
}
#endif // end REDIS_PLUS_PLUS_HAS_redisEnableKeepAliveWithInterval

if (!_opts.keep_alive) {
return;
}
Expand Down
7 changes: 7 additions & 0 deletions src/sw/redis++/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "sw/redis++/reply.h"
#include "sw/redis++/utils.h"
#include "sw/redis++/tls.h"
#include "sw/redis++/hiredis_features.h"

namespace sw {

Expand Down Expand Up @@ -67,6 +68,12 @@ struct ConnectionOptions {

bool keep_alive = false;

#ifdef REDIS_PLUS_PLUS_HAS_redisEnableKeepAliveWithInterval

std::chrono::seconds keep_alive_s = std::chrono::seconds{0};

#endif // end REDIS_PLUS_PLUS_HAS_redisEnableKeepAliveWithInterval

std::chrono::milliseconds connect_timeout{0};

std::chrono::milliseconds socket_timeout{0};
Expand Down

0 comments on commit 96d0653

Please sign in to comment.