Repository: ClickHouse/ClickHouse
File: src/Client/Connection.cpp (C++)

Code before:
    setDescription();
}


void Connection::connect(const ConnectionTimeouts & timeouts)
{
    /// if connection was broken it is necessary to cancel it before reconnecting
    disconnect();

    try
    {
        LOG_TRACE(log_wrapper.get(), "Connecting. Database: {}. User: {}{}{}. Bind_Host: {}",
            default_database.empty() ? "(not specified)" : default_database,
            user,
            static_cast<bool>(secure) ? ". Secure" : "",
            static_cast<bool>(compression) ? "" : ". Uncompressed",
            bind_host.empty() ? "(not specified)" : bind_host);

        auto addresses = DNSResolver::instance().resolveAddressList(host, port);
        const auto & connection_timeout = static_cast<bool>(secure) ? timeouts.secure_connection_timeout : timeouts.connection_timeout;

        for (auto it = addresses.begin(); it != addresses.end();)
        {
            have_more_addresses_to_connect = it != std::prev(addresses.end());

            LOG_TRACE(log_wrapper.get(), "Connecting to {}:{}, address: {}, have_more_addresses_to_connect: {}", host, port, it->toString(), have_more_addresses_to_connect);

            if (isConnected())
                disconnect();

            if (static_cast<bool>(secure))
            {
#if USE_SSL
                socket = std::make_unique<Poco::Net::SecureStreamSocket>();

                /// we resolve the ip when we open SecureStreamSocket, so to make Server Name Indication (SNI)
                /// work we need to pass host name separately. It will be send into TLS Hello packet to let
                /// the server know which host we want to talk with (single IP can process requests for multiple hosts using SNI).
                static_cast<Poco::Net::SecureStreamSocket *>(socket.get())
                    ->setPeerHostName(tls_sni_override.empty() ? host : tls_sni_override);
                /// we want to postpone SSL handshake until first read or write operation
                /// so any errors during negotiation would be properly processed
                static_cast<Poco::Net::SecureStreamSocket*>(socket.get())->setLazyHandshake(true);

                if (!bind_host.empty())
                {
                    Poco::Net::SocketAddress socket_address(bind_host, 0);

                    static_cast<Poco::Net::SecureStreamSocket*>(socket.get())->bind(socket_address, true);
                }
#else

Review comment (suggestion):
```suggestion
            LOG_TRACE(log_wrapper.get(), "Connecting to {}:{}, address: {}, have_more_addresses_to_connect: {}", host, port, it->toString(), have_more_addresses_to_connect);
```

Let's not use internal variables, and write something more user friendly, and log which address we are trying out of what, i.e. `("Connecting to {}:{} (using {}, {}/{})", host, port, it->toString(), std::distance(addresses.begin(), it), addresses.size())`

Code after:
    setDescription();
}


void Connection::connect(const ConnectionTimeouts & timeouts)
{
    /// if connection was broken it is necessary to cancel it before reconnecting
    disconnect();

    try
    {
        LOG_TRACE(log_wrapper.get(), "Connecting. Database: {}. User: {}{}{}. Bind_Host: {}",
            default_database.empty() ? "(not specified)" : default_database,
            user,
            static_cast<bool>(secure) ? ". Secure" : "",
            static_cast<bool>(compression) ? "" : ". Uncompressed",
            bind_host.empty() ? "(not specified)" : bind_host);

        auto addresses = DNSResolver::instance().resolveAddressList(host, port);
        const auto & connection_timeout = static_cast<bool>(secure) ? timeouts.secure_connection_timeout : timeouts.connection_timeout;

        for (auto it = addresses.begin(); it != addresses.end();)
        {
            have_more_addresses_to_connect = it != std::prev(addresses.end());

            LOG_TRACE(log_wrapper.get(), "Connecting to {}:{} (using address {}, {}/{})", host, port, it->toString(), std::distance(addresses.begin(), it) + 1, addresses.size());

            if (isConnected())
                disconnect();

            if (static_cast<bool>(secure))
            {
#if USE_SSL
                socket = std::make_unique<Poco::Net::SecureStreamSocket>();

                /// we resolve the ip when we open SecureStreamSocket, so to make Server Name Indication (SNI)
                /// work we need to pass host name separately. It will be send into TLS Hello packet to let
                /// the server know which host we want to talk with (single IP can process requests for multiple hosts using SNI).
                static_cast<Poco::Net::SecureStreamSocket *>(socket.get())
                    ->setPeerHostName(tls_sni_override.empty() ? host : tls_sni_override);
                /// we want to postpone SSL handshake until first read or write operation
                /// so any errors during negotiation would be properly processed
                static_cast<Poco::Net::SecureStreamSocket*>(socket.get())->setLazyHandshake(true);

                if (!bind_host.empty())
                {
                    Poco::Net::SocketAddress socket_address(bind_host, 0);

                    static_cast<Poco::Net::SecureStreamSocket*>(socket.get())->bind(socket_address, true);
                }
#else