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

Code before:
        return 1;
    }

    return 0;
}


void readQueryFromClient(connection *conn) {
    client *c = connGetPrivateData(conn);
    /* Check if we can send the client to be handled by the IO-thread */
    if (postponeClientRead(c)) return;

    if (c->io_write_state != CLIENT_IDLE || c->io_read_state != CLIENT_IDLE) return;

    bool shouldRepeat = false;
    int iter = 0;
    do {
        readToQueryBuf(c);

        if (handleReadResult(c) == C_OK) {
            if (processInputBuffer(c) == C_ERR) return;
        }
        iter++;
        shouldRepeat = shouldRepeatReadFromPrimary(c, iter);
        beforeNextClient(c);
    } while (shouldRepeat);
}

/* An "Address String" is a colon separated ip:port pair.
 * For IPv4 it's in the form x.y.z.k:port, example: "127.0.0.1:1234".
 * For IPv6 addresses we use [] around the IP part, like in "[::1]:1234".
 * For Unix sockets we use path:0, like in "/tmp/redis:0".
 *
 * An Address String always fits inside a buffer of NET_ADDR_STR_LEN bytes,
 * including the null term.
 *
 * On failure the function still populates 'addr' with the "?:0" string in case
 * you want to relax error checking or need to display something anyway (see
 * anetFdToString implementation for more info). */
void genClientAddrString(client *client, char *addr, size_t addr_len, int remote) {
    if (client->flag.unix_socket) {
        /* Unix socket client. */
        snprintf(addr, addr_len, "%s:0", server.unixsocket);
    } else {
        /* TCP client. */
        connFormatAddr(client->conn, addr, addr_len, remote);
    }
}

/* This function returns the client peer id, by creating and caching it
 * if client->peerid is NULL, otherwise returning the cached value.

Review comment (suggestion):
Does shouldRepeatReadFromPrimary need to be called before beforeNextClient? If not, we can do the check in the while condition:

```suggestion
        beforeNextClient(c);
    } while (shouldRepeatReadFromPrimary(c, iter));
```

Code after:
    bool repeat = false;
    int iter = 0;
    do {
        bool full_read = readToQueryBuf(c);
        if (handleReadResult(c) == C_OK) {
            if (processInputBuffer(c) == C_ERR) return;
        }
        repeat = (c->flag.primary &&
                  !c->flag.close_asap &&
                  ++iter < REPL_MAX_READS_PER_IO_EVENT &&
                  full_read);
        beforeNextClient(c);
    } while (repeat);
}

/* An "Address String" is a colon separated ip:port pair.
 * For IPv4 it's in the form x.y.z.k:port, example: "127.0.0.1:1234".
 * For IPv6 addresses we use [] around the IP part, like in "[::1]:1234".
 * For Unix sockets we use path:0, like in "/tmp/redis:0".
 *
 * An Address String always fits inside a buffer of NET_ADDR_STR_LEN bytes,
 * including the null term.
 *
 * On failure the function still populates 'addr' with the "?:0" string in case
 * you want to relax error checking or need to display something anyway (see
 * anetFdToString implementation for more info). */
void genClientAddrString(client *client, char *addr, size_t addr_len, int remote) {
    if (client->flag.unix_socket) {
        /* Unix socket client. */
        snprintf(addr, addr_len, "%s:0", server.unixsocket);
    } else {
        /* TCP client. */
        connFormatAddr(client->conn, addr, addr_len, remote);
    }
}

/* This function returns the client peer id, by creating and caching it
 * if client->peerid is NULL, otherwise returning the cached value.
 * The Peer ID never changes during the life of the client, however it
 * is expensive to compute. */
char *getClientPeerId(client *c) {
    char peerid[NET_ADDR_STR_LEN] = {0};

    if (c->peerid == NULL) {
        genClientAddrString(c, peerid, sizeof(peerid), 1);
        c->peerid = sdsnew(peerid);
    }
    return c->peerid;
}

/* This function returns the client bound socket name, by creating and caching