Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions database/gdb/gdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ type DB interface {
// SetMaxConnLifeTime sets the maximum amount of time a connection may be reused.
SetMaxConnLifeTime(d time.Duration)

// SetMaxIdleConnTime sets the maximum amount of time a connection may be idle before being closed.
SetMaxIdleConnTime(d time.Duration)

// ===========================================================================
// Utility methods.
// ===========================================================================
Expand Down Expand Up @@ -528,6 +531,7 @@ type dynamicConfig struct {
MaxIdleConnCount int
MaxOpenConnCount int
MaxConnLifeTime time.Duration
MaxIdleConnTime time.Duration
}

// DoCommitInput is the input parameters for function DoCommit.
Expand Down Expand Up @@ -965,6 +969,7 @@ func newDBByConfigNode(node *ConfigNode, group string) (db DB, err error) {
MaxIdleConnCount: node.MaxIdleConnCount,
MaxOpenConnCount: node.MaxOpenConnCount,
MaxConnLifeTime: node.MaxConnLifeTime,
MaxIdleConnTime: node.MaxIdleConnTime,
},
}
if v, ok := driverMap[node.Type]; ok {
Expand Down Expand Up @@ -1144,6 +1149,9 @@ func (c *Core) getSqlDb(master bool, schema ...string) (sqlDb *sql.DB, err error
} else {
sqlDb.SetConnMaxLifetime(defaultMaxConnLifeTime)
}
if c.dynamicConfig.MaxIdleConnTime > 0 {
sqlDb.SetConnMaxIdleTime(c.dynamicConfig.MaxIdleConnTime)
}
return sqlDb
}
// it here uses NODE VALUE not pointer as the cache key, in case of oracle ORA-12516 error.
Expand Down
15 changes: 15 additions & 0 deletions database/gdb/gdb_core_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ type ConfigNode struct {
// Optional field
MaxConnLifeTime time.Duration `json:"maxLifeTime"`

// MaxIdleConnTime specifies the maximum idle time of a connection before being closed
// This is Go 1.15+ feature: sql.DB.SetConnMaxIdleTime
// Optional field
MaxIdleConnTime time.Duration `json:"maxIdleTime"`

// QueryTimeout specifies the maximum execution time for DQL operations
// Optional field
QueryTimeout time.Duration `json:"queryTimeout"`
Expand Down Expand Up @@ -353,6 +358,16 @@ func (c *Core) SetMaxConnLifeTime(d time.Duration) {
c.dynamicConfig.MaxConnLifeTime = d
}

// SetMaxIdleConnTime sets the maximum amount of time a connection may be idle before being closed.
//
// Idle connections may be closed lazily before reuse.
//
// If d <= 0, connections are not closed due to a connection's idle time.
// This is Go 1.15+ feature: sql.DB.SetConnMaxIdleTime.
func (c *Core) SetMaxIdleConnTime(d time.Duration) {
c.dynamicConfig.MaxIdleConnTime = d
}

// GetConfig returns the current used node configuration.
func (c *Core) GetConfig() *ConfigNode {
var configNode = c.getConfigNodeFromCtx(c.db.GetCtx())
Expand Down
38 changes: 38 additions & 0 deletions database/gdb/gdb_z_core_config_external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package gdb_test

import (
"testing"
"time"

"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/test/gtest"
Expand Down Expand Up @@ -1189,3 +1190,40 @@ func Test_IsConfigured(t *testing.T) {
t.Assert(result, true)
})
}

func Test_ConfigNode_ConnectionPoolSettings(t *testing.T) {
// Test connection pool configuration fields
gtest.C(t, func(t *gtest.T) {
// Save original config and restore after test
originalConfig := gdb.GetAllConfig()
defer func() {
gdb.SetConfig(originalConfig)
}()

// Reset config
gdb.SetConfig(make(gdb.Config))

testNode := gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
User: "root",
Pass: "123456",
Name: "test_db",
Type: "mysql",
MaxIdleConnCount: 10,
MaxOpenConnCount: 100,
MaxConnLifeTime: 30 * time.Second,
MaxIdleConnTime: 10 * time.Second,
}

err := gdb.AddConfigNode("pool_test", testNode)
t.AssertNil(err)

result := gdb.GetAllConfig()
t.Assert(len(result), 1)
t.Assert(result["pool_test"][0].MaxIdleConnCount, 10)
t.Assert(result["pool_test"][0].MaxOpenConnCount, 100)
t.Assert(result["pool_test"][0].MaxConnLifeTime, 30*time.Second)
t.Assert(result["pool_test"][0].MaxIdleConnTime, 10*time.Second)
})
}
5 changes: 5 additions & 0 deletions database/gdb/gdb_z_core_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ func Test_Core_SetMaxConnections(t *testing.T) {
testDuration := time.Hour
core.SetMaxConnLifeTime(testDuration)
t.Assert(core.dynamicConfig.MaxConnLifeTime, testDuration)

// Test SetMaxIdleConnTime
idleTimeDuration := 30 * time.Minute
core.SetMaxIdleConnTime(idleTimeDuration)
t.Assert(core.dynamicConfig.MaxIdleConnTime, idleTimeDuration)
})
}

Expand Down
Loading