Skip to content

Commit c27510e

Browse files
authored
[datasource] set gorm max conn (#558)
1 parent b2e7598 commit c27510e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

datasource/postgres.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ func (p *Postgres) New(dsn string) (Datasource, error) {
9292
if err != nil {
9393
return nil, errors.Wrapf(err, "failed to connect postgres, dsn %s", dsn)
9494
}
95+
sqlDB, err := db.DB()
96+
if err != nil {
97+
return nil, errors.Wrap(err, "failed to get sql db")
98+
}
99+
sqlDB.SetMaxOpenConns(500)
100+
95101
d = &postgres{db}
96102
p.ps[dsn] = d
97103
return d, nil

datasource/postgres_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package datasource
22

33
import (
4+
"database/sql"
45
"encoding/json"
56
"testing"
67

@@ -25,13 +26,28 @@ func TestNewPostgres(t *testing.T) {
2526
r.Nil(datasource)
2627
r.ErrorContains(err, t.Name())
2728
})
29+
t.Run("FailedToGetSQLDB", func(t *testing.T) {
30+
p := NewPatches()
31+
defer p.Reset()
32+
33+
d := &gorm.DB{}
34+
testutil.GormOpen(p, d, nil)
35+
p.ApplyMethodReturn(d, "DB", nil, errors.New(t.Name()))
36+
pg := NewPostgres()
37+
datasource, err := pg.New("any")
38+
r.Nil(datasource)
39+
r.ErrorContains(err, t.Name())
40+
})
2841
t.Run("Success", func(t *testing.T) {
2942
p := NewPatches()
3043
defer p.Reset()
3144

3245
d := &gorm.DB{}
46+
sd := &sql.DB{}
3347

3448
testutil.GormOpen(p, d, nil)
49+
p.ApplyMethodReturn(d, "DB", sd, nil)
50+
p.ApplyMethodReturn(sd, "SetMaxOpenConns")
3551
pg := NewPostgres()
3652
datasource, err := pg.New("any")
3753
r.NotNil(datasource)

0 commit comments

Comments
 (0)