Skip to content

Commit

Permalink
chore: revert adding pgxpool (chainloop-dev#1602)
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Martinez <[email protected]>
  • Loading branch information
migmartri authored Nov 27, 2024
1 parent 544141b commit e976312
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 46 deletions.
2 changes: 1 addition & 1 deletion app/controlplane/cmd/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func wireApp(*conf.Bootstrap, credentials.ReaderWriter, log.Logger, sdk.Availabl
}

func newDataConf(in *conf.Data_Database) *data.NewConfig {
c := &data.NewConfig{Driver: in.Driver, Source: in.Source, MinOpenConns: in.MinOpenConns, MaxOpenConns: in.MaxOpenConns}
c := &data.NewConfig{Driver: in.Driver, Source: in.Source, MaxIdleConns: int(in.MaxIdleConns), MaxOpenConns: int(in.MaxOpenConns)}
if in.MaxConnIdleTime != nil {
c.MaxConnIdleTime = in.MaxConnIdleTime.AsDuration()
}
Expand Down
2 changes: 1 addition & 1 deletion app/controlplane/cmd/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/controlplane/configs/config.devel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ data:
driver: pgx
source: postgresql://postgres:@${DB_HOST:0.0.0.0}/controlplane
# max_open_conns: 5
# min_open_conns: 1
# max_idle_conns: 10
# max_conn_idle_time: 120s

# Development credentials for the SSO authentication roundtrip
Expand Down
21 changes: 12 additions & 9 deletions app/controlplane/internal/conf/controlplane/config/v1/conf.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions app/controlplane/internal/conf/controlplane/config/v1/conf.proto
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ message Data {
message Database {
string driver = 1;
string source = 2;
// default 0
int32 min_open_conns = 3;
// default max(4, runtime.NumCPU())
// sets the maximum amount of time a connection may be idle.
// default to 10
int32 max_idle_conns = 3;
// if not set defaults to dynamic up to the max number of connections
// provided by the target database
int32 max_open_conns = 4;
// default 30 minutes
// sets the maximum amount of time a connection may be idle
google.protobuf.Duration max_conn_idle_time = 5;
}
Database database = 1;
Expand Down
55 changes: 32 additions & 23 deletions app/controlplane/pkg/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"io"
"time"

"database/sql"

"entgo.io/ent/dialect"
entsql "entgo.io/ent/dialect/sql"

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

// Load PGX driver

"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
_ "github.com/jackc/pgx/v5/stdlib"
)

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

type NewConfig struct {
Driver, Source string
MinOpenConns, MaxOpenConns int32
MaxIdleConns, MaxOpenConns int
MaxConnIdleTime time.Duration
}

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

func initSQLDatabase(c *NewConfig, log *log.Helper) (*ent.Client, error) {
if c.Driver != "pgx" {
return nil, fmt.Errorf("unsupported driver: %s", c.Driver)
}
const (
DefaultMaxIdleConns = 10
DefaultMaxOpenConns = 50
DefaultMaxIdleTime = 5 * time.Minute
)

func initSQLDatabase(c *NewConfig, log *log.Helper) (*ent.Client, error) {
log.Debugf("connecting to db: driver=%s", c.Driver)
poolConfig, err := pgxpool.ParseConfig(c.Source)
db, err := sql.Open(
c.Driver,
c.Source,
)
if err != nil {
log.Fatal(err)
return nil, fmt.Errorf("error opening the connection, driver=%s: %w", c.Driver, err)
}

maxOpenConns := DefaultMaxOpenConns
if c.MaxOpenConns > 0 {
log.Infof("DB: setting max open conns: %d", c.MaxOpenConns)
poolConfig.MaxConns = c.MaxOpenConns
maxOpenConns = c.MaxOpenConns
}

if n := c.MinOpenConns; n > 0 {
log.Infof("DB: setting min open conns: %v", n)
poolConfig.MinConns = c.MinOpenConns
}
log.Infof("DB: setting max open conns: %d", maxOpenConns)
db.SetMaxOpenConns(maxOpenConns)

if t := c.MaxConnIdleTime; t > 0 {
log.Infof("DB: setting max conn idle time: %v", t)
poolConfig.MaxConnIdleTime = t
maxIdleConns := DefaultMaxIdleConns
if c.MaxIdleConns > 0 {
maxIdleConns = c.MaxIdleConns
}

pool, err := pgxpool.NewWithConfig(context.TODO(), poolConfig)
if err != nil {
return nil, fmt.Errorf("error creating the pool: %w", err)
log.Infof("DB: setting max idle conns: %d", maxIdleConns)
db.SetMaxIdleConns(maxIdleConns)

maxIdleTime := DefaultMaxIdleTime
if c.MaxConnIdleTime > 0 {
maxIdleTime = c.MaxConnIdleTime
}

db := stdlib.OpenDBFromPool(pool)
log.Infof("DB: setting max conn idle time: %v", maxIdleTime)
db.SetConnMaxIdleTime(maxIdleTime)

// Create an ent.Driver from `db`.
drv := entsql.OpenDB(dialect.Postgres, db)
client := ent.NewClient(ent.Driver(drv))

Expand Down
2 changes: 1 addition & 1 deletion deployment/chainloop/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: Chainloop is an open source software supply chain control plane, a

type: application
# Bump the patch (not minor, not major) version on each change in the Chart Source code
version: 1.150.0
version: 1.150.1
# Do not update appVersion, this is handled automatically by the release process
appVersion: v0.132.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ stringData:
{{- if and .Values.controlplane.externalDatabase.maxOpenConns }}
max_open_conns: {{ .Values.controlplane.externalDatabase.maxOpenConns }}
{{- end }}
{{- if and .Values.controlplane.externalDatabase.minOpenConns }}
min_open_conns: {{ .Values.controlplane.externalDatabase.minOpenConns }}
{{- if and .Values.controlplane.externalDatabase.maxIdleConns }}
max_idle_conns: {{ .Values.controlplane.externalDatabase.maxIdleConns }}
{{- end }}
{{- if and .Values.controlplane.externalDatabase.maxIdleTime }}
max_conn_idle_time: "{{ .Values.controlplane.externalDatabase.maxIdleTime }}"
Expand Down
8 changes: 4 additions & 4 deletions deployment/chainloop/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ controlplane:
## @param controlplane.externalDatabase.user Non-root username
## @param controlplane.externalDatabase.database Database name
## @param controlplane.externalDatabase.password Password for the non-root username
## @extra controlplane.externalDatabase.maxOpenConns Maximum number of open connections to the database. Default: max(4, num_cpus)
## @extra controlplane.externalDatabase.minOpenConns Min number of connections. Default: 0
## @extra controlplane.externalDatabase.maxIdleTime Max time a connection may be idle. Default: 30m
## @extra controlplane.externalDatabase.maxOpenConns Maximum number of open connections to the database. Default: 50
## @extra controlplane.externalDatabase.maxIdleConns Maximum number of connections in the idle connection pool. Default: 10
## @extra controlplane.externalDatabase.maxIdleTime Max time a connection may be idle. Default: 5m
##
externalDatabase:
host: ""
Expand All @@ -228,7 +228,7 @@ controlplane:
database: ""
password: ""
# maxOpenConns: 50
# minOpenConns: 5
# maxIdleConns: 10
# maxIdleTime: 5m

## @section Control Plane Authentication
Expand Down

0 comments on commit e976312

Please sign in to comment.