diff --git a/bridge-history-api/conf/config.json b/bridge-history-api/conf/config.json index 873bbbad5a..496b7af526 100644 --- a/bridge-history-api/conf/config.json +++ b/bridge-history-api/conf/config.json @@ -43,7 +43,9 @@ "dsn": "postgres://postgres:123456@localhost:5444/test?sslmode=disable", "driverName": "postgres", "maxOpenNum": 200, - "maxIdleNum": 20 + "maxIdleNum": 20, + "maxLifetime": 600, + "maxIdleTime": 300 }, "redis": { "address": "localhost:6379", diff --git a/common/database/config.go b/common/database/config.go index 0a99a6b153..8e090ea018 100644 --- a/common/database/config.go +++ b/common/database/config.go @@ -8,4 +8,6 @@ type Config struct { MaxOpenNum int `json:"maxOpenNum"` MaxIdleNum int `json:"maxIdleNum"` + MaxLifetime int `json:"maxLifetime,omitempty"` + MaxIdleTime int `json:"maxIdleTime,omitempty"` } diff --git a/common/database/db.go b/common/database/db.go index 322ba27115..e7f13510bd 100644 --- a/common/database/db.go +++ b/common/database/db.go @@ -15,6 +15,11 @@ import ( cutils "scroll-tech/common/utils" ) +const ( + defaultConnMaxLifetime = time.Minute * 10 + defaultConnMaxIdleTime = time.Minute * 5 +) + type gormLogger struct { gethLogger log.Logger } @@ -70,8 +75,17 @@ func InitDB(config *Config) (*gorm.DB, error) { return nil, pingErr } - sqlDB.SetConnMaxLifetime(time.Minute * 10) - sqlDB.SetConnMaxIdleTime(time.Minute * 5) + if (config.MaxLifetime != 0) { + sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.MaxLifetime)) + } else { + sqlDB.SetConnMaxLifetime(defaultConnMaxLifetime) + } + + if (config.MaxIdleTime != 0) { + sqlDB.SetConnMaxIdleTime(time.Second * time.Duration(config.MaxIdleTime)) + } else { + sqlDB.SetConnMaxIdleTime(defaultConnMaxIdleTime) + } sqlDB.SetMaxOpenConns(config.MaxOpenNum) sqlDB.SetMaxIdleConns(config.MaxIdleNum) diff --git a/coordinator/conf/config.json b/coordinator/conf/config.json index b5a09e25ad..5bc7fa858b 100644 --- a/coordinator/conf/config.json +++ b/coordinator/conf/config.json @@ -17,7 +17,9 @@ "driver_name": "postgres", "dsn": "postgres://localhost/scroll?sslmode=disable", "maxOpenNum": 200, - "maxIdleNum": 20 + "maxIdleNum": 20, + "maxLifetime": 600, + "maxIdleTime": 300 }, "l2": { "chain_id": 111 diff --git a/coordinator/internal/config/config_test.go b/coordinator/internal/config/config_test.go index 748fe9c2a9..2e43e3da75 100644 --- a/coordinator/internal/config/config_test.go +++ b/coordinator/internal/config/config_test.go @@ -29,7 +29,9 @@ func TestConfig(t *testing.T) { "driver_name": "postgres", "dsn": "postgres://admin:123456@localhost/test?sslmode=disable", "maxOpenNum": 200, - "maxIdleNum": 20 + "maxIdleNum": 20, + "maxLifetime": 600, + "maxIdleTime": 300 }, "l2": { "chain_id": 111 diff --git a/database/config.go b/database/config.go index 4c037946c1..f768721151 100644 --- a/database/config.go +++ b/database/config.go @@ -12,8 +12,10 @@ type DBConfig struct { DSN string `json:"dsn"` DriverName string `json:"driver_name"` - MaxOpenNum int `json:"maxOpenNum"` - MaxIdleNum int `json:"maxIdleNum"` + MaxOpenNum int `json:"maxOpenNum"` + MaxIdleNum int `json:"maxIdleNum"` + MaxLifetime int `json:"maxLifetime,omitempty"` + MaxIdleTime int `json:"maxIdleTime,omitempty"` } // NewConfig returns a new instance of Config. diff --git a/database/config.json b/database/config.json index eda41de4c4..7a4a24a620 100644 --- a/database/config.json +++ b/database/config.json @@ -2,5 +2,7 @@ "dsn": "postgres://postgres:123456@localhost:5444/test?sslmode=disable", "driver_name": "postgres", "maxOpenNum": 200, - "maxIdleNum": 20 + "maxIdleNum": 20, + "maxLifetime": 600, + "maxIdleTime": 300 } \ No newline at end of file diff --git a/database/config_test.go b/database/config_test.go index 4c8b723c90..f12a6e628c 100644 --- a/database/config_test.go +++ b/database/config_test.go @@ -15,7 +15,9 @@ func TestConfig(t *testing.T) { "dsn": "postgres://postgres:123456@localhost:5444/test?sslmode=disable", "driver_name": "postgres", "maxOpenNum": %d, - "maxIdleNum": %d + "maxIdleNum": %d, + "maxLifetime": %d, + "maxIdleTime": %d }` t.Run("Success Case", func(t *testing.T) { diff --git a/rollup/conf/config.json b/rollup/conf/config.json index 904fcb21d1..8dfb20dac6 100644 --- a/rollup/conf/config.json +++ b/rollup/conf/config.json @@ -82,6 +82,8 @@ "driver_name": "postgres", "dsn": "postgres://localhost/scroll?sslmode=disable", "maxOpenNum": 200, - "maxIdleNum": 20 + "maxIdleNum": 20, + "maxLifetime": 600, + "maxIdleTime": 300 } }