Skip to content

Commit

Permalink
parseDSN: support connection pool settings (#1082) (#1084)
Browse files Browse the repository at this point in the history
* parseDSN: support connection pool settings (#1082)

* add unit test

---------

Co-authored-by: Kuba Kaflik <[email protected]>
  • Loading branch information
hanjm and jkaflik authored Sep 14, 2023
1 parent 3eb995b commit 596ddb2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
18 changes: 18 additions & 0 deletions clickhouse_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
18 changes: 18 additions & 0 deletions clickhouse_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package clickhouse
import (
"crypto/tls"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 596ddb2

Please sign in to comment.