Skip to content

Commit 7074419

Browse files
committed
sqldb/v2: align sqlite idle defaults
Keep SQLite's default idle connection limit aligned with the open connection limit so the default pool matches v1 behavior. This is a follow-up regression fix to the restored open-connection default in e263ea145. After that change, SQLite again defaulted to SetMaxOpenConns(2), but SetMaxIdleConns still fell back to 6. Go silently caps idle connections at the open limit, so nothing crashed, but the configured idle default became misleading and no longer matched v1. Use cfg.MaxConns() as the inherited idle default, keep the explicit MaxIdleConnections override, and add unit coverage for the default and override cases. The mismatch was easy to miss because the code still compiled and basic tests did not assert the effective idle pool sizing.
1 parent 115daef commit 7074419

3 files changed

Lines changed: 56 additions & 6 deletions

File tree

sqldb/v2/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ func (s *SqliteConfig) MaxConns() int {
7777
return DefaultSqliteMaxConns
7878
}
7979

80+
// MaxIdleConns returns the effective maximum number of idle SQLite
81+
// connections.
82+
func (s *SqliteConfig) MaxIdleConns() int {
83+
if s.MaxIdleConnections > 0 {
84+
return s.MaxIdleConnections
85+
}
86+
87+
return s.MaxConns()
88+
}
89+
8090
// Validate checks that the SqliteConfig values are valid.
8191
func (p *SqliteConfig) Validate() error {
8292
if err := p.QueryConfig.Validate(true); err != nil {

sqldb/v2/config_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,48 @@ func TestSqliteConfigMaxConns(t *testing.T) {
4141
})
4242
}
4343
}
44+
45+
// TestSqliteConfigMaxIdleConns verifies that SQLite defaults its idle
46+
// connections to the open connection limit unless the caller overrides it.
47+
func TestSqliteConfigMaxIdleConns(t *testing.T) {
48+
t.Parallel()
49+
50+
testCases := []struct {
51+
name string
52+
maxConns int
53+
maxIdleConns int
54+
expectedIdleConn int
55+
}{
56+
{
57+
name: "default idle limit",
58+
expectedIdleConn: DefaultSqliteMaxConns,
59+
},
60+
{
61+
name: "inherits explicit open limit",
62+
maxConns: 4,
63+
expectedIdleConn: 4,
64+
},
65+
{
66+
name: "explicit idle limit",
67+
maxConns: 4,
68+
maxIdleConns: 3,
69+
expectedIdleConn: 3,
70+
},
71+
}
72+
73+
for _, testCase := range testCases {
74+
testCase := testCase
75+
76+
t.Run(testCase.name, func(t *testing.T) {
77+
t.Parallel()
78+
79+
cfg := &SqliteConfig{
80+
MaxConnections: testCase.maxConns,
81+
MaxIdleConnections: testCase.maxIdleConns,
82+
}
83+
84+
require.Equal(t, testCase.expectedIdleConn,
85+
cfg.MaxIdleConns())
86+
})
87+
}
88+
}

sqldb/v2/sqlite.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,13 @@ func NewSqliteStore(cfg *SqliteConfig, dbPath string) (*SqliteStore, error) {
136136
err)
137137
}
138138

139-
maxIdleConns := defaultMaxIdleConns
140-
if cfg.MaxIdleConnections > 0 {
141-
maxIdleConns = cfg.MaxIdleConnections
142-
}
143-
144139
connMaxLifetime := defaultConnMaxLifetime
145140
if cfg.ConnMaxLifetime > 0 {
146141
connMaxLifetime = cfg.ConnMaxLifetime
147142
}
148143

149144
db.SetMaxOpenConns(cfg.MaxConns())
150-
db.SetMaxIdleConns(maxIdleConns)
145+
db.SetMaxIdleConns(cfg.MaxIdleConns())
151146
db.SetConnMaxLifetime(connMaxLifetime)
152147

153148
s := &SqliteStore{

0 commit comments

Comments
 (0)