Skip to content

Commit f7694fa

Browse files
author
Daniel Bedrood
committed
feat: Add new database connection settings
1 parent aa6740e commit f7694fa

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

pkg/database/config.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"strings"
7+
"time"
78

89
cbuilder "github.com/scribd/go-sdk/internal/pkg/configuration/builder"
910
)
@@ -15,8 +16,13 @@ type Config struct {
1516
Username string `mapstructure:"username"`
1617
Password string `mapstructure:"password"`
1718
Database string `mapstructure:"database"`
18-
Pool int `mapstructure:"pool"`
1919
Timeout string `mapstructure:"timeout"`
20+
// Connection settings
21+
// TODO Pool field name must be modified in the next major change.
22+
Pool int `mapstructure:"pool"`
23+
MaxOpenConnections int `mapstructure:"max_open_connections"`
24+
ConnectionMaxIdleTime time.Duration `mapstructure:"connection_max_idle_time"`
25+
ConnectionMaxLifetime time.Duration `mapstructure:"connection_max_lifetime"`
2026
}
2127

2228
// NewConfig returns a new Config instance.

pkg/database/config_test.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package database
33
import (
44
"os"
55
"testing"
6+
"time"
67

78
assert "github.com/stretchr/testify/assert"
89
)
@@ -15,26 +16,32 @@ func TestNewConfig(t *testing.T) {
1516
})
1617

1718
testCases := []struct {
18-
name string
19-
wantError bool
20-
host string
21-
port int
22-
username string
23-
password string
24-
database string
25-
pool int
26-
timeout string
19+
name string
20+
wantError bool
21+
host string
22+
port int
23+
username string
24+
password string
25+
database string
26+
timeout string
27+
pool int
28+
maxOpenConnections int
29+
connectionMaxIdleTime time.Duration
30+
connectionMaxLifetime time.Duration
2731
}{
2832
{
29-
name: "NewWithoutConfigFileFails",
30-
wantError: true,
31-
host: "",
32-
port: 0,
33-
username: "",
34-
password: "",
35-
database: "",
36-
pool: 0,
37-
timeout: "",
33+
name: "NewWithoutConfigFileFails",
34+
wantError: true,
35+
host: "",
36+
port: 0,
37+
username: "",
38+
password: "",
39+
database: "",
40+
timeout: "",
41+
pool: 0,
42+
maxOpenConnections: 0,
43+
connectionMaxIdleTime: 0,
44+
connectionMaxLifetime: 0,
3845
},
3946
}
4047

@@ -50,8 +57,11 @@ func TestNewConfig(t *testing.T) {
5057
assert.Equal(t, c.Username, tc.username)
5158
assert.Equal(t, c.Password, tc.password)
5259
assert.Equal(t, c.Database, tc.database)
53-
assert.Equal(t, c.Pool, tc.pool)
5460
assert.Equal(t, c.Timeout, tc.timeout)
61+
assert.Equal(t, c.Pool, tc.pool)
62+
assert.Equal(t, c.MaxOpenConnections, tc.maxOpenConnections)
63+
assert.Equal(t, c.ConnectionMaxIdleTime, tc.connectionMaxIdleTime)
64+
assert.Equal(t, c.ConnectionMaxLifetime, tc.connectionMaxLifetime)
5565
})
5666
}
5767
}

pkg/database/gorm.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func NewConnection(config *Config, environment string) (*gorm.DB, error) {
4545
}
4646

4747
sqlDB.SetMaxIdleConns(config.Pool)
48+
sqlDB.SetMaxOpenConns(config.MaxOpenConnections)
49+
sqlDB.SetConnMaxIdleTime(config.ConnectionMaxIdleTime)
50+
sqlDB.SetConnMaxLifetime(config.ConnectionMaxLifetime)
4851

4952
return db, nil
5053
}

0 commit comments

Comments
 (0)