Repository: valkey-io/valkey
File: src/cluster_legacy.c (C)

Code before:
    if (node->flags & CLUSTER_NODE_PFAIL) server.cluster->stats_pfail_nodes++;

    /* A Node in HANDSHAKE state has a limited lifespan equal to the
     * configured node timeout. */
    if (nodeInHandshake(node) && nodeExceedsHandshakeTimeout(node, now)) {
        serverLog(LL_WARNING, "Clusterbus handshake timeout %s:%d", node->ip, node->cport);
        clusterDelNode(node);
        return 1;
    }
    if (nodeInNormalState(node) && node->link != NULL && node->inbound_link == NULL &&
        now - node->inbound_link_freed_time > getHandshakeTimeout() &&
        now - node->meet_sent > getHandshakeTimeout()) {
        /* Node has an outbound link, but no inbound link for more than the handshake timeout.
         * This probably means this node does not know us yet, whereas we know it.
         * So we send it a MEET packet to do a handshake with it and correct the inconsistent cluster view.
         * We make sure to not re-send a MEET packet more than once every handshake timeout period, so as to
         * leave the other node time to complete the handshake. */
        node->flags |= CLUSTER_NODE_MEET;
        serverLog(LL_NOTICE, "Sending MEET packet to node %.40s (%s) because there is no inbound link for it",
                  node->name, node->human_nodename);
        clusterSendPing(node->link, CLUSTERMSG_TYPE_MEET);
    }

    if (node->link == NULL) {
        mstime_t reconnect_interval = server.cluster_node_timeout / 2;
        /* Skip this outbound connection attempt when all three are true:
         *  1. No inbound link from the peer exists.
         *  2. The back‑off window since the last try is still active
         *  3. The cluster has already exceeded its retry budget for this cron cycle
         */
        if (!node->inbound_link && (now - node->outbound_link_attempt_time < reconnect_interval / NODE_CONNECTION_RETRIES_PER_TIMEOUT && *cluster_conn_attempts == 0)) {
            return 1;
        }
        node->outbound_link_attempt_time = now;
        (*cluster_conn_attempts)--;
        clusterLink *link = createClusterLink(node);
        link->conn = connCreate(connTypeOfCluster());
        connSetPrivateData(link->conn, link);
        if (connConnect(link->conn, node->ip, node->cport, server.bind_source_addr, 0, clusterLinkConnectHandler) ==
            C_ERR) {
            /* We got a synchronous error from connect before
             * clusterSendPing() had a chance to be called.
             * If node->ping_sent is zero, failure detection can't work,
             * so we claim we actually sent a ping now (that will
             * be really sent as soon as the link is obtained). */
            if (node->ping_sent == 0) node->ping_sent = mstime();
            serverLog(LL_DEBUG,
                      "Unable to connect to "
                      "Cluster Node [%s]:%d -> %s",
                      node->ip, node->cport, server.neterr);


Review comment (suggestion):
```suggestion
        /* Skip this outbound connection attempt when all these conditions are true:
```

Code after:
    if (node->flags & CLUSTER_NODE_PFAIL) server.cluster->stats_pfail_nodes++;

    /* A Node in HANDSHAKE state has a limited lifespan equal to the
     * configured node timeout. */
    if (nodeInHandshake(node) && nodeExceedsHandshakeTimeout(node, now)) {
        serverLog(LL_WARNING, "Clusterbus handshake timeout %s:%d", node->ip, node->cport);
        clusterDelNode(node);
        return 1;
    }
    if (nodeInNormalState(node) && node->link != NULL && node->inbound_link == NULL &&
        now - node->inbound_link_freed_time > getHandshakeTimeout() &&
        now - node->meet_sent > getHandshakeTimeout()) {
        /* Node has an outbound link, but no inbound link for more than the handshake timeout.
         * This probably means this node does not know us yet, whereas we know it.
         * So we send it a MEET packet to do a handshake with it and correct the inconsistent cluster view.
         * We make sure to not re-send a MEET packet more than once every handshake timeout period, so as to
         * leave the other node time to complete the handshake. */
        node->flags |= CLUSTER_NODE_MEET;
        serverLog(LL_NOTICE, "Sending MEET packet to node %.40s (%s) because there is no inbound link for it",
                  node->name, node->human_nodename);
        clusterSendPing(node->link, CLUSTERMSG_TYPE_MEET);
    }

    if (node->link == NULL) {
        mstime_t reconnect_interval = server.cluster_node_timeout / 2;
        /* Skip this outbound connection attempt when all these conditions are true:
         *  1. No inbound link from the peer exists.
         *  2. The back‑off window since the last try is still active
         *  3. The node has already exceeded its retry budget for this cron cycle
         */
        if (!node->inbound_link && (now - node->outbound_link_attempt_time < reconnect_interval / NODE_CONNECTION_RETRIES_PER_TIMEOUT && *cluster_conn_attempts == 0)) {
            return 1;
        }
        node->outbound_link_attempt_time = now;
        (*cluster_conn_attempts)--;
        clusterLink *link = createClusterLink(node);
        link->conn = connCreate(connTypeOfCluster());
        connSetPrivateData(link->conn, link);
        if (connConnect(link->conn, node->ip, node->cport, server.bind_source_addr, 0, clusterLinkConnectHandler) ==
            C_ERR) {
            /* We got a synchronous error from connect before
             * clusterSendPing() had a chance to be called.
             * If node->ping_sent is zero, failure detection can't work,
             * so we claim we actually sent a ping now (that will
             * be really sent as soon as the link is obtained). */
            if (node->ping_sent == 0) node->ping_sent = mstime();
            serverLog(LL_DEBUG,
                      "Unable to connect to "
                      "Cluster Node [%s]:%d -> %s",
                      node->ip, node->cport, server.neterr);