From f8e378f9e74d7368e2b5fa7bf4c97c2e4e930fdf Mon Sep 17 00:00:00 2001 From: fantasy-peak <1356346239@qq.com> Date: Tue, 23 Jul 2024 16:47:04 +0800 Subject: [PATCH] Add set heartbeat_timeout and heartbeat_ivl --- out/bi_web/include/impl/bi_channel.h | 9 +++++++++ out/bi_web/include/impl/date_time.h | 23 ++++++++++------------- out/bi_web/include/impl/utils.h | 2 ++ out/include/impl/bi_channel.h | 9 +++++++++ out/include/impl/date_time.h | 23 ++++++++++------------- out/include/impl/utils.h | 2 ++ template/cpp/impl/bi_channel.inja | 7 +++++++ template/cpp/impl/utils.inja | 2 ++ 8 files changed, 51 insertions(+), 26 deletions(-) diff --git a/out/bi_web/include/impl/bi_channel.h b/out/bi_web/include/impl/bi_channel.h index b703b7f..f68070a 100644 --- a/out/bi_web/include/impl/bi_channel.h +++ b/out/bi_web/include/impl/bi_channel.h @@ -216,6 +216,15 @@ class BiChannel final { m_socket_ptr->set(zmq::sockopt::sndbuf, config.sendbuf); m_socket_ptr->set(zmq::sockopt::rcvbuf, config.recvbuf); m_socket_ptr->set(zmq::sockopt::linger, config.linger); + if (config.heartbeat_timeout) + m_socket_ptr->set(zmq::sockopt::heartbeat_timeout, + config.heartbeat_timeout.value()); + // http://api.zeromq.org/4-2:zmq-setsockopt + // https://github.com/zeromq/libzmq/issues/3188 + // https://wenku.csdn.net/answer/5e884e5fe84044989d65c9b2ec54f122 + if (config.heartbeat_ivl) + m_socket_ptr->set(zmq::sockopt::heartbeat_ivl, + config.heartbeat_ivl.value()); if (config.tcp_keepalive) { m_socket_ptr->set(zmq::sockopt::tcp_keepalive, 1); m_socket_ptr->set(zmq::sockopt::tcp_keepalive_idle, diff --git a/out/bi_web/include/impl/date_time.h b/out/bi_web/include/impl/date_time.h index 8434e8e..ac7465f 100644 --- a/out/bi_web/include/impl/date_time.h +++ b/out/bi_web/include/impl/date_time.h @@ -26,7 +26,8 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Date, year, month, day) inline std::string toString(const Date& value) { std::ostringstream ss; - ss << "Date" << " year=" << value.year + ss << "Date" + << " year=" << value.year << " month=" << static_cast(value.month) << " day=" << static_cast(value.day); return ss.str(); @@ -60,7 +61,8 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Time, hour, minute, second, microsecond) inline std::string toString(const Time& value) { std::ostringstream ss; - ss << "Time" << " hour=" << static_cast(value.hour) + ss << "Time" + << " hour=" << static_cast(value.hour) << " minute=" << static_cast(value.minute) << " second=" << static_cast(value.second) << " microsecond=" << value.microsecond; @@ -92,8 +94,8 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(DateTime, date, time) inline std::string toString(const DateTime& value) { std::ostringstream ss; - ss << "DateTime" << " date=" << toString(value.date) - << " time=" << toString(value.time); + ss << "DateTime" + << " date=" << toString(value.date) << " time=" << toString(value.time); return ss.str(); } @@ -111,9 +113,7 @@ inline DateTime getFrpcDateTime(std::chrono::system_clock::time_point now_time = now_time.time_since_epoch()); auto microseconds = mic.count() % 1000000; auto time_tt = std::chrono::system_clock::to_time_t(now_time); - - struct tm work {}; - + struct tm work{}; localtime_r(&time_tt, &work); Date d(1900 + work.tm_year, work.tm_mon + 1, work.tm_mday); Time t(work.tm_hour, work.tm_min, work.tm_sec, microseconds); @@ -122,8 +122,7 @@ inline DateTime getFrpcDateTime(std::chrono::system_clock::time_point now_time = inline std::chrono::system_clock::time_point frpcDateTimeToTimePoint( const DateTime& date_time) { - struct tm timeinfo {}; - + struct tm timeinfo{}; timeinfo.tm_year = date_time.date.year - 1900; timeinfo.tm_mon = date_time.date.month - 1; timeinfo.tm_mday = date_time.date.day; @@ -139,8 +138,7 @@ template inline std::string fromFrpcDateTime( const DateTime& date_time, const std::string& format = "%Y-%m-%d %H:%M:%S") { - struct tm timeinfo {}; - + struct tm timeinfo{}; timeinfo.tm_year = date_time.date.year - 1900; timeinfo.tm_mon = date_time.date.month - 1; timeinfo.tm_mday = date_time.date.day; @@ -159,8 +157,7 @@ inline std::string fromFrpcDateTime( inline DateTime toFrpcDateTime( const std::string& date, const std::string& format = "%Y-%m-%d %H:%M:%S.") { - struct tm tm {}; - + struct tm tm{}; std::stringstream ss(date); ss >> std::get_time(&tm, format.c_str()); int milliseconds = 0; diff --git a/out/bi_web/include/impl/utils.h b/out/bi_web/include/impl/utils.h index 619d4a4..a4cf36b 100644 --- a/out/bi_web/include/impl/utils.h +++ b/out/bi_web/include/impl/utils.h @@ -63,6 +63,8 @@ struct ChannelConfig { bool probe{false}; int16_t context_pool_size{1}; std::size_t channel_size{50000}; + std::optional heartbeat_timeout; + std::optional heartbeat_ivl; }; inline std::string createUuid() { diff --git a/out/include/impl/bi_channel.h b/out/include/impl/bi_channel.h index b703b7f..f68070a 100644 --- a/out/include/impl/bi_channel.h +++ b/out/include/impl/bi_channel.h @@ -216,6 +216,15 @@ class BiChannel final { m_socket_ptr->set(zmq::sockopt::sndbuf, config.sendbuf); m_socket_ptr->set(zmq::sockopt::rcvbuf, config.recvbuf); m_socket_ptr->set(zmq::sockopt::linger, config.linger); + if (config.heartbeat_timeout) + m_socket_ptr->set(zmq::sockopt::heartbeat_timeout, + config.heartbeat_timeout.value()); + // http://api.zeromq.org/4-2:zmq-setsockopt + // https://github.com/zeromq/libzmq/issues/3188 + // https://wenku.csdn.net/answer/5e884e5fe84044989d65c9b2ec54f122 + if (config.heartbeat_ivl) + m_socket_ptr->set(zmq::sockopt::heartbeat_ivl, + config.heartbeat_ivl.value()); if (config.tcp_keepalive) { m_socket_ptr->set(zmq::sockopt::tcp_keepalive, 1); m_socket_ptr->set(zmq::sockopt::tcp_keepalive_idle, diff --git a/out/include/impl/date_time.h b/out/include/impl/date_time.h index 8434e8e..ac7465f 100644 --- a/out/include/impl/date_time.h +++ b/out/include/impl/date_time.h @@ -26,7 +26,8 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Date, year, month, day) inline std::string toString(const Date& value) { std::ostringstream ss; - ss << "Date" << " year=" << value.year + ss << "Date" + << " year=" << value.year << " month=" << static_cast(value.month) << " day=" << static_cast(value.day); return ss.str(); @@ -60,7 +61,8 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Time, hour, minute, second, microsecond) inline std::string toString(const Time& value) { std::ostringstream ss; - ss << "Time" << " hour=" << static_cast(value.hour) + ss << "Time" + << " hour=" << static_cast(value.hour) << " minute=" << static_cast(value.minute) << " second=" << static_cast(value.second) << " microsecond=" << value.microsecond; @@ -92,8 +94,8 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(DateTime, date, time) inline std::string toString(const DateTime& value) { std::ostringstream ss; - ss << "DateTime" << " date=" << toString(value.date) - << " time=" << toString(value.time); + ss << "DateTime" + << " date=" << toString(value.date) << " time=" << toString(value.time); return ss.str(); } @@ -111,9 +113,7 @@ inline DateTime getFrpcDateTime(std::chrono::system_clock::time_point now_time = now_time.time_since_epoch()); auto microseconds = mic.count() % 1000000; auto time_tt = std::chrono::system_clock::to_time_t(now_time); - - struct tm work {}; - + struct tm work{}; localtime_r(&time_tt, &work); Date d(1900 + work.tm_year, work.tm_mon + 1, work.tm_mday); Time t(work.tm_hour, work.tm_min, work.tm_sec, microseconds); @@ -122,8 +122,7 @@ inline DateTime getFrpcDateTime(std::chrono::system_clock::time_point now_time = inline std::chrono::system_clock::time_point frpcDateTimeToTimePoint( const DateTime& date_time) { - struct tm timeinfo {}; - + struct tm timeinfo{}; timeinfo.tm_year = date_time.date.year - 1900; timeinfo.tm_mon = date_time.date.month - 1; timeinfo.tm_mday = date_time.date.day; @@ -139,8 +138,7 @@ template inline std::string fromFrpcDateTime( const DateTime& date_time, const std::string& format = "%Y-%m-%d %H:%M:%S") { - struct tm timeinfo {}; - + struct tm timeinfo{}; timeinfo.tm_year = date_time.date.year - 1900; timeinfo.tm_mon = date_time.date.month - 1; timeinfo.tm_mday = date_time.date.day; @@ -159,8 +157,7 @@ inline std::string fromFrpcDateTime( inline DateTime toFrpcDateTime( const std::string& date, const std::string& format = "%Y-%m-%d %H:%M:%S.") { - struct tm tm {}; - + struct tm tm{}; std::stringstream ss(date); ss >> std::get_time(&tm, format.c_str()); int milliseconds = 0; diff --git a/out/include/impl/utils.h b/out/include/impl/utils.h index 619d4a4..a4cf36b 100644 --- a/out/include/impl/utils.h +++ b/out/include/impl/utils.h @@ -63,6 +63,8 @@ struct ChannelConfig { bool probe{false}; int16_t context_pool_size{1}; std::size_t channel_size{50000}; + std::optional heartbeat_timeout; + std::optional heartbeat_ivl; }; inline std::string createUuid() { diff --git a/template/cpp/impl/bi_channel.inja b/template/cpp/impl/bi_channel.inja index d002e60..318011d 100644 --- a/template/cpp/impl/bi_channel.inja +++ b/template/cpp/impl/bi_channel.inja @@ -180,6 +180,13 @@ private: m_socket_ptr->set(zmq::sockopt::sndbuf, config.sendbuf); m_socket_ptr->set(zmq::sockopt::rcvbuf, config.recvbuf); m_socket_ptr->set(zmq::sockopt::linger, config.linger); + if (config.heartbeat_timeout) + m_socket_ptr->set(zmq::sockopt::heartbeat_timeout, config.heartbeat_timeout.value()); + // http://api.zeromq.org/4-2:zmq-setsockopt + // https://github.com/zeromq/libzmq/issues/3188 + // https://wenku.csdn.net/answer/5e884e5fe84044989d65c9b2ec54f122 + if (config.heartbeat_ivl) + m_socket_ptr->set(zmq::sockopt::heartbeat_ivl, config.heartbeat_ivl.value()); if (config.tcp_keepalive) { m_socket_ptr->set(zmq::sockopt::tcp_keepalive, 1); m_socket_ptr->set(zmq::sockopt::tcp_keepalive_idle, config.tcp_keepalive_idle); diff --git a/template/cpp/impl/utils.inja b/template/cpp/impl/utils.inja index 0123c3e..78c37f6 100644 --- a/template/cpp/impl/utils.inja +++ b/template/cpp/impl/utils.inja @@ -63,6 +63,8 @@ struct ChannelConfig { bool probe{false}; int16_t context_pool_size{1}; std::size_t channel_size{50000}; + std::optional heartbeat_timeout; + std::optional heartbeat_ivl; }; inline std::string createUuid() {