Skip to content

Commit

Permalink
scroll-tech#1042 Allow configure db ConnMaxLifetime & ConnMaxIdleTime
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswinsimon committed Mar 25, 2024
1 parent b01b581 commit 86ac5f8
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 44 deletions.
4 changes: 3 additions & 1 deletion bridge-history-api/conf/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions common/database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
13 changes: 11 additions & 2 deletions common/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions common/database/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions common/docker/docker_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ func (b *App) mockDBConfig() error {
DriverName: "postgres",
MaxOpenNum: 200,
MaxIdleNum: 20,
MaxLifetime: 600,
MaxIdleTime: 300,
}

if b.DBImg != nil {
Expand Down
4 changes: 3 additions & 1 deletion coordinator/conf/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion coordinator/internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions coordinator/internal/orm/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions coordinator/test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion database/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion database/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion rollup/conf/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
"driver_name": "postgres",
"dsn": "postgres://localhost/scroll?sslmode=disable",
"maxOpenNum": 200,
"maxIdleNum": 20
"maxIdleNum": 20,
"maxLifetime": 600,
"maxIdleTime": 300
}
}
10 changes: 6 additions & 4 deletions rollup/internal/controller/relayer/relayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions rollup/internal/controller/sender/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions rollup/internal/controller/watcher/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 6 additions & 4 deletions rollup/internal/orm/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions rollup/tests/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions tests/integration-test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 86ac5f8

Please sign in to comment.