Skip to content

Commit a229077

Browse files
authored
chore: use pgxpool (#1594)
Signed-off-by: Miguel Martinez <[email protected]> Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent c48f39f commit a229077

File tree

11 files changed

+54
-68
lines changed

11 files changed

+54
-68
lines changed

Diff for: app/controlplane/cmd/wire.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func wireApp(*conf.Bootstrap, credentials.ReaderWriter, log.Logger, sdk.Availabl
6363
}
6464

6565
func newDataConf(in *conf.Data_Database) *data.NewConfig {
66-
c := &data.NewConfig{Driver: in.Driver, Source: in.Source, MaxIdleConns: int(in.MaxIdleConns), MaxOpenConns: int(in.MaxOpenConns)}
66+
c := &data.NewConfig{Driver: in.Driver, Source: in.Source, MinOpenConns: in.MinOpenConns, MaxOpenConns: in.MaxOpenConns}
6767
if in.MaxConnIdleTime != nil {
6868
c.MaxConnIdleTime = in.MaxConnIdleTime.AsDuration()
6969
}

Diff for: app/controlplane/cmd/wire_gen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: app/controlplane/configs/config.devel.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ data:
5151
database:
5252
driver: pgx
5353
source: postgresql://postgres:@${DB_HOST:0.0.0.0}/controlplane
54-
# max_open_conns: 5
55-
# max_idle_conns: 10
56-
# max_conn_idle_time: 120s
54+
max_open_conns: 5
55+
min_open_conns: 1
56+
max_conn_idle_time: 120s
5757

5858
# Development credentials for the SSO authentication roundtrip
5959
auth:

Diff for: app/controlplane/internal/conf/controlplane/config/v1/conf.pb.go

+9-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: app/controlplane/internal/conf/controlplane/config/v1/conf.proto

+4-6
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,11 @@ message Data {
132132
message Database {
133133
string driver = 1;
134134
string source = 2;
135-
// sets the maximum amount of time a connection may be idle.
136-
// default to 10
137-
int32 max_idle_conns = 3;
138-
// if not set defaults to dynamic up to the max number of connections
139-
// provided by the target database
135+
// default 0
136+
int32 min_open_conns = 3;
137+
// default max(4, runtime.NumCPU())
140138
int32 max_open_conns = 4;
141-
// sets the maximum amount of time a connection may be idle
139+
// default 30 minutes
142140
google.protobuf.Duration max_conn_idle_time = 5;
143141
}
144142
Database database = 1;

Diff for: app/controlplane/pkg/data/data.go

+23-32
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"io"
2222
"time"
2323

24-
"database/sql"
25-
2624
"entgo.io/ent/dialect"
2725
entsql "entgo.io/ent/dialect/sql"
2826

@@ -33,7 +31,9 @@ import (
3331
"github.com/google/wire"
3432

3533
// Load PGX driver
36-
_ "github.com/jackc/pgx/v5/stdlib"
34+
35+
"github.com/jackc/pgx/v5/pgxpool"
36+
"github.com/jackc/pgx/v5/stdlib"
3737
)
3838

3939
// ProviderSet is data providers.
@@ -73,7 +73,7 @@ func (data *Data) SchemaLoad() error {
7373

7474
type NewConfig struct {
7575
Driver, Source string
76-
MaxIdleConns, MaxOpenConns int
76+
MinOpenConns, MaxOpenConns int32
7777
MaxConnIdleTime time.Duration
7878
}
7979

@@ -100,47 +100,38 @@ func NewData(c *NewConfig, logger log.Logger) (*Data, func(), error) {
100100
return &Data{DB: db}, cleanup, nil
101101
}
102102

103-
const (
104-
DefaultMaxIdleConns = 10
105-
DefaultMaxOpenConns = 50
106-
DefaultMaxIdleTime = 5 * time.Minute
107-
)
108-
109103
func initSQLDatabase(c *NewConfig, log *log.Helper) (*ent.Client, error) {
104+
if c.Driver != "pgx" {
105+
return nil, fmt.Errorf("unsupported driver: %s", c.Driver)
106+
}
107+
110108
log.Debugf("connecting to db: driver=%s", c.Driver)
111-
db, err := sql.Open(
112-
c.Driver,
113-
c.Source,
114-
)
109+
poolConfig, err := pgxpool.ParseConfig(c.Source)
115110
if err != nil {
116-
return nil, fmt.Errorf("error opening the connection, driver=%s: %w", c.Driver, err)
111+
log.Fatal(err)
117112
}
118113

119-
maxOpenConns := DefaultMaxOpenConns
120114
if c.MaxOpenConns > 0 {
121-
maxOpenConns = c.MaxOpenConns
115+
log.Infof("DB: setting max open conns: %d", c.MaxOpenConns)
116+
poolConfig.MaxConns = c.MaxOpenConns
122117
}
123118

124-
log.Infof("DB: setting max open conns: %d", maxOpenConns)
125-
db.SetMaxOpenConns(maxOpenConns)
126-
127-
maxIdleConns := DefaultMaxIdleConns
128-
if c.MaxIdleConns > 0 {
129-
maxIdleConns = c.MaxIdleConns
119+
if n := c.MinOpenConns; n > 0 {
120+
log.Infof("DB: setting min open conns: %v", n)
121+
poolConfig.MinConns = c.MinOpenConns
130122
}
131123

132-
log.Infof("DB: setting max idle conns: %d", maxIdleConns)
133-
db.SetMaxIdleConns(maxIdleConns)
134-
135-
maxIdleTime := DefaultMaxIdleTime
136-
if c.MaxConnIdleTime > 0 {
137-
maxIdleTime = c.MaxConnIdleTime
124+
if t := c.MaxConnIdleTime; t > 0 {
125+
log.Infof("DB: setting max conn idle time: %v", t)
126+
poolConfig.MaxConnIdleTime = t
138127
}
139128

140-
log.Infof("DB: setting max conn idle time: %v", maxIdleTime)
141-
db.SetConnMaxIdleTime(maxIdleTime)
129+
pool, err := pgxpool.NewWithConfig(context.TODO(), poolConfig)
130+
if err != nil {
131+
return nil, fmt.Errorf("error creating the pool: %w", err)
132+
}
142133

143-
// Create an ent.Driver from `db`.
134+
db := stdlib.OpenDBFromPool(pool)
144135
drv := entsql.OpenDB(dialect.Postgres, db)
145136
client := ent.NewClient(ent.Driver(drv))
146137

Diff for: deployment/chainloop/Chart.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description: Chainloop is an open source software supply chain control plane, a
77

88
type: application
99
# Bump the patch (not minor, not major) version on each change in the Chart Source code
10-
version: 1.146.1
10+
version: 1.146.2
1111
# Do not update appVersion, this is handled automatically by the release process
1212
appVersion: v0.128.0
1313

Diff for: deployment/chainloop/templates/controlplane/secret-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ stringData:
5757
{{- if and .Values.controlplane.externalDatabase.maxOpenConns }}
5858
max_open_conns: {{ .Values.controlplane.externalDatabase.maxOpenConns }}
5959
{{- end }}
60-
{{- if and .Values.controlplane.externalDatabase.maxIdleConns }}
61-
max_idle_conns: {{ .Values.controlplane.externalDatabase.maxIdleConns }}
60+
{{- if and .Values.controlplane.externalDatabase.minOpenConns }}
61+
min_open_conns: {{ .Values.controlplane.externalDatabase.minOpenConns }}
6262
{{- end }}
6363
{{- if and .Values.controlplane.externalDatabase.maxIdleTime }}
6464
max_conn_idle_time: "{{ .Values.controlplane.externalDatabase.maxIdleTime }}"

Diff for: deployment/chainloop/values.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ controlplane:
217217
## @param controlplane.externalDatabase.user Non-root username
218218
## @param controlplane.externalDatabase.database Database name
219219
## @param controlplane.externalDatabase.password Password for the non-root username
220-
## @extra controlplane.externalDatabase.maxOpenConns Maximum number of open connections to the database. Default: 50
221-
## @extra controlplane.externalDatabase.maxIdleConns Maximum number of connections in the idle connection pool. Default: 10
222-
## @extra controlplane.externalDatabase.maxIdleTime Max time a connection may be idle. Default: 5m
220+
## @extra controlplane.externalDatabase.maxOpenConns Maximum number of open connections to the database. Default: max(4, num_cpus)
221+
## @extra controlplane.externalDatabase.minOpenConns Min number of connections. Default: 0
222+
## @extra controlplane.externalDatabase.maxIdleTime Max time a connection may be idle. Default: 30m
223223
##
224224
externalDatabase:
225225
host: ""
@@ -228,7 +228,7 @@ controlplane:
228228
database: ""
229229
password: ""
230230
# maxOpenConns: 50
231-
# maxIdleConns: 10
231+
# minOpenConns: 5
232232
# maxIdleTime: 5m
233233

234234
## @section Control Plane Authentication

Diff for: go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ require (
7676
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0
7777
github.com/in-toto/attestation v0.1.1-0.20231010121940-09a03152c04a
7878
github.com/invopop/jsonschema v0.7.0
79-
github.com/jackc/pgx/v5 v5.6.0
79+
github.com/jackc/pgx/v5 v5.7.1
8080
github.com/muesli/reflow v0.3.0
8181
github.com/open-policy-agent/opa v0.68.0
8282
github.com/openvex/go-vex v0.2.5
@@ -288,7 +288,7 @@ require (
288288
github.com/imdario/mergo v0.3.16 // indirect
289289
github.com/inconshreveable/mousetrap v1.1.0 // indirect
290290
github.com/jackc/pgpassfile v1.0.0 // indirect
291-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
291+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
292292
github.com/jedisct1/go-minisign v0.0.0-20230811132847-661be99b8267 // indirect
293293
github.com/josharian/intern v1.0.0 // indirect
294294
github.com/json-iterator/go v1.1.12 // indirect

Diff for: go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -836,10 +836,10 @@ github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy77
836836
github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
837837
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
838838
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
839-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
840-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
841-
github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
842-
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
839+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
840+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
841+
github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs=
842+
github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA=
843843
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
844844
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
845845
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=

0 commit comments

Comments
 (0)