(feat) support max connection setting for database configuration

This commit is contained in:
huydx
2017-02-10 12:31:55 +09:00
committed by bergquist
parent 3c7cf3f728
commit 28d93b574d
2 changed files with 20 additions and 1 deletions

View File

@@ -29,6 +29,9 @@ type DatabaseConfig struct {
ClientKeyPath string
ClientCertPath string
ServerCertName string
MaxConn int
MaxOpenConn int
MaxIdleConn int
}
var (
@@ -150,7 +153,15 @@ func getEngine() (*xorm.Engine, error) {
}
sqlog.Info("Initializing DB", "dbtype", DbCfg.Type)
return xorm.NewEngine(DbCfg.Type, cnnstr)
engine, err := xorm.NewEngine(DbCfg.Type, cnnstr)
if err != nil {
return nil, err
} else {
engine.SetMaxConns(DbCfg.MaxConn)
engine.SetMaxOpenConns(DbCfg.MaxOpenConn)
engine.SetMaxIdleConns(DbCfg.MaxIdleConn)
}
return engine, nil
}
func LoadConfig() {
@@ -177,6 +188,9 @@ func LoadConfig() {
DbCfg.Host = sec.Key("host").String()
DbCfg.Name = sec.Key("name").String()
DbCfg.User = sec.Key("user").String()
DbCfg.MaxConn = sec.Key("max_conn").MustInt(0)
DbCfg.MaxOpenConn = sec.Key("max_open_conn").MustInt(0)
DbCfg.MaxIdleConn = sec.Key("max_idle_conn").MustInt(0)
if len(DbCfg.Pwd) == 0 {
DbCfg.Pwd = sec.Key("password").String()
}