Skip to content

Commit

Permalink
Add set heartbeat_timeout and heartbeat_ivl
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy-peak committed Jul 23, 2024
1 parent 26aeb2f commit f8e378f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 26 deletions.
9 changes: 9 additions & 0 deletions out/bi_web/include/impl/bi_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 10 additions & 13 deletions out/bi_web/include/impl/date_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(value.month)
<< " day=" << static_cast<uint32_t>(value.day);
return ss.str();
Expand Down Expand Up @@ -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<uint32_t>(value.hour)
ss << "Time"
<< " hour=" << static_cast<uint32_t>(value.hour)
<< " minute=" << static_cast<uint32_t>(value.minute)
<< " second=" << static_cast<uint32_t>(value.second)
<< " microsecond=" << value.microsecond;
Expand Down Expand Up @@ -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();
}

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -139,8 +138,7 @@ template <bool millisec = false>
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;
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions out/bi_web/include/impl/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ struct ChannelConfig {
bool probe{false};
int16_t context_pool_size{1};
std::size_t channel_size{50000};
std::optional<int32_t> heartbeat_timeout;
std::optional<int32_t> heartbeat_ivl;
};

inline std::string createUuid() {
Expand Down
9 changes: 9 additions & 0 deletions out/include/impl/bi_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 10 additions & 13 deletions out/include/impl/date_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(value.month)
<< " day=" << static_cast<uint32_t>(value.day);
return ss.str();
Expand Down Expand Up @@ -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<uint32_t>(value.hour)
ss << "Time"
<< " hour=" << static_cast<uint32_t>(value.hour)
<< " minute=" << static_cast<uint32_t>(value.minute)
<< " second=" << static_cast<uint32_t>(value.second)
<< " microsecond=" << value.microsecond;
Expand Down Expand Up @@ -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();
}

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -139,8 +138,7 @@ template <bool millisec = false>
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;
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions out/include/impl/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ struct ChannelConfig {
bool probe{false};
int16_t context_pool_size{1};
std::size_t channel_size{50000};
std::optional<int32_t> heartbeat_timeout;
std::optional<int32_t> heartbeat_ivl;
};

inline std::string createUuid() {
Expand Down
7 changes: 7 additions & 0 deletions template/cpp/impl/bi_channel.inja
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions template/cpp/impl/utils.inja
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ struct ChannelConfig {
bool probe{false};
int16_t context_pool_size{1};
std::size_t channel_size{50000};
std::optional<int32_t> heartbeat_timeout;
std::optional<int32_t> heartbeat_ivl;
};

inline std::string createUuid() {
Expand Down

0 comments on commit f8e378f

Please sign in to comment.