Skip to content

Commit

Permalink
chore: add timeout for db
Browse files Browse the repository at this point in the history
  • Loading branch information
qloog committed Jun 13, 2024
1 parent a5f51e0 commit 53954e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
5 changes: 4 additions & 1 deletion config/docker/database.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ default:
ShowLog: true # 是否打印所有SQL日志
MaxIdleConn: 10 # 最大闲置的连接数,0意味着使用默认的大小2, 小于0表示不使用连接池
MaxOpenConn: 60 # 最大打开的连接数, 需要小于数据库配置中的max_connections数
Timeout: 3s # 数据库连接超时时间
ReadTimeout: 3s # 数据库去读超时时间, 0代表不限制
WriteTimeout: 3s # 数据库写入超时时间, 0代表不限制
ConnMaxLifeTime: 4h # 单个连接最大存活时间,建议设置比数据库超时时长(wait_timeout)稍小一些
SlowThreshold: 500ms # 慢查询阈值,设置后只打印慢查询日志,默认为200ms
SlowThreshold: 500ms # 慢查询阈值,设置后只打印慢查询日志,默认为200ms
6 changes: 6 additions & 0 deletions config/local/database.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ default:
ShowLog: true # 是否打印所有SQL日志
MaxIdleConn: 10 # 最大闲置的连接数,0意味着使用默认的大小2, 小于0表示不使用连接池
MaxOpenConn: 60 # 最大打开的连接数, 需要小于数据库配置中的max_connections数
Timeout: 3s # 数据库连接超时时间, 如果是 PostgreSQL 不需要加入单位
ReadTimeout: 3s # 数据库去读超时时间, 0代表不限制,如果是PostgreSQL, 3000代表3s
WriteTimeout: 3s # 数据库写入超时时间, 0代表不限制,如果是PostgreSQL, 不会使用该字段的值
ConnMaxLifeTime: 4h # 单个连接最大存活时间,建议设置比数据库超时时长(wait_timeout)稍小一些
SlowThreshold: 500ms # 慢查询阈值,设置后只打印慢查询日志,默认为200ms
user:
Expand All @@ -18,5 +21,8 @@ user:
ShowLog: true # 是否打印所有SQL日志
MaxIdleConn: 10 # 最大闲置的连接数,0意味着使用默认的大小2, 小于0表示不使用连接池
MaxOpenConn: 60 # 最大打开的连接数, 需要小于数据库配置中的max_connections数
Timeout: 3s # 数据库连接超时时间
ReadTimeout: 3s # 数据库去读超时时间, 0代表不限制
WriteTimeout: 3s # 数据库写入超时时间, 0代表不限制
ConnMaxLifeTime: 4h # 单个连接最大存活时间,建议设置比数据库超时时长(wait_timeout)稍小一些
SlowThreshold: 500ms # 慢查询阈值,设置后只打印慢查询日志,默认为200ms
15 changes: 12 additions & 3 deletions pkg/storage/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type Config struct {
ShowLog bool
MaxIdleConn int
MaxOpenConn int
Timeout string // connect timeout
ReadTimeout string
WriteTimeout string
ConnMaxLifeTime time.Duration
SlowThreshold time.Duration // 慢查询时长,默认500ms
}
Expand Down Expand Up @@ -189,21 +192,27 @@ func LoadConf(name string) (ret *Config, err error) {
// getDSN return dsn string
func getDSN(c *Config) string {
// default mysql
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=%t&loc=%s",
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=%t&loc=%s&timeout=%s%readTimeout=%s%writeTimeout=%s",
c.UserName,
c.Password,
c.Addr,
c.Name,
true,
//"Asia/Shanghai"),
"Local")
"Local",
c.Timeout,
c.ReadTimeout,
c.WriteTimeout,
)

if c.Driver == DriverPostgres {
dsn = fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable",
dsn = fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable&connect_timeout=%s%statement_timeout=%s",
c.UserName,
c.Password,
c.Addr,
c.Name,
c.Timeout,
c.ReadTimeout,
)
}

Expand Down

0 comments on commit 53954e5

Please sign in to comment.