diff --git a/go.mod b/go.mod index 890cc8dfe5..346b3317f0 100644 --- a/go.mod +++ b/go.mod @@ -21,8 +21,8 @@ require ( github.com/gorilla/handlers v1.5.2 github.com/gorilla/mux v1.8.1 github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 + github.com/jackc/pgx/v5 v5.6.0 github.com/kylelemons/godebug v1.1.0 - github.com/lib/pq v1.10.9 github.com/mattermost/xml-roundtrip-validator v0.1.0 github.com/mattn/go-sqlite3 v1.14.22 github.com/oklog/run v1.1.0 @@ -72,6 +72,9 @@ require ( github.com/huandu/xstrings v1.3.3 // indirect github.com/imdario/mergo v0.3.11 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/jonboulle/clockwork v0.2.2 // indirect github.com/mitchellh/copystructure v1.0.0 // indirect github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect @@ -94,6 +97,7 @@ require ( go.uber.org/multierr v1.6.0 // indirect go.uber.org/zap v1.17.0 // indirect golang.org/x/mod v0.17.0 // indirect + golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.22.0 // indirect golang.org/x/text v0.16.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f // indirect diff --git a/go.sum b/go.sum index da52911df8..fba674ef53 100644 --- a/go.sum +++ b/go.sum @@ -140,6 +140,14 @@ github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA= github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY= +github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jcmturner/aescts/v2 v2.0.0 h1:9YKLH6ey7H4eDBXW8khjYslgyqG2xZikXP0EQFKrle8= github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs= github.com/jcmturner/dnsutils/v2 v2.0.0 h1:lltnkeZGL0wILNvrNiVCR6Ro5PGU/SeBvVO/8c/iPbo= @@ -167,8 +175,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= -github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= -github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mattermost/xml-roundtrip-validator v0.1.0 h1:RXbVD2UAl7A7nOTR4u7E3ILa4IbtvKBHw64LDsmu9hU= github.com/mattermost/xml-roundtrip-validator v0.1.0/go.mod h1:qccnGMcpgwcNaBnxqpJpWWUiPNr5H3O8eDgGV9gT5To= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= diff --git a/storage/ent/postgres.go b/storage/ent/postgres.go index dad81df445..907c994b7a 100644 --- a/storage/ent/postgres.go +++ b/storage/ent/postgres.go @@ -12,12 +12,14 @@ import ( "strings" "time" + "entgo.io/ent/dialect" entSQL "entgo.io/ent/dialect/sql" - _ "github.com/lib/pq" // Register postgres driver. "github.com/dexidp/dex/storage" "github.com/dexidp/dex/storage/ent/client" "github.com/dexidp/dex/storage/ent/db" + "github.com/jackc/pgx/v5/pgxpool" + "github.com/jackc/pgx/v5/stdlib" ) const ( @@ -61,28 +63,28 @@ func (p *Postgres) Open(logger *slog.Logger) (storage.Storage, error) { } func (p *Postgres) driver() (*entSQL.Driver, error) { - drv, err := entSQL.Open("postgres", p.dsn()) + pool, err := pgxpool.New(context.Background(), p.dsn()) if err != nil { return nil, err } + db := stdlib.OpenDBFromPool(pool) + // set database/sql tunables if configured + // using pool no need maxIdleConns if p.ConnMaxLifetime != 0 { - drv.DB().SetConnMaxLifetime(time.Duration(p.ConnMaxLifetime) * time.Second) - } - - if p.MaxIdleConns == 0 { - drv.DB().SetMaxIdleConns(5) - } else { - drv.DB().SetMaxIdleConns(p.MaxIdleConns) + db.SetConnMaxLifetime(time.Duration(p.ConnMaxLifetime) * time.Second) } if p.MaxOpenConns == 0 { - drv.DB().SetMaxOpenConns(5) + db.SetMaxOpenConns(5) } else { - drv.DB().SetMaxOpenConns(p.MaxOpenConns) + db.SetMaxOpenConns(p.MaxOpenConns) } + // Create an ent.Driver from `db`. + drv := entSQL.OpenDB(dialect.Postgres, db) + return drv, nil } diff --git a/storage/sql/config.go b/storage/sql/config.go index 5379aeb6b2..cd729ff293 100644 --- a/storage/sql/config.go +++ b/storage/sql/config.go @@ -1,6 +1,7 @@ package sql import ( + "context" "crypto/tls" "crypto/x509" "database/sql" @@ -14,7 +15,9 @@ import ( "time" "github.com/go-sql-driver/mysql" - "github.com/lib/pq" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgxpool" + "github.com/jackc/pgx/v5/stdlib" "github.com/dexidp/dex/storage" ) @@ -165,22 +168,19 @@ func (p *Postgres) createDataSourceName() string { func (p *Postgres) open(logger *slog.Logger) (*conn, error) { dataSourceName := p.createDataSourceName() - db, err := sql.Open("postgres", dataSourceName) + pool, err := pgxpool.New(context.Background(), dataSourceName) if err != nil { return nil, err } + db := stdlib.OpenDBFromPool(pool) + // set database/sql tunables if configured + // using pool no need maxIdleConns if p.ConnMaxLifetime != 0 { db.SetConnMaxLifetime(time.Duration(p.ConnMaxLifetime) * time.Second) } - if p.MaxIdleConns == 0 { - db.SetMaxIdleConns(5) - } else { - db.SetMaxIdleConns(p.MaxIdleConns) - } - if p.MaxOpenConns == 0 { db.SetMaxOpenConns(5) } else { @@ -188,11 +188,11 @@ func (p *Postgres) open(logger *slog.Logger) (*conn, error) { } errCheck := func(err error) bool { - sqlErr, ok := err.(*pq.Error) + sqlErr, ok := err.(*pgconn.PgError) if !ok { return false } - return sqlErr.Code == pgErrUniqueViolation + return sqlErr.SQLState() == pgErrUniqueViolation } c := &conn{db, &flavorPostgres, logger, errCheck} diff --git a/storage/sql/sql.go b/storage/sql/sql.go index d671021fca..4400e5aa57 100644 --- a/storage/sql/sql.go +++ b/storage/sql/sql.go @@ -8,7 +8,7 @@ import ( "time" // import third party drivers - _ "github.com/lib/pq" + _ "github.com/jackc/pgx/v5/stdlib" _ "github.com/mattn/go-sqlite3" )