Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clickhouse: add more driver connection options #6422

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions runtime/drivers/clickhouse/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ type configProperties struct {
// EmbedPort is the port to run Clickhouse locally (0 is random port).
EmbedPort int `mapstructure:"embed_port"`
CanScaleToZero bool `mapstructure:"can_scale_to_zero"`
// MaxOpenConns is the maximum number of open connections to the database.
// https://github.com/ClickHouse/clickhouse-go/blob/main/clickhouse_options.go
MaxOpenConns int `mapstructure:"max_open_conns"`
// MaxIdleConns is the maximum number of connections in the idle connection pool. Default is 5s.
MaxIdleConns int `mapstructure:"max_idle_conns"`
// DialTimeout is the timeout for dialing the Clickhouse server. Default is 30s.
// key: dial_timeout
DialTimeout time.Duration `mapstructure:"-"` // Temporarily exclude Timeout from automatic decoding
// ConnMaxLifetime is the maximum amount of time a connection may be reused.
// key: conn_max_lifetime
ConnMaxLifetime time.Duration `mapstructure:"-"` // Temporarily exclude Timeout from automatic decoding
// ReadTimeout is the maximum amount of time a connection may be reused. Default is 300s.
// key: read_timeout
ReadTimeout time.Duration `mapstructure:"-"` // Temporarily exclude Timeout from automatic decoding
}

// Open connects to Clickhouse using std API.
Expand Down Expand Up @@ -149,6 +163,18 @@ func (d driver) Open(instanceID string, config map[string]any, st *storage.Clien
} else if conf.Host != "" {
opts = &clickhouse.Options{}

// Anonymous function to parse duration fields
parseDurationField := func(config map[string]interface{}, key string, field *time.Duration) error {
if value, ok := config[key].(string); ok {
parsedDuration, err := time.ParseDuration(value)
if err != nil {
return fmt.Errorf("failed to parse '%s': %w", key, err)
}
*field = parsedDuration
}
return nil
}

// address
host := conf.Host
if conf.Port != 0 {
Expand All @@ -174,6 +200,31 @@ func (d driver) Open(instanceID string, config map[string]any, st *storage.Clien
if conf.Database != "" {
opts.Auth.Database = conf.Database
}

// max_open_conns
if conf.MaxOpenConns != 0 {
maxOpenConnections = conf.MaxOpenConns
}

// max_idle_conns
if conf.MaxIdleConns != 0 {
opts.MaxIdleConns = conf.MaxIdleConns
}

// dial_timeout
if err := parseDurationField(config, "dial_timeout", &conf.DialTimeout); err != nil {
return nil, err
}

// conn_max_lifetime
if err := parseDurationField(config, "conn_max_lifetime", &conf.ConnMaxLifetime); err != nil {
return nil, err
}

// read_timeout
if err := parseDurationField(config, "read_timeout", &conf.ReadTimeout); err != nil {
return nil, err
}
} else if conf.Provision {
// run clickhouse locally
dataDir, err := st.DataDir(instanceID)
Expand Down
Loading