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

Code before:

/* Some flags (currently just the NOFAILOVER flag) may need to be updated
 * in the "myself" node based on the current configuration of the node,
 * that may change at runtime via CONFIG SET. This function changes the
 * set of flags in myself->flags accordingly. */
void clusterUpdateMyselfFlags(void) {
    if (!myself) return;
    int oldflags = myself->flags;
    int nofailover = server.cluster_replica_no_failover ? CLUSTER_NODE_NOFAILOVER : 0;
    myself->flags &= ~CLUSTER_NODE_NOFAILOVER;
    myself->flags |= nofailover;
    myself->flags |= CLUSTER_NODE_EXTENSIONS_SUPPORTED |
                     CLUSTER_NODE_LIGHT_HDR_PUBLISH_SUPPORTED |
                     CLUSTER_NODE_LIGHT_HDR_MODULE_SUPPORTED;
    if (myself->flags != oldflags) {
        clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_UPDATE_STATE);
    }
}


/* We want to take myself->port/cport/pport in sync with the
 * cluster-announce-port/cluster-announce-bus-port/cluster-announce-tls-port option.
 * The option can be set at runtime via CONFIG SET. */
void clusterUpdateMyselfAnnouncedPorts(void) {
    if (!myself) return;
    deriveAnnouncedPorts(&myself->tcp_port, &myself->tls_port, &myself->cport, &myself->announce_client_tcp_port, &myself->announce_client_tls_port);
    clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
}

/* We want to take myself->ip in sync with the cluster-announce-ip option.
 * The option can be set at runtime via CONFIG SET. */
void clusterUpdateMyselfIp(void) {
    if (!myself) return;
    static char *prev_ip = NULL;
    char *curr_ip = server.cluster_announce_ip;
    int changed = 0;

    if (prev_ip == NULL && curr_ip != NULL)
        changed = 1;
    else if (prev_ip != NULL && curr_ip == NULL)
        changed = 1;
    else if (prev_ip && curr_ip && strcmp(prev_ip, curr_ip))
        changed = 1;

    if (changed) {
        if (prev_ip) zfree(prev_ip);
        prev_ip = curr_ip;

        if (curr_ip) {
            /* We always take a copy of the previous IP address, by
             * duplicating the string. This way later we can check if

Review comment (suggestion):
```suggestion
    deriveAnnouncedPorts(&myself->tcp_port, &myself->tls_port, &myself->cport,
                         &myself->announce_client_tcp_port, &myself->announce_client_tls_port);
```

Code after:

/* Some flags (currently just the NOFAILOVER flag) may need to be updated
 * in the "myself" node based on the current configuration of the node,
 * that may change at runtime via CONFIG SET. This function changes the
 * set of flags in myself->flags accordingly. */
void clusterUpdateMyselfFlags(void) {
    if (!myself) return;
    int oldflags = myself->flags;
    int nofailover = server.cluster_replica_no_failover ? CLUSTER_NODE_NOFAILOVER : 0;
    myself->flags &= ~CLUSTER_NODE_NOFAILOVER;
    myself->flags |= nofailover;
    myself->flags |= CLUSTER_NODE_EXTENSIONS_SUPPORTED |
                     CLUSTER_NODE_LIGHT_HDR_PUBLISH_SUPPORTED |
                     CLUSTER_NODE_LIGHT_HDR_MODULE_SUPPORTED;
    if (myself->flags != oldflags) {
        clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_UPDATE_STATE);
    }
}


/* We want to take myself->port/cport/pport in sync with the
 * cluster-announce-port/cluster-announce-bus-port/cluster-announce-tls-port option.
 * The option can be set at runtime via CONFIG SET. */
void clusterUpdateMyselfAnnouncedPorts(void) {
    if (!myself) return;
    deriveAnnouncedPorts(&myself->tcp_port, &myself->tls_port, &myself->cport,
                         &myself->announce_client_tcp_port, &myself->announce_client_tls_port);
    clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);
}

/* We want to take myself->ip in sync with the cluster-announce-ip option.
 * The option can be set at runtime via CONFIG SET. */
void clusterUpdateMyselfIp(void) {
    if (!myself) return;
    static char *prev_ip = NULL;
    char *curr_ip = server.cluster_announce_ip;
    int changed = 0;

    if (prev_ip == NULL && curr_ip != NULL)
        changed = 1;
    else if (prev_ip != NULL && curr_ip == NULL)
        changed = 1;
    else if (prev_ip && curr_ip && strcmp(prev_ip, curr_ip))
        changed = 1;

    if (changed) {
        if (prev_ip) zfree(prev_ip);
        prev_ip = curr_ip;

        if (curr_ip) {
            /* We always take a copy of the previous IP address, by