当前位置: 首页 > news >正文

bootstrap做网站产品推广策划书

bootstrap做网站,产品推广策划书,花钱做的网站推广被骗,办公室装修设计简约目录测试1go 1.15.15go 1.17.12测试2go 1.15.15go 1.17.12参考在使用golang 连接 mysql时,为了节省连接资源,在连接使用过后,希望在指定长度时间不再使用后,自动关闭连接。 这时,经常会使用SetConnMaxLifetime()&#…

目录

  • 测试1
    • go 1.15.15
    • go 1.17.12
  • 测试2
    • go 1.15.15
    • go 1.17.12
  • 参考

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

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

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

package mainimport ("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 如何设置最大连接数和最大空闲连接数

http://www.hkea.cn/news/531794/

相关文章:

  • 深圳公司名称大全网站结构优化的内容和方法
  • 安康市代驾公司上海网站关键词排名优化报价
  • 怎么在网站上建设投票统计在线培训系统app
  • 泰州网站建设哪家好网站seo的主要优化内容
  • 洛卡博网站谁做的seo权重查询
  • 东莞网络科技公司有哪些山东网站seo
  • 网站建设需要学什么网站模板购买
  • 用html做的游戏网站关键词推广效果分析
  • 做影视网站引流正规推广平台有哪些
  • 免费下载简历模板北京seo排名厂家
  • 西昌市做网站的百度搜索排名靠前
  • 办公室装修实景拍摄图重庆seo俱乐部联系方式
  • 网站建设阶段推广计划书怎么写
  • 代做毕业设计网站现成注册网站平台
  • 电商网站开发工作计划企业网络营销策划
  • 用wps网站栏目做树形结构图网页设计代码案例
  • 多媒体网站设计开发是指什么每日关键词搜索排行
  • 网站 seo正规网络公司关键词排名优化
  • 建立网站赚多少钱seo收录排名
  • 怎么做app网站seo学习网站
  • 广西建设职业技术学院官网免费的seo优化
  • 凡科网电脑版怎么做网站百度知道官网手机版
  • 贵卅省住房和城乡建设厅网站周口seo推广
  • 搭建flv视频网站seo工具查询
  • 企业展示网站 数据库设计模板自助建站
  • 房地产设计师上海seo网络优化
  • wordpress迁移打不开百度seo泛解析代发排名
  • 网站兼容性测试怎么做微信营销软件群发
  • wordpress如何设置内容页seo营销优化
  • 高端大气的网站制作南宁百度seo软件