第一次使用 clickhouse
在创建连接时出现了这个错误
invalid settings. use SetMaxIdleConns,SetMaxOpenConns,SetConnMaxLifetime
我的使用方法
options := click.Options{
Addr: ch.Addresses,
Auth: click.Auth{
Database: ch.Database,
Username: ch.Username,
Password: ch.Password,
},
Debug: true,
DialTimeout: ch.DialTimeout.AsDuration(),
ReadTimeout: ch.ReadTimeout.AsDuration(),
}
sqlDB := click.OpenDB(&options)
cc := clickhouse.Config{
Conn: sqlDB, // initialize with existing database conn
}
稍微往底层翻开看了一下,发现在 OpenDB
的时候限制了这样使用
var debugf = func(format string, v ...any) {}
if opt == nil {
opt = &Options{}
}
var settings []string
if opt.MaxIdleConns > 0 {
settings = append(settings, "SetMaxIdleConns")
}
if opt.MaxOpenConns > 0 {
settings = append(settings, "SetMaxOpenConns")
}
if opt.ConnMaxLifetime > 0 {
settings = append(settings, "SetConnMaxLifetime")
}
if opt.Debug {
debugf = log.New(os.Stdout, "[clickhouse-std][opener] ", 0).Printf
}
if len(settings) != 0 {
return sql.OpenDB(&stdConnOpener{
err: fmt.Errorf("cannot connect. invalid settings. use %s (see https://pkg.go.dev/database/sql)", strings.Join(settings, ",")),
debugf: debugf,
})
}
o := opt.setDefaults()
return sql.OpenDB(&stdConnOpener{
opt: o,
debugf: debugf,
})
通过 settings
判断是否设置了这三个参数中的任何一个,如果使用了就会抛出错误,不理解提供了 options
还要限制使用。
后续的修改办法,第一步就是现在 options
中删除掉这三个参数(先让 OpenDB
顺利通过),然后通过方法设置即可。
sqlDB.SetMaxOpenConns(int(ch.MaxOpenConn))
sqlDB.SetMaxIdleConns(int(ch.MaxIdleConn))
sqlDB.SetConnMaxLifetime(time.Hour * time.Duration(ch.ConnMaxLeftTime))