From 2548a847a75f70f40b9fed998caa3621964b1723 Mon Sep 17 00:00:00 2001 From: Piotr Grabowski Date: Thu, 31 Oct 2024 16:25:51 +0100 Subject: [PATCH] Make ClickHouse connection configuration more user-friendly Before this change, Quesma supported only the "clickhouse://" protocol to connect to ClickHouse. However, ClickHouse also supports the HTTP protocol (8123) and HTTPS protocol (8443). This PR extends the ClickHouse connection logic to switch to using HTTP(S) protocol if the user put that endpoint instead. This change is motivated by the fact that the HTTPS endpoint is the first/default endpoint shown on ClickHouse Cloud and by the fact that we saw a user try to use the HTTP endpoint. --- quesma/clickhouse/connection.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/quesma/clickhouse/connection.go b/quesma/clickhouse/connection.go index e489fc727..1f2af9ccb 100644 --- a/quesma/clickhouse/connection.go +++ b/quesma/clickhouse/connection.go @@ -16,8 +16,8 @@ import ( ) func initDBConnection(c *config.QuesmaConfiguration, tlsConfig *tls.Config) *sql.DB { - options := clickhouse.Options{Addr: []string{c.ClickHouse.Url.Host}} + if c.ClickHouse.User != "" || c.ClickHouse.Password != "" || c.ClickHouse.Database != "" { options.Auth = clickhouse.Auth{ @@ -29,6 +29,18 @@ func initDBConnection(c *config.QuesmaConfiguration, tlsConfig *tls.Config) *sql if !c.ClickHouse.DisableTLS { options.TLS = tlsConfig } + if c.ClickHouse.Url.Scheme == "clickhouse" { + options.Protocol = clickhouse.Native + } else if c.ClickHouse.Url.Scheme == "http" { + logger.Warn().Msgf("Using HTTP protocol to connect to ClickHouse. We recommend using ClickHouse Native protocol instead (default port 9000).") + options.Protocol = clickhouse.HTTP + options.TLS = nil + } else if c.ClickHouse.Url.Scheme == "https" { + logger.Warn().Msgf("Using HTTPS protocol to connect to ClickHouse. We recommend using ClickHouse Native protocol instead (default port 9000).") + options.Protocol = clickhouse.HTTP + } else { + logger.Error().Msgf("Unknown ClickHouse endpoint protocol '%s' in the provided URL %s. Defaulting to ClickHouse Native protocol. Please provide the ClickHouse URL in the following format: clickhouse://host:port", c.ClickHouse.Url.Scheme, c.ClickHouse.Url.String()) + } info := struct { Name string