1 Star 0 Fork 0

玟兵/go-util

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mysql_conn_option.go 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
玟兵 提交于 2024-07-02 17:09 +08:00 . v2 start
package util
import (
"github.com/go-sql-driver/mysql"
"time"
)
type MysqlConnOption struct {
Dsn string
Addr string
DbName string
MaxOpen int
MaxIdle int
MaxIdleTime time.Duration
MaxLifetime time.Duration
}
func NewMysqlConnOption(
dsn string,
maxOpen, maxIdle int,
maxIdleTime, maxLifeTime time.Duration,
) (*MysqlConnOption, error) {
addr := ""
dbName := ""
if cfg, err := mysql.ParseDSN(dsn); err != nil {
return nil, err
} else if cfg.Addr == "" || cfg.DBName == "" {
return nil, ErrMysqlWrongDsn
} else {
addr = cfg.Addr
dbName = cfg.DBName
}
if maxOpen <= 0 {
maxOpen = NumCPU()*2 + 1
}
if maxIdle <= 0 {
maxIdle = maxOpen / 2
if maxIdle < 2 {
maxIdle = 2
}
}
if maxIdleTime < 0 {
maxIdleTime = 3 * time.Minute
}
if maxLifeTime < 0 {
maxLifeTime = 30 * time.Minute
}
return &MysqlConnOption{
Dsn: dsn,
Addr: addr,
DbName: dbName,
MaxOpen: maxOpen,
MaxIdle: maxIdle,
MaxIdleTime: maxIdleTime,
MaxLifetime: maxLifeTime,
}, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/binny_w/go-util.git
git@gitee.com:binny_w/go-util.git
binny_w
go-util
go-util
v0.0.49

搜索帮助