Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #1042 Allow configure db ConnMaxLifetime & ConnMaxIdleTime #1207

Closed
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
816c403
#1042 Allow configure db ConnMaxLifetime & ConnMaxIdleTime
jeswinsimon Mar 24, 2024
4238c9a
Cast int to duration
jeswinsimon Mar 25, 2024
86ac5f8
#1042 Allow configure db ConnMaxLifetime & ConnMaxIdleTime
jeswinsimon Mar 24, 2024
2228b41
Cast int to duration
jeswinsimon Mar 25, 2024
907afcf
Merge branch 'develop' of https://github.com/scroll-tech/scroll into …
jeswinsimon Mar 25, 2024
4016d5a
Merge branch 'feat/issue-1042_config' of github.com:jeswinsimon/scrol…
jeswinsimon Mar 25, 2024
2cdc9a4
Merge branch 'develop' of https://github.com/scroll-tech/scroll into …
jeswinsimon Mar 26, 2024
51eae06
Merge branch 'develop' of https://github.com/scroll-tech/scroll into …
jeswinsimon Mar 27, 2024
d853a82
Merge branch 'develop' of https://github.com/scroll-tech/scroll into …
jeswinsimon Apr 1, 2024
243bda7
Added consts for defaultConnMaxLifetime and defaultConnMaxIdleTime
jeswinsimon Apr 1, 2024
9203c0c
Added consts for defaultConnMaxLifetime and defaultConnMaxIdleTime
jeswinsimon Apr 1, 2024
6ef367b
Merge branch 'develop' of https://github.com/scroll-tech/scroll into …
jeswinsimon Apr 3, 2024
76e9c29
Merge branch 'develop' of https://github.com/scroll-tech/scroll into …
jeswinsimon Apr 15, 2024
43aaa65
Fix CI issues
jeswinsimon Apr 15, 2024
d4d0239
Merge branch 'develop' into feat/issue-1042_config
jeswinsimon Apr 22, 2024
ef2d199
Merge branch 'develop' into feat/issue-1042_config
jeswinsimon Apr 24, 2024
07bdddd
Merge branch 'develop' into feat/issue-1042_config
0xmountaintop Apr 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
jeswinsimon marked this conversation as resolved.
Show resolved Hide resolved
},
"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 * time.Duration(config.MaxLifetime))
} else {
sqlDB.SetConnMaxLifetime(time.Minute * 10)
jeswinsimon marked this conversation as resolved.
Show resolved Hide resolved
}

if (config.MaxIdleTime != 0) {
sqlDB.SetConnMaxIdleTime(time.Second * time.Duration(config.MaxIdleTime))
} else {
sqlDB.SetConnMaxIdleTime(time.Minute * 5)
jeswinsimon marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@ -80,6 +80,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 @@ -59,10 +59,12 @@ func setupEnv(t *testing.T) {
cfg.L2Config.RelayerConfig.SenderConfig.Endpoint = posL1TestEnv.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 @@ -86,10 +86,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 @@ -47,10 +47,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