golang clickhouse 错误 invalid settings. use SetMaxIdleConns,SetMaxOpenConns,SetConnMaxLifetime

用户在使用clickhouse时遇到连接问题,因设置限制参数引发错误。解决方法是移除options中的对应参数并动态调整。
摘要由CSDN通过智能技术生成

第一次使用 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))
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值