Skip to content

Commit f6948ba

Browse files
authored
Add additional DB connection properties (caraml-dev#86)
* Add additional DB connection properties * Update unit test cases * Run gofmt on code
1 parent 779530a commit f6948ba

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

api/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cover.out

api/config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"strings"
7+
"time"
78

89
"github.com/caraml-dev/mlp/api/models/v2"
910
"github.com/go-playground/validator/v10"
@@ -55,6 +56,11 @@ type DatabaseConfig struct {
5556
Password string `validate:"required"`
5657
Database string `validate:"required"`
5758
MigrationPath string `validate:"required,url"`
59+
60+
ConnMaxIdleTime time.Duration
61+
ConnMaxLifetime time.Duration
62+
MaxIdleConns int
63+
MaxOpenConns int
5864
}
5965

6066
type AuthorizationConfig struct {

api/config/config_test.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"os"
66
"testing"
7+
"time"
78

89
"github.com/caraml-dev/mlp/api/config"
910
"github.com/caraml-dev/mlp/api/models/v2"
@@ -34,6 +35,9 @@ func envSetter(envs map[string]string) (closer func()) {
3435
}
3536

3637
func TestLoad(t *testing.T) {
38+
oneSecond, _ := time.ParseDuration("1s")
39+
twoSeconds, _ := time.ParseDuration("2s")
40+
3741
suite := map[string]struct {
3842
configs []string
3943
env map[string]string
@@ -56,11 +60,15 @@ func TestLoad(t *testing.T) {
5660
Enabled: false,
5761
},
5862
Database: &config.DatabaseConfig{
59-
Host: "localhost",
60-
Port: 5432,
61-
Database: "mlp",
62-
User: "mlp",
63-
MigrationPath: "file://db-migrations",
63+
Host: "localhost",
64+
Port: 5432,
65+
Database: "mlp",
66+
User: "mlp",
67+
MigrationPath: "file://db-migrations",
68+
ConnMaxIdleTime: oneSecond,
69+
ConnMaxLifetime: twoSeconds,
70+
MaxIdleConns: 10,
71+
MaxOpenConns: 20,
6472
},
6573
Mlflow: &config.MlflowConfig{},
6674
Docs: []config.Documentation{},
@@ -90,12 +98,16 @@ func TestLoad(t *testing.T) {
9098
Enabled: false,
9199
},
92100
Database: &config.DatabaseConfig{
93-
Host: "localhost",
94-
Port: 5432,
95-
Database: "mlp",
96-
User: "mlp",
97-
Password: "secret",
98-
MigrationPath: "file://db-migrations",
101+
Host: "localhost",
102+
Port: 5432,
103+
Database: "mlp",
104+
User: "mlp",
105+
Password: "secret",
106+
MigrationPath: "file://db-migrations",
107+
ConnMaxIdleTime: oneSecond,
108+
ConnMaxLifetime: twoSeconds,
109+
MaxIdleConns: 10,
110+
MaxOpenConns: 20,
99111
},
100112
Mlflow: &config.MlflowConfig{},
101113
Docs: []config.Documentation{},
@@ -152,12 +164,16 @@ func TestLoad(t *testing.T) {
152164
KetoServerURL: "http://localhost:4466",
153165
},
154166
Database: &config.DatabaseConfig{
155-
Host: "localhost",
156-
Port: 5432,
157-
Database: "mlp",
158-
User: "mlp",
159-
Password: "secret",
160-
MigrationPath: "file://db-migrations",
167+
Host: "localhost",
168+
Port: 5432,
169+
Database: "mlp",
170+
User: "mlp",
171+
Password: "secret",
172+
MigrationPath: "file://db-migrations",
173+
ConnMaxIdleTime: oneSecond,
174+
ConnMaxLifetime: twoSeconds,
175+
MaxIdleConns: 10,
176+
MaxOpenConns: 20,
161177
},
162178
Docs: []config.Documentation{
163179
{

api/config/testdata/config-1.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
database:
22
user: mlp
3+
connMaxIdleTime: 1s
4+
connMaxLifetime: 2s
5+
maxIdleConns: 10
6+
maxOpenConns: 20
37

48
streams:
59
stream-1:

api/database/database.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ func InitDB(dbCfg *config.DatabaseConfig) (*gorm.DB, error) {
3131
if err != nil {
3232
return nil, err
3333
}
34+
35+
// Get the underlying SQL DB and apply connection properties
36+
sqlDB := db.DB()
37+
if sqlDB == nil {
38+
return nil, errors.New("Failed to get underlying database connection")
39+
}
40+
sqlDB.SetConnMaxIdleTime(dbCfg.ConnMaxIdleTime)
41+
sqlDB.SetConnMaxLifetime(dbCfg.ConnMaxLifetime)
42+
sqlDB.SetMaxIdleConns(dbCfg.MaxIdleConns)
43+
sqlDB.SetMaxOpenConns(dbCfg.MaxOpenConns)
44+
3445
db.LogMode(false)
3546
err = runDBMigration(db, dbCfg.MigrationPath)
3647
if err != nil {

0 commit comments

Comments
 (0)