diff --git a/bridge-history-api/conf/config.json b/bridge-history-api/conf/config.json index bcbf6384f2..49c069bbaf 100644 --- a/bridge-history-api/conf/config.json +++ b/bridge-history-api/conf/config.json @@ -40,7 +40,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..2a3d0773b3 100644 --- a/common/database/db.go +++ b/common/database/db.go @@ -70,8 +70,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 * config.MaxLifetime) + } else { + sqlDB.SetConnMaxLifetime(time.Minute * 10) + } + + if (config.MaxIdleTime != 0) { + sqlDB.SetConnMaxIdleTime(time.Second * config.MaxIdleTime) + } else { + sqlDB.SetConnMaxIdleTime(time.Minute * 5) + } sqlDB.SetMaxOpenConns(config.MaxOpenNum) sqlDB.SetMaxIdleConns(config.MaxIdleNum) diff --git a/common/database/db_test.go b/common/database/db_test.go index df6bcd6496..8b097b5873 100644 --- a/common/database/db_test.go +++ b/common/database/db_test.go @@ -44,10 +44,12 @@ func TestDB(t *testing.T) { base.RunDBImage(t) dbCfg := &Config{ - DSN: base.DBConfig.DSN, - DriverName: base.DBConfig.DriverName, - MaxOpenNum: base.DBConfig.MaxOpenNum, - MaxIdleNum: base.DBConfig.MaxIdleNum, + DSN: base.DBConfig.DSN, + DriverName: base.DBConfig.DriverName, + MaxOpenNum: base.DBConfig.MaxOpenNum, + MaxIdleNum: base.DBConfig.MaxIdleNum, + MaxLifetime: base.DBConfig.MaxLifetime, + MaxIdleTime: base.DBConfig.MaxIdleTime, } var err error diff --git a/common/docker/docker_app.go b/common/docker/docker_app.go index 568921001b..3974543279 100644 --- a/common/docker/docker_app.go +++ b/common/docker/docker_app.go @@ -167,6 +167,8 @@ func (b *App) mockDBConfig() error { DriverName: "postgres", MaxOpenNum: 200, MaxIdleNum: 20, + MaxLifetime: 600, + MaxIdleTime: 300, } if b.DBImg != nil { diff --git a/coordinator/conf/config.json b/coordinator/conf/config.json index b143a702f7..a1ffae9069 100644 --- a/coordinator/conf/config.json +++ b/coordinator/conf/config.json @@ -16,7 +16,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/coordinator/internal/orm/orm_test.go b/coordinator/internal/orm/orm_test.go index ed6f719b78..737975e2c3 100644 --- a/coordinator/internal/orm/orm_test.go +++ b/coordinator/internal/orm/orm_test.go @@ -38,10 +38,12 @@ func setupEnv(t *testing.T) { var err error db, err = database.InitDB( &database.Config{ - DSN: base.DBConfig.DSN, - DriverName: base.DBConfig.DriverName, - MaxOpenNum: base.DBConfig.MaxOpenNum, - MaxIdleNum: base.DBConfig.MaxIdleNum, + DSN: base.DBConfig.DSN, + DriverName: base.DBConfig.DriverName, + MaxOpenNum: base.DBConfig.MaxOpenNum, + MaxIdleNum: base.DBConfig.MaxIdleNum, + MaxLifetime: base.DBConfig.MaxLifetime, + MaxIdleTime: base.DBConfig.MaxIdleTime, }, ) assert.NoError(t, err) diff --git a/coordinator/test/api_test.go b/coordinator/test/api_test.go index 179390300e..c79897944a 100644 --- a/coordinator/test/api_test.go +++ b/coordinator/test/api_test.go @@ -125,10 +125,12 @@ func setEnv(t *testing.T) { base.RunDBImage(t) dbCfg = &database.Config{ - DSN: base.DBConfig.DSN, - DriverName: base.DBConfig.DriverName, - MaxOpenNum: base.DBConfig.MaxOpenNum, - MaxIdleNum: base.DBConfig.MaxIdleNum, + DSN: base.DBConfig.DSN, + DriverName: base.DBConfig.DriverName, + MaxOpenNum: base.DBConfig.MaxOpenNum, + MaxIdleNum: base.DBConfig.MaxIdleNum, + MaxLifetime: base.DBConfig.MaxLifetime, + MaxIdleTime: base.DBConfig.MaxIdleTime, } var err error diff --git a/database/config.go b/database/config.go index 4c037946c1..06920c743c 100644 --- a/database/config.go +++ b/database/config.go @@ -14,6 +14,8 @@ type DBConfig struct { 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 bfecdd003d..4589a6fd28 100644 --- a/rollup/conf/config.json +++ b/rollup/conf/config.json @@ -81,6 +81,8 @@ "driver_name": "postgres", "dsn": "postgres://localhost/scroll?sslmode=disable", "maxOpenNum": 200, - "maxIdleNum": 20 + "maxIdleNum": 20, + "maxLifetime": 600, + "maxIdleTime": 300 } } diff --git a/rollup/internal/controller/relayer/relayer_test.go b/rollup/internal/controller/relayer/relayer_test.go index 60f2137e59..bbe6b09b4c 100644 --- a/rollup/internal/controller/relayer/relayer_test.go +++ b/rollup/internal/controller/relayer/relayer_test.go @@ -56,10 +56,12 @@ func setupEnv(t *testing.T) { cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = base.L1gethImg.Endpoint() cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2gethImg.Endpoint() cfg.DBConfig = &database.Config{ - DSN: base.DBConfig.DSN, - DriverName: base.DBConfig.DriverName, - MaxOpenNum: base.DBConfig.MaxOpenNum, - MaxIdleNum: base.DBConfig.MaxIdleNum, + DSN: base.DBConfig.DSN, + DriverName: base.DBConfig.DriverName, + MaxOpenNum: base.DBConfig.MaxOpenNum, + MaxIdleNum: base.DBConfig.MaxIdleNum, + MaxLifetime: base.DBConfig.MaxLifetime, + MaxIdleTime: base.DBConfig.MaxIdleTime, } port, err := rand.Int(rand.Reader, big.NewInt(10000)) assert.NoError(t, err) diff --git a/rollup/internal/controller/sender/sender_test.go b/rollup/internal/controller/sender/sender_test.go index 7513c32170..4ae6fee37e 100644 --- a/rollup/internal/controller/sender/sender_test.go +++ b/rollup/internal/controller/sender/sender_test.go @@ -85,10 +85,12 @@ func setupEnv(t *testing.T) { base.RunDBImage(t) db, err = database.InitDB( &database.Config{ - DSN: base.DBConfig.DSN, - DriverName: base.DBConfig.DriverName, - MaxOpenNum: base.DBConfig.MaxOpenNum, - MaxIdleNum: base.DBConfig.MaxIdleNum, + DSN: base.DBConfig.DSN, + DriverName: base.DBConfig.DriverName, + MaxOpenNum: base.DBConfig.MaxOpenNum, + MaxIdleNum: base.DBConfig.MaxIdleNum, + MaxLifetime: base.DBConfig.MaxLifetime, + MaxIdleTime: base.DBConfig.MaxIdleTime, }, ) assert.NoError(t, err) diff --git a/rollup/internal/controller/watcher/watcher_test.go b/rollup/internal/controller/watcher/watcher_test.go index 0e73bad703..a2105f6066 100644 --- a/rollup/internal/controller/watcher/watcher_test.go +++ b/rollup/internal/controller/watcher/watcher_test.go @@ -47,10 +47,12 @@ func setupEnv(t *testing.T) (err error) { cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = base.L1gethImg.Endpoint() cfg.L1Config.RelayerConfig.SenderConfig.Endpoint = base.L2gethImg.Endpoint() cfg.DBConfig = &database.Config{ - DSN: base.DBConfig.DSN, - DriverName: base.DBConfig.DriverName, - MaxOpenNum: base.DBConfig.MaxOpenNum, - MaxIdleNum: base.DBConfig.MaxIdleNum, + DSN: base.DBConfig.DSN, + DriverName: base.DBConfig.DriverName, + MaxOpenNum: base.DBConfig.MaxOpenNum, + MaxIdleNum: base.DBConfig.MaxIdleNum, + MaxLifetime: base.DBConfig.MaxLifetime, + MaxIdleTime: base.DBConfig.MaxIdleTime, } // Create l2geth client. diff --git a/rollup/internal/orm/orm_test.go b/rollup/internal/orm/orm_test.go index dc458a0b35..bf82137af1 100644 --- a/rollup/internal/orm/orm_test.go +++ b/rollup/internal/orm/orm_test.go @@ -50,10 +50,12 @@ func setupEnv(t *testing.T) { var err error db, err = database.InitDB( &database.Config{ - DSN: base.DBConfig.DSN, - DriverName: base.DBConfig.DriverName, - MaxOpenNum: base.DBConfig.MaxOpenNum, - MaxIdleNum: base.DBConfig.MaxIdleNum, + DSN: base.DBConfig.DSN, + DriverName: base.DBConfig.DriverName, + MaxOpenNum: base.DBConfig.MaxOpenNum, + MaxIdleNum: base.DBConfig.MaxIdleNum, + MaxLifetime: base.DBConfig.MaxLifetime, + MaxIdleTime: base.DBConfig.MaxIdleTime, }, ) assert.NoError(t, err) diff --git a/rollup/tests/bridge_test.go b/rollup/tests/bridge_test.go index a7e1be9dba..4e1dc7683d 100644 --- a/rollup/tests/bridge_test.go +++ b/rollup/tests/bridge_test.go @@ -48,10 +48,12 @@ var ( func setupDB(t *testing.T) *gorm.DB { cfg := &database.Config{ - DSN: base.DBConfig.DSN, - DriverName: base.DBConfig.DriverName, - MaxOpenNum: base.DBConfig.MaxOpenNum, - MaxIdleNum: base.DBConfig.MaxIdleNum, + DSN: base.DBConfig.DSN, + DriverName: base.DBConfig.DriverName, + MaxOpenNum: base.DBConfig.MaxOpenNum, + MaxIdleNum: base.DBConfig.MaxIdleNum, + MaxLifetime: base.DBConfig.MaxLifetime, + MaxIdleTime: base.DBConfig.MaxIdleTime, } db, err := database.InitDB(cfg) assert.NoError(t, err) diff --git a/tests/integration-test/integration_test.go b/tests/integration-test/integration_test.go index 16455fc728..7bcdd8f6f8 100644 --- a/tests/integration-test/integration_test.go +++ b/tests/integration-test/integration_test.go @@ -48,10 +48,12 @@ func TestCoordinatorProverInteraction(t *testing.T) { // Init data dbCfg := &database.Config{ - DSN: base.DBConfig.DSN, - DriverName: base.DBConfig.DriverName, - MaxOpenNum: base.DBConfig.MaxOpenNum, - MaxIdleNum: base.DBConfig.MaxIdleNum, + DSN: base.DBConfig.DSN, + DriverName: base.DBConfig.DriverName, + MaxOpenNum: base.DBConfig.MaxOpenNum, + MaxIdleNum: base.DBConfig.MaxIdleNum, + MaxLifetime: base.DBConfig.MaxLifetime, + MaxIdleTime: base.DBConfig.MaxIdleTime, } db, err := database.InitDB(dbCfg)