From 816c403c6d9998a8a6b81e1471b2d1837d34b576 Mon Sep 17 00:00:00 2001 From: Jeswin Simon Date: Sun, 24 Mar 2024 17:04:43 +0530 Subject: [PATCH 1/7] #1042 Allow configure db ConnMaxLifetime & ConnMaxIdleTime --- bridge-history-api/conf/config.json | 4 +++- common/database/config.go | 2 ++ common/database/db.go | 13 +++++++++++-- common/database/db_test.go | 10 ++++++---- common/docker/docker_app.go | 2 ++ coordinator/conf/config.json | 4 +++- coordinator/internal/config/config_test.go | 4 +++- coordinator/internal/orm/orm_test.go | 10 ++++++---- coordinator/test/api_test.go | 10 ++++++---- database/config.go | 2 ++ database/config.json | 4 +++- database/config_test.go | 4 +++- rollup/conf/config.json | 4 +++- rollup/internal/controller/relayer/relayer_test.go | 10 ++++++---- rollup/internal/controller/sender/sender_test.go | 10 ++++++---- rollup/internal/controller/watcher/watcher_test.go | 10 ++++++---- rollup/internal/orm/orm_test.go | 10 ++++++---- rollup/tests/bridge_test.go | 10 ++++++---- tests/integration-test/integration_test.go | 10 ++++++---- 19 files changed, 89 insertions(+), 44 deletions(-) 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) From 4238c9a025917b45ca893e852978cf8b966bbb43 Mon Sep 17 00:00:00 2001 From: Jeswin Simon Date: Mon, 25 Mar 2024 14:13:49 +0530 Subject: [PATCH 2/7] Cast int to duration --- common/database/db.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/database/db.go b/common/database/db.go index 2a3d0773b3..38c09e1ffa 100644 --- a/common/database/db.go +++ b/common/database/db.go @@ -71,13 +71,13 @@ func InitDB(config *Config) (*gorm.DB, error) { } if (config.MaxLifetime != 0) { - sqlDB.SetConnMaxLifetime(time.Second * config.MaxLifetime) + sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.MaxLifetime)) } else { sqlDB.SetConnMaxLifetime(time.Minute * 10) } if (config.MaxIdleTime != 0) { - sqlDB.SetConnMaxIdleTime(time.Second * config.MaxIdleTime) + sqlDB.SetConnMaxIdleTime(time.Second * time.Duration(config.MaxIdleTime)) } else { sqlDB.SetConnMaxIdleTime(time.Minute * 5) } From 86ac5f8b210f5821d3fb430cb47d54de8af65b80 Mon Sep 17 00:00:00 2001 From: Jeswin Simon Date: Sun, 24 Mar 2024 17:04:43 +0530 Subject: [PATCH 3/7] #1042 Allow configure db ConnMaxLifetime & ConnMaxIdleTime --- bridge-history-api/conf/config.json | 4 +++- common/database/config.go | 2 ++ common/database/db.go | 13 +++++++++++-- common/database/db_test.go | 10 ++++++---- common/docker/docker_app.go | 2 ++ coordinator/conf/config.json | 4 +++- coordinator/internal/config/config_test.go | 4 +++- coordinator/internal/orm/orm_test.go | 10 ++++++---- coordinator/test/api_test.go | 10 ++++++---- database/config.go | 2 ++ database/config.json | 4 +++- database/config_test.go | 4 +++- rollup/conf/config.json | 4 +++- rollup/internal/controller/relayer/relayer_test.go | 10 ++++++---- rollup/internal/controller/sender/sender_test.go | 10 ++++++---- rollup/internal/controller/watcher/watcher_test.go | 10 ++++++---- rollup/internal/orm/orm_test.go | 10 ++++++---- rollup/tests/bridge_test.go | 10 ++++++---- tests/integration-test/integration_test.go | 10 ++++++---- 19 files changed, 89 insertions(+), 44 deletions(-) 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 cb72d99283..331b0c7923 100644 --- a/coordinator/test/api_test.go +++ b/coordinator/test/api_test.go @@ -155,10 +155,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 baa44ccebe..11ab9e0b9f 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) From 2228b410eca26f58a7c0b56da8da1abbf597cffb Mon Sep 17 00:00:00 2001 From: Jeswin Simon Date: Mon, 25 Mar 2024 14:13:49 +0530 Subject: [PATCH 4/7] Cast int to duration --- common/database/db.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/database/db.go b/common/database/db.go index 2a3d0773b3..38c09e1ffa 100644 --- a/common/database/db.go +++ b/common/database/db.go @@ -71,13 +71,13 @@ func InitDB(config *Config) (*gorm.DB, error) { } if (config.MaxLifetime != 0) { - sqlDB.SetConnMaxLifetime(time.Second * config.MaxLifetime) + sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.MaxLifetime)) } else { sqlDB.SetConnMaxLifetime(time.Minute * 10) } if (config.MaxIdleTime != 0) { - sqlDB.SetConnMaxIdleTime(time.Second * config.MaxIdleTime) + sqlDB.SetConnMaxIdleTime(time.Second * time.Duration(config.MaxIdleTime)) } else { sqlDB.SetConnMaxIdleTime(time.Minute * 5) } From 243bda7f2ef0db85e87cddd83ba0e2763dd19a69 Mon Sep 17 00:00:00 2001 From: Jeswin Simon Date: Mon, 1 Apr 2024 21:22:43 +0530 Subject: [PATCH 5/7] Added consts for defaultConnMaxLifetime and defaultConnMaxIdleTime --- common/database/db.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/common/database/db.go b/common/database/db.go index 38c09e1ffa..a696c7e318 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 } From 9203c0c1aa588383e3b052082ec411cae45c99e6 Mon Sep 17 00:00:00 2001 From: Jeswin Simon Date: Mon, 1 Apr 2024 21:23:17 +0530 Subject: [PATCH 6/7] Added consts for defaultConnMaxLifetime and defaultConnMaxIdleTime --- common/database/db.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/database/db.go b/common/database/db.go index a696c7e318..e7f13510bd 100644 --- a/common/database/db.go +++ b/common/database/db.go @@ -78,13 +78,13 @@ func InitDB(config *Config) (*gorm.DB, error) { if (config.MaxLifetime != 0) { sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.MaxLifetime)) } else { - sqlDB.SetConnMaxLifetime(time.Minute * 10) + sqlDB.SetConnMaxLifetime(defaultConnMaxLifetime) } if (config.MaxIdleTime != 0) { sqlDB.SetConnMaxIdleTime(time.Second * time.Duration(config.MaxIdleTime)) } else { - sqlDB.SetConnMaxIdleTime(time.Minute * 5) + sqlDB.SetConnMaxIdleTime(defaultConnMaxIdleTime) } sqlDB.SetMaxOpenConns(config.MaxOpenNum) From 43aaa65579dc7ff63b90f184a8fe395bbd2edbca Mon Sep 17 00:00:00 2001 From: Jeswin Simon Date: Mon, 15 Apr 2024 17:31:43 +0530 Subject: [PATCH 7/7] Fix CI issues --- database/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/config.go b/database/config.go index 06920c743c..f768721151 100644 --- a/database/config.go +++ b/database/config.go @@ -12,8 +12,8 @@ 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"` }