From 53954e520f1f97503f7e48b6780e0dc7110c69a9 Mon Sep 17 00:00:00 2001 From: qloog Date: Thu, 13 Jun 2024 18:14:26 +0800 Subject: [PATCH] chore: add timeout for db --- config/docker/database.yaml | 5 ++++- config/local/database.yaml | 6 ++++++ pkg/storage/orm/orm.go | 15 ++++++++++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/config/docker/database.yaml b/config/docker/database.yaml index f36f6b406..67321c9cf 100644 --- a/config/docker/database.yaml +++ b/config/docker/database.yaml @@ -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 \ No newline at end of file + SlowThreshold: 500ms # 慢查询阈值,设置后只打印慢查询日志,默认为200ms diff --git a/config/local/database.yaml b/config/local/database.yaml index d814ffc36..3236984f3 100644 --- a/config/local/database.yaml +++ b/config/local/database.yaml @@ -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: @@ -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 diff --git a/pkg/storage/orm/orm.go b/pkg/storage/orm/orm.go index 46e1bbbdb..619731750 100644 --- a/pkg/storage/orm/orm.go +++ b/pkg/storage/orm/orm.go @@ -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 } @@ -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, ) }