为什么在连接mysql时,设置 SetConnMaxIdleTime 没有作用

112 篇文章 6 订阅
25 篇文章 8 订阅

在使用golang 连接 mysql时,为了节省连接资源,在连接使用过后,希望在指定长度时间不再使用后,自动关闭连接。
这时,经常会使用SetConnMaxLifetime(),设置最大连接有效时间,
使用SetConnMaxIdleTime(),设置最大空闲连接时间 max idle time。

这两个函数的作用听起来差不多,理论上,都能达到相同效果。
但是在实际使用中,却有些出人意料。

首先看下测试代码,在以下代码中,
设置 max idle time 为传入的参数值,
设置 max life time 为传入的参数值,
设置 max open conn 为1,
设置 max idle conn 为1

package main

import (
	"database/sql"
	"log"
	"fmt"
	"os"
	"strconv"
	"time"
	
	_ "github.com/go-sql-driver/mysql"
)

var dataBase = "root:xxxx@tcp(127.0.0.1:3306)/mysql?timeout=2s&readTimeout=6s&interpolateParams=true"

func getVar(name string) int {
	val := os.Getenv(name)
	if len(val) == 0 {
		panic(fmt.Sprintf("error getting: %v", name))
	}
	v, err := strconv.Atoi(val)
	if err != nil {
		panic(fmt.Sprintf("error parsing %v %v", name, err))
	}
	return v
}



func main() {
	db, err :=sql.Open("mysql", dataBase)  // connect to the db of your choice.
	if err != nil {
		panic(err)
	}
	defer db.Close()
	db.SetConnMaxIdleTime(time.Second * time.Duration(getVar("MAXIDLE")))
	db.SetConnMaxLifetime(time.Second * time.Duration(getVar("MAXLIFE")))
	db.SetMaxIdleConns(1)
	db.SetMaxOpenConns(1)
	sleep := time.Second*time.Duration(getVar("SLEEP"))
	for i := 0; i < 10; i++ {
		err = db.Ping()
		if err != nil {
            log.Fatalln("ping db fail:", err)
        }
		time.Sleep(sleep)
		print("\r", i)
	}
	fmt.Printf("\n%+v\n", db.Stats())
}

测试1

MAXIDLE=1 MAXLIFE=0 SLEEP=5 go run .

设置 max idle time 为1s,设置max life time为0s,也就是永不过期,相当于不设置life time。
每次连接之后,sleep 5s。

预期结果,循环10次,每次都会打开一个新的连接,旧的连接由于idle time到期而自动关闭。

go 1.15.15

output

9
{MaxOpenConnections:1 OpenConnections:1 InUse:0 Idle:1 WaitCount:0 WaitDuration:0s MaxIdleClosed:0 MaxIdleTimeClosed:0 MaxLifetimeClosed:0}

实际结果,只新建了一个连接,一直在使用,没有关闭连接。
也就是说设置max idle time 并没有生效。

go 1.17.12

9
{MaxOpenConnections:1 OpenConnections:0 InUse:0 Idle:0 WaitCount:0 WaitDuration:0s MaxIdleClosed:0 MaxIdleTimeClosed:10 MaxLifetimeClosed:0}

实际结果与预期相符。

测试2

MAXIDLE=1 MAXLIFE=2 SLEEP=5 go run .

设置 max idle time 为1s,设置max life time为2s,也就是idle time先到期
每次连接之后,sleep 5s。

预期结果,循环10次,每次都会打开一个新的连接,旧的连接是由于idle time到期自动关闭。

go 1.15.15

output

9
{MaxOpenConnections:1 OpenConnections:0 InUse:0 Idle:0 WaitCount:0 WaitDuration:0s MaxIdleClosed:0 MaxIdleTimeClosed:10 MaxLifetimeClosed:0}

实际结果与预期相符。

go 1.17.12

output

9
{MaxOpenConnections:1 OpenConnections:0 InUse:0 Idle:0 WaitCount:0 WaitDuration:0s MaxIdleClosed:0 MaxIdleTimeClosed:10 MaxLifetimeClosed:0}

实际结果与预期相符。

总结下,汇总以上测试结果如下表所示:

go版本是否 max idle time是否max life time空闲连接回收是否生效
go 1.15.15YNN
go 1.15.15YYY
go 1.17.12YNY
go 1.17.12YYY

在 go 1.15.15版本 或者其他相近版本中,只设置max idle time,不能自动回收空闲连接。

具体原因,可以参见 issue,或者具体查看go 源码,这应该是一个bug。

参考

database/sql: SetConnMaxIdleTime without SetConnMaxLifetime has no effect #41114

golang mysql 如何设置最大连接数和最大空闲连接数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值