From 96d0653ee0fb2e0437cc6d76c1c61c2b7946e872 Mon Sep 17 00:00:00 2001 From: sewenew Date: Wed, 5 Apr 2023 10:57:56 +0800 Subject: [PATCH] Support keeping alive with interval (#470) --- CMakeLists.txt | 22 ++++++++++++++++++++++ hiredis_features.h.in | 1 + src/sw/redis++/connection.cpp | 10 ++++++++++ src/sw/redis++/connection.h | 7 +++++++ 4 files changed, 40 insertions(+) create mode 100644 hiredis_features.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 9354dc7..6470b32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}") diff --git a/hiredis_features.h.in b/hiredis_features.h.in new file mode 100644 index 0000000..3463e85 --- /dev/null +++ b/hiredis_features.h.in @@ -0,0 +1 @@ +#cmakedefine REDIS_PLUS_PLUS_HAS_redisEnableKeepAliveWithInterval diff --git a/src/sw/redis++/connection.cpp b/src/sw/redis++/connection.cpp index aab7d0d..04ba860 100644 --- a/src/sw/redis++/connection.cpp +++ b/src/sw/redis++/connection.cpp @@ -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; } diff --git a/src/sw/redis++/connection.h b/src/sw/redis++/connection.h index e0ae18b..8ca3684 100644 --- a/src/sw/redis++/connection.h +++ b/src/sw/redis++/connection.h @@ -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 { @@ -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};