diff --git a/src/brpc/builtin/connections_service.cpp b/src/brpc/builtin/connections_service.cpp index fdef0e6831..02adc56b38 100644 --- a/src/brpc/builtin/connections_service.cpp +++ b/src/brpc/builtin/connections_service.cpp @@ -161,7 +161,7 @@ void ConnectionsService::PrintConnections( if (ret < 0) { continue; } else if (ret > 0) { - if (ptr->_health_check_interval_s <= 0) { + if (!ptr->HCEnabled()) { // Sockets without HC will soon be destroyed continue; } diff --git a/src/brpc/socket.cpp b/src/brpc/socket.cpp index a634dd376c..200905acb2 100644 --- a/src/brpc/socket.cpp +++ b/src/brpc/socket.cpp @@ -958,7 +958,7 @@ int Socket::SetFailed(int error_code, const char* error_fmt, ...) { // Do health-checking even if we're not connected before, needed // by Channel to revive never-connected socket when server side // comes online. - if (_health_check_interval_s > 0) { + if (HCEnabled()) { bool expect = false; if (_hc_started.compare_exchange_strong(expect, true, diff --git a/src/brpc/socket.h b/src/brpc/socket.h index 74f7360ea2..e4fc5896f5 100644 --- a/src/brpc/socket.h +++ b/src/brpc/socket.h @@ -319,10 +319,14 @@ friend class policy::H2GlobalStreamCreator; // ip/port of the other end of the connection. butil::EndPoint remote_side() const { return _remote_side; } - // Positive value enables health checking. // Initialized by SocketOptions.health_check_interval_s. int health_check_interval() const { return _health_check_interval_s; } + // True if health checking is enabled. + bool HCEnabled() const { + return _health_check_interval_s > 0 && _is_hc_related_ref_held; + } + // When someone holds a health-checking-related reference, // this function need to be called to make health checking run normally. void SetHCRelatedRefHeld() { _is_hc_related_ref_held = true; } diff --git a/src/brpc/socket_map.cpp b/src/brpc/socket_map.cpp index d169afd8a9..a451c1a68c 100644 --- a/src/brpc/socket_map.cpp +++ b/src/brpc/socket_map.cpp @@ -149,7 +149,7 @@ SocketMap::~SocketMap() { for (Map::iterator it = _map.begin(); it != _map.end(); ++it) { SingleConnection* sc = &it->second; if ((!sc->socket->Failed() || - sc->socket->health_check_interval() > 0/*HC enabled*/) && + sc->socket->HCEnabled()) && sc->ref_count != 0) { ++nleft; if (nleft == 0) { @@ -216,9 +216,7 @@ int SocketMap::Insert(const SocketMapKey& key, SocketId* id, std::unique_lock mu(_mutex); SingleConnection* sc = _map.seek(key); if (sc) { - if (!sc->socket->Failed() || - (sc->socket->health_check_interval() > 0 && - sc->socket->IsHCRelatedRefHeld())/*HC enabled*/) { + if (!sc->socket->Failed() || sc->socket->HCEnabled()) { ++sc->ref_count; *id = sc->socket->id(); return 0;