From 596ddb250c981a825c5e9c08d14a6214819f01fe Mon Sep 17 00:00:00 2001 From: Jimmie Han Date: Thu, 14 Sep 2023 17:50:51 +0800 Subject: [PATCH] parseDSN: support connection pool settings (#1082) (#1084) * parseDSN: support connection pool settings (#1082) * add unit test --------- Co-authored-by: Kuba Kaflik --- clickhouse_options.go | 18 ++++++++++++++++++ clickhouse_options_test.go | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/clickhouse_options.go b/clickhouse_options.go index c6b4283ef9..d64b04eb89 100644 --- a/clickhouse_options.go +++ b/clickhouse_options.go @@ -266,6 +266,24 @@ func (o *Options) fromDSN(in string) error { case "round_robin": o.ConnOpenStrategy = ConnOpenRoundRobin } + case "max_open_conns": + maxOpenConns, err := strconv.Atoi(params.Get(v)) + if err != nil { + return errors.Wrap(err, "max_open_conns invalid value") + } + o.MaxOpenConns = maxOpenConns + case "max_idle_conns": + maxIdleConns, err := strconv.Atoi(params.Get(v)) + if err != nil { + return errors.Wrap(err, "max_idle_conns invalid value") + } + o.MaxIdleConns = maxIdleConns + case "conn_max_lifetime": + connMaxLifetime, err := time.ParseDuration(params.Get(v)) + if err != nil { + return errors.Wrap(err, "conn_max_lifetime invalid value") + } + o.ConnMaxLifetime = connMaxLifetime case "username": o.Auth.Username = params.Get(v) case "password": diff --git a/clickhouse_options_test.go b/clickhouse_options_test.go index bfce52f1d1..a39aeccc7f 100644 --- a/clickhouse_options_test.go +++ b/clickhouse_options_test.go @@ -20,6 +20,7 @@ package clickhouse import ( "crypto/tls" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -449,6 +450,23 @@ func TestParseDSN(t *testing.T) { }, "", }, + { + "client connection pool settings", + "clickhouse://127.0.0.1/test_database?max_open_conns=-1&max_idle_conns=0&conn_max_lifetime=1h", + &Options{ + Protocol: Native, + MaxOpenConns: -1, + MaxIdleConns: 0, + ConnMaxLifetime: time.Hour, + Addr: []string{"127.0.0.1"}, + Settings: Settings{}, + Auth: Auth{ + Database: "test_database", + }, + scheme: "clickhouse", + }, + "", + }, } for _, testCase := range testCases {