10 Star 31 Fork 5

夏午茶 / sago

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
DB_mysql.go 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
gjy 提交于 2019-09-05 18:10 . 协程池优化
package sago
import (
"gitee.com/xiawucha365/sago/internal/logger"
"github.com/go-xorm/xorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"sync"
"time"
)
var (
once sync.Once
//通用模式,可以调用xorm底层方法
MysqlEngine *DbEngine
)
type DbEngine = xorm.Engine
func CreateMysqlDialect() *DbEngine {
once.Do(func() {
var err error
if MysqlEngine, err = xorm.NewEngine("mysql",
GConfig.Mysql.Username+":"+GConfig.Mysql.Password+
"@tcp("+GConfig.Mysql.Addr+")/"+GConfig.Mysql.Dbname+"?charset="+
GConfig.Mysql.Charset+"&parseTime=True&loc=Local"); err != nil {
logger.Error("mysqlconn", err)
}
//mysqlConn.ShowSQL(true)
//engine, err = xorm.NewEngine("mysql", comm.G_config.Mysql.Username+":"+comm.G_config.Mysql.Password+"@/"+comm.G_config.Mysql.Dbname+"?charset=utf8")
//连接数默认设置
//连接数默认设置
MysqlEngine.SetMaxOpenConns(120)
MysqlEngine.SetMaxIdleConns(100)
MysqlEngine.SetConnMaxLifetime(time.Second * 20)
//探活
go keepDbAlived(MysqlEngine)
})
return MysqlEngine
}
func keepDbAlived(engine *xorm.Engine) {
t := time.Tick(180 * time.Second)
for {
<-t
engine.Ping()
}
}
Go
1
https://gitee.com/xiawucha365/sago.git
git@gitee.com:xiawucha365/sago.git
xiawucha365
sago
sago
v1.1.0

搜索帮助

53164aa7 5694891 3bd8fe86 5694891