Skip to content

Commit e976312

Browse files
authored
chore: revert adding pgxpool (#1602)
Signed-off-by: Miguel Martinez <[email protected]>
1 parent 544141b commit e976312

File tree

9 files changed

+60
-46
lines changed

9 files changed

+60
-46
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, MinOpenConns: in.MinOpenConns, MaxOpenConns: in.MaxOpenConns}
66+
c := &data.NewConfig{Driver: in.Driver, Source: in.Source, MaxIdleConns: int(in.MaxIdleConns), MaxOpenConns: int(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-
# min_open_conns: 1
55+
# max_idle_conns: 10
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: 12 additions & 9 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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,13 @@ message Data {
132132
message Database {
133133
string driver = 1;
134134
string source = 2;
135-
// default 0
136-
int32 min_open_conns = 3;
137-
// default max(4, runtime.NumCPU())
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
138140
int32 max_open_conns = 4;
139-
// default 30 minutes
141+
// sets the maximum amount of time a connection may be idle
140142
google.protobuf.Duration max_conn_idle_time = 5;
141143
}
142144
Database database = 1;

app/controlplane/pkg/data/data.go

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"io"
2222
"time"
2323

24+
"database/sql"
25+
2426
"entgo.io/ent/dialect"
2527
entsql "entgo.io/ent/dialect/sql"
2628

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

3335
// Load PGX driver
34-
35-
"github.com/jackc/pgx/v5/pgxpool"
36-
"github.com/jackc/pgx/v5/stdlib"
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-
MinOpenConns, MaxOpenConns int32
76+
MaxIdleConns, MaxOpenConns int
7777
MaxConnIdleTime time.Duration
7878
}
7979

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

103-
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-
}
103+
const (
104+
DefaultMaxIdleConns = 10
105+
DefaultMaxOpenConns = 50
106+
DefaultMaxIdleTime = 5 * time.Minute
107+
)
107108

109+
func initSQLDatabase(c *NewConfig, log *log.Helper) (*ent.Client, error) {
108110
log.Debugf("connecting to db: driver=%s", c.Driver)
109-
poolConfig, err := pgxpool.ParseConfig(c.Source)
111+
db, err := sql.Open(
112+
c.Driver,
113+
c.Source,
114+
)
110115
if err != nil {
111-
log.Fatal(err)
116+
return nil, fmt.Errorf("error opening the connection, driver=%s: %w", c.Driver, err)
112117
}
113118

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

119-
if n := c.MinOpenConns; n > 0 {
120-
log.Infof("DB: setting min open conns: %v", n)
121-
poolConfig.MinConns = c.MinOpenConns
122-
}
124+
log.Infof("DB: setting max open conns: %d", maxOpenConns)
125+
db.SetMaxOpenConns(maxOpenConns)
123126

124-
if t := c.MaxConnIdleTime; t > 0 {
125-
log.Infof("DB: setting max conn idle time: %v", t)
126-
poolConfig.MaxConnIdleTime = t
127+
maxIdleConns := DefaultMaxIdleConns
128+
if c.MaxIdleConns > 0 {
129+
maxIdleConns = c.MaxIdleConns
127130
}
128131

129-
pool, err := pgxpool.NewWithConfig(context.TODO(), poolConfig)
130-
if err != nil {
131-
return nil, fmt.Errorf("error creating the pool: %w", err)
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
132138
}
133139

134-
db := stdlib.OpenDBFromPool(pool)
140+
log.Infof("DB: setting max conn idle time: %v", maxIdleTime)
141+
db.SetConnMaxIdleTime(maxIdleTime)
142+
143+
// Create an ent.Driver from `db`.
135144
drv := entsql.OpenDB(dialect.Postgres, db)
136145
client := ent.NewClient(ent.Driver(drv))
137146

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.150.0
10+
version: 1.150.1
1111
# Do not update appVersion, this is handled automatically by the release process
1212
appVersion: v0.132.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.minOpenConns }}
61-
min_open_conns: {{ .Values.controlplane.externalDatabase.minOpenConns }}
60+
{{- if and .Values.controlplane.externalDatabase.maxIdleConns }}
61+
max_idle_conns: {{ .Values.controlplane.externalDatabase.maxIdleConns }}
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: 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
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
223223
##
224224
externalDatabase:
225225
host: ""
@@ -228,7 +228,7 @@ controlplane:
228228
database: ""
229229
password: ""
230230
# maxOpenConns: 50
231-
# minOpenConns: 5
231+
# maxIdleConns: 10
232232
# maxIdleTime: 5m
233233

234234
## @section Control Plane Authentication

0 commit comments

Comments
 (0)