diff --git a/conf/defaults.ini b/conf/defaults.ini index 358847724ab..4606de9212e 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -73,6 +73,11 @@ password = # Example: mysql://user:secret@host:port/database url = +# Max conn setting default is 0 (mean not set) +max_conn = +max_idle_conn = +max_open_conn = + # For "postgres", use either "disable", "require" or "verify-full" # For "mysql", use either "true", "false", or "skip-verify". ssl_mode = disable diff --git a/conf/sample.ini b/conf/sample.ini index ce9344e1d4f..eaedfcc6f5e 100644 --- a/conf/sample.ini +++ b/conf/sample.ini @@ -82,6 +82,12 @@ # For "sqlite3" only, path relative to data_path setting ;path = grafana.db +# Max conn setting default is 0 (mean not set) +;max_conn = +;max_idle_conn = +;max_open_conn = + + #################################### Session #################################### [session] # Either "memory", "file", "redis", "mysql", "postgres", default is "file" diff --git a/pkg/services/sqlstore/sqlstore.go b/pkg/services/sqlstore/sqlstore.go index 5c44fe85f50..0d16d57bcde 100644 --- a/pkg/services/sqlstore/sqlstore.go +++ b/pkg/services/sqlstore/sqlstore.go @@ -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() }