Skip to content

Commit

Permalink
switch to http port if connection with native port failed
Browse files Browse the repository at this point in the history
  • Loading branch information
k-anshul committed Dec 20, 2024
1 parent ff30bcd commit f12cc0b
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions runtime/drivers/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"errors"
"fmt"
"strings"

"github.com/ClickHouse/clickhouse-go/v2"
"github.com/XSAM/otelsql"
Expand Down Expand Up @@ -187,6 +188,24 @@ func (d driver) Open(instanceID string, config map[string]any, st *storage.Clien
}

db := sqlx.NewDb(otelsql.OpenDB(clickhouse.Connector(opts)), "clickhouse")
err = db.Ping()
if err != nil {
if !strings.Contains(err.Error(), "unexpected packet") && !strings.Contains(err.Error(), "i/o timeout") {
return nil, err
}
if conf.DSN != "" {
return nil, err
}
// may be the port is http, also try with http protocol if DSN is not provided
opts.Protocol = clickhouse.HTTP
db = sqlx.NewDb(otelsql.OpenDB(clickhouse.Connector(opts)), "clickhouse")
err := db.Ping()
if err != nil {
return nil, err
}
// connection with http protocol is successful
logger.Warn("clickHouse connection is established with HTTP protocol. Use native port for better performance")
}
// very roughly approximating num queries required for a typical page load
// TODO: copied from druid reevaluate
db.SetMaxOpenConns(maxOpenConnections)
Expand All @@ -196,11 +215,6 @@ func (d driver) Open(instanceID string, config map[string]any, st *storage.Clien
return nil, fmt.Errorf("registering db stats metrics: %w", err)
}

err = db.Ping()
if err != nil {
return nil, fmt.Errorf("connection: %w", err)
}

// group by positional args are supported post 22.7 and we use them heavily in our queries
row := db.QueryRow(`
WITH
Expand Down

0 comments on commit f12cc0b

Please sign in to comment.