Skip to content

Commit 0545a4a

Browse files
authored
chore: re-enable pgxpool (#1606)
Signed-off-by: Miguel Martinez <[email protected]>
1 parent 2bb65b9 commit 0545a4a

File tree

9 files changed

+46
-60
lines changed

9 files changed

+46
-60
lines changed

app/controlplane/cmd/wire.go

Lines changed: 1 addition & 1 deletion
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
}

app/controlplane/cmd/wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/configs/config.devel.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ data:
5252
driver: pgx
5353
source: postgresql://postgres:@${DB_HOST:0.0.0.0}/controlplane
5454
# max_open_conns: 5
55-
# max_idle_conns: 10
55+
# min_open_conns: 1
5656
# max_conn_idle_time: 120s
5757

5858
# Development credentials for the SSO authentication roundtrip

app/controlplane/internal/conf/controlplane/config/v1/conf.pb.go

Lines changed: 9 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/internal/conf/controlplane/config/v1/conf.proto

Lines changed: 4 additions & 6 deletions
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;

app/controlplane/pkg/data/data.go

Lines changed: 23 additions & 32 deletions
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

deployment/chainloop/Chart.yaml

Lines changed: 1 addition & 1 deletion
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.151.0
10+
version: 1.151.1
1111
# Do not update appVersion, this is handled automatically by the release process
1212
appVersion: v0.133.0
1313

deployment/chainloop/templates/controlplane/secret-config.yaml

Lines changed: 2 additions & 2 deletions
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 }}"

deployment/chainloop/values.yaml

Lines changed: 4 additions & 4 deletions
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

0 commit comments

Comments
 (0)