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 all 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 @@ -43,7 +43,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"`
}
18 changes: 16 additions & 2 deletions common/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -70,8 +75,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(defaultConnMaxLifetime)
}

if (config.MaxIdleTime != 0) {
sqlDB.SetConnMaxIdleTime(time.Second * time.Duration(config.MaxIdleTime))
} else {
sqlDB.SetConnMaxIdleTime(defaultConnMaxIdleTime)
}

sqlDB.SetMaxOpenConns(config.MaxOpenNum)
sqlDB.SetMaxIdleConns(config.MaxIdleNum)
Expand Down
4 changes: 3 additions & 1 deletion coordinator/conf/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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
6 changes: 4 additions & 2 deletions database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ 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"`
}

// 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 @@ -82,6 +82,8 @@
"driver_name": "postgres",
"dsn": "postgres://localhost/scroll?sslmode=disable",
"maxOpenNum": 200,
"maxIdleNum": 20
"maxIdleNum": 20,
"maxLifetime": 600,
"maxIdleTime": 300
}
}
Loading