Skip to content

Commit

Permalink
#1042 Allow configure db ConnMaxLifetime & ConnMaxIdleTime
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswinsimon committed Mar 19, 2024
1 parent 02415a6 commit 8a21fe4
Show file tree
Hide file tree
Showing 19 changed files with 80 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"`
MaxIdleTime int `json:"maxIdleTime"`
}
4 changes: 2 additions & 2 deletions common/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func InitDB(config *Config) (*gorm.DB, error) {
return nil, pingErr
}

sqlDB.SetConnMaxLifetime(time.Minute * 10)
sqlDB.SetConnMaxIdleTime(time.Minute * 5)
sqlDB.SetConnMaxLifetime(config.maxLifetime)

Check failure on line 73 in common/database/db.go

View workflow job for this annotation

GitHub Actions / tests

config.maxLifetime undefined (type *Config has no field or method maxLifetime, but does have MaxLifetime)
sqlDB.SetConnMaxIdleTime(config.maxIdleTime)

Check failure on line 74 in common/database/db.go

View workflow job for this annotation

GitHub Actions / tests

config.maxIdleTime undefined (type *Config has no field or method maxIdleTime, but does have MaxIdleTime)

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

Check failure on line 171 in common/docker/docker_app.go

View workflow job for this annotation

GitHub Actions / tests

syntax error: unexpected newline in composite literal; possibly missing comma or }
}

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 @@ -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
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"`
MaxIdleTime int `json:"maxIdleTime"`
}

// 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 8a21fe4

Please sign in to comment.