提交 95326137 authored 作者: mooncake's avatar mooncake

sql slow threshold

上级 2efa54df
......@@ -332,6 +332,7 @@ type Sqlite struct {
type Mysql struct {
ConnMaxLifetime int `yaml:"connMaxLifetime" json:"connMaxLifetime" mapstructure:"connMaxLifetime"`
Dsn string `yaml:"dsn" json:"dsn" mapstructure:"dsn"`
SlowThreshold string `yaml:"slowThreshold" json:"slowThreshold" mapstructure:"slowThreshold"`
EnableLog bool `yaml:"enableLog" json:"enableLog" mapstructure:"enableLog"`
MastersDsn []string `yaml:"mastersDsn" json:"mastersDsn" mapstructure:"mastersDsn"`
MaxIdleConns int `yaml:"maxIdleConns" json:"maxIdleConns" mapstructure:"maxIdleConns"`
......@@ -342,6 +343,7 @@ type Mysql struct {
type Postgresql struct {
ConnMaxLifetime int `yaml:"connMaxLifetime" json:"connMaxLifetime" mapstructure:"connMaxLifetime"`
Dsn string `yaml:"dsn" json:"dsn" mapstructure:"dsn"`
SlowThreshold string `yaml:"slowThreshold" json:"slowThreshold" mapstructure:"slowThreshold"`
EnableLog bool `yaml:"enableLog" json:"enableLog" mapstructure:"enableLog"`
MaxIdleConns int `yaml:"maxIdleConns" json:"maxIdleConns" mapstructure:"maxIdleConns"`
MaxOpenConns int `yaml:"maxOpenConns" json:"maxOpenConns" mapstructure:"maxOpenConns"`
......
......@@ -17,11 +17,22 @@ func InitMysql(dbConfig *config.Database) (*sgorm.DB, error) {
config.Read(func(c *config.Config) {
enableTrace = c.App.Middleware.Trace.Enable
})
slowThreshold := 100 * time.Millisecond
if dbConfig.Mysql.SlowThreshold != "" {
st, e := time.ParseDuration(dbConfig.Mysql.SlowThreshold)
if e == nil && st > 0 {
slowThreshold = st
}
}
opts := []mysql.Option{
mysql.WithSlowThreshold(slowThreshold),
mysql.WithMaxIdleConns(dbConfig.Mysql.MaxIdleConns),
mysql.WithMaxOpenConns(dbConfig.Mysql.MaxOpenConns),
mysql.WithConnMaxLifetime(time.Duration(dbConfig.Mysql.ConnMaxLifetime) * time.Minute),
}
if dbConfig.Mysql.EnableLog {
opts = append(opts,
mysql.WithLogging(logger.Get()),
......
......@@ -17,11 +17,22 @@ func InitPostgreSQL(dbConfig *config.Database) (*sgorm.DB, error) {
config.Read(func(c *config.Config) {
enableTrace = c.App.Middleware.Trace.Enable
})
slowThreshold := 100 * time.Millisecond
if dbConfig.Mysql.SlowThreshold != "" {
st, e := time.ParseDuration(dbConfig.Postgresql.SlowThreshold)
if e == nil && st > 0 {
slowThreshold = st
}
}
opts := []postgresql.Option{
postgresql.WithSlowThreshold(slowThreshold),
postgresql.WithMaxIdleConns(dbConfig.Postgresql.MaxIdleConns),
postgresql.WithMaxOpenConns(dbConfig.Postgresql.MaxOpenConns),
postgresql.WithConnMaxLifetime(time.Duration(dbConfig.Postgresql.ConnMaxLifetime) * time.Minute),
}
if dbConfig.Postgresql.EnableLog {
opts = append(opts,
postgresql.WithLogging(logger.Get()),
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论