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

北京网站优化网站文章来源seo

北京网站优化,网站文章来源seo,学视频剪辑去哪里学比较好,绥中做网站公司目录测试1go 1.15.15go 1.17.12测试2go 1.15.15go 1.17.12参考在使用golang 连接 mysql时#xff0c;为了节省连接资源#xff0c;在连接使用过后#xff0c;希望在指定长度时间不再使用后#xff0c;自动关闭连接。 这时#xff0c;经常会使用SetConnMaxLifetime()#… 目录测试1go 1.15.15go 1.17.12测试2go 1.15.15go 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/sqllogfmtosstrconvtime_ github.com/go-sql-driver/mysql )var dataBase root:xxxxtcp(127.0.0.1:3306)/mysql?timeout2sreadTimeout6sinterpolateParamstruefunc 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 MAXIDLE1 MAXLIFE0 SLEEP5 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 MAXIDLE1 MAXLIFE2 SLEEP5 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.15YNNgo 1.15.15YYYgo 1.17.12YNYgo 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.w-s-a.com/news/994458/

相关文章:

  • 网站拍照的幕布扬中网站建设价位
  • 网站ie兼容性差西安小程序开发的公司
  • 上海网站建设培训app网站开发成本
  • 个人网站icp外贸网站开发 河南
  • 遵义建设网站无锡市规划建设局网站
  • 海外留学网站建设方案门户网站的发布特点
  • 网站建设不赚钱net112企业建站系统
  • 网站建设团队管理模板贵州省住房和城乡建设部网站
  • 曲沃网站建设网上学编程的有哪些比较好的网站
  • 厦门网站建设慕枫学做网站需要多久
  • 爱奇艺做任务领vip网站设计广告图片
  • 中科汇联网站建设手册上海公司名称注册查询网
  • 网站建设电子商务课总结和体会关于做网站书籍
  • 仪征网站建设公司哪家好简单网页制作素材图片
  • 甘肃第九建设集团公司网站潍坊个人做网站
  • 如何做后台网站的教程网站建设 关于我们
  • 极速网站建设哪家好连云港百度推广网站建设
  • 医院网站建设的目标wordpress中英文网站模板
  • 门户型网站开发难度网站导航栏有哪些
  • 推荐做任务网站软件定制开发哪家好
  • 邯郸兄弟建站第三方仓储配送公司
  • 商丘家具网站建设wordpress 添加代码
  • 基础建设的网站有哪些内容成都科技网站建设咨询电话
  • 券多多是谁做的网站招聘网站开发模板
  • 网站主机一般选哪种的企业数字展厅
  • 网站建设该如何学衡水建设局网站首页
  • 高校网站建设工作总结番禺网站开发哪家好
  • 苏州 网站的公司wordpress主页代码
  • 怎么用html做图片展示网站外贸网站建设推广费用
  • 可以做本地生活服务的有哪些网站中油七建公司官网