diff --git a/go.mod b/go.mod index 811bffb..25e000c 100644 --- a/go.mod +++ b/go.mod @@ -67,6 +67,7 @@ require ( github.com/invopop/yaml v0.3.1 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/klauspost/compress v1.17.7 // indirect github.com/leodido/go-urn v1.4.0 // indirect @@ -101,6 +102,7 @@ require ( go.opentelemetry.io/otel/trace v1.29.0 // indirect golang.org/x/crypto v0.27.0 // indirect golang.org/x/net v0.28.0 // indirect + golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.25.0 // indirect golang.org/x/time v0.6.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f // indirect diff --git a/internal/search/datasources/postgres/postgres.go b/internal/search/datasources/postgres/postgres.go index f68b38a..1ed92d5 100644 --- a/internal/search/datasources/postgres/postgres.go +++ b/internal/search/datasources/postgres/postgres.go @@ -6,6 +6,7 @@ import ( d "github.com/PDOK/gomagpie/internal/search/domain" "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" pggeom "github.com/twpayne/go-geom" "github.com/twpayne/go-geom/encoding/geojson" pgxgeom "github.com/twpayne/pgx-geom" @@ -15,7 +16,7 @@ import ( ) type Postgres struct { - db *pgx.Conn + db *pgxpool.Pool ctx context.Context queryTimeout time.Duration @@ -24,19 +25,24 @@ type Postgres struct { func NewPostgres(dbConn string, queryTimeout time.Duration, searchIndex string) (*Postgres, error) { ctx := context.Background() - db, err := pgx.Connect(ctx, dbConn) + config, err := pgxpool.ParseConfig(dbConn) if err != nil { - return nil, fmt.Errorf("unable to connect to database: %w", err) + return nil, fmt.Errorf("unable to parse database config: %w", err) } + // add support for Go <-> PostGIS conversions - if err := pgxgeom.Register(ctx, db); err != nil { - return nil, err + config.AfterConnect = pgxgeom.Register + + db, err := pgxpool.NewWithConfig(ctx, config) + if err != nil { + return nil, fmt.Errorf("unable to connect to database: %w", err) } + return &Postgres{db, ctx, queryTimeout, searchIndex}, nil } func (p *Postgres) Close() { - _ = p.db.Close(p.ctx) + p.db.Close() } func (p *Postgres) SearchFeaturesAcrossCollections(ctx context.Context, searchTerm string, collections d.CollectionsWithParams, diff --git a/internal/search/testdata/expected-search-den-single-collection-rd.json b/internal/search/testdata/expected-search-den-single-collection-rd.json index 0461836..2581bad 100644 --- a/internal/search/testdata/expected-search-den-single-collection-rd.json +++ b/internal/search/testdata/expected-search-den-single-collection-rd.json @@ -38,8 +38,8 @@ 575110.8422034585 ], [ - 109142.385825345, - 575219.5200152525 + 109142.38582534503, + 575219.520015255 ], [ 108930.82374757381, @@ -86,8 +86,8 @@ 575110.8422034585 ], [ - 109142.385825345, - 575219.5200152525 + 109142.38582534503, + 575219.520015255 ], [ 108930.82374757381, @@ -122,7 +122,7 @@ "coordinates": [ [ [ - 109043.81291995465, + 109043.8129199546, 553082.0701673088 ], [ @@ -138,7 +138,7 @@ 575338.2567024586 ], [ - 109043.81291995465, + 109043.8129199546, 553082.0701673088 ] ] @@ -170,7 +170,7 @@ "coordinates": [ [ [ - 109043.81291995465, + 109043.8129199546, 553082.0701673088 ], [ @@ -186,7 +186,7 @@ 575338.2567024586 ], [ - 109043.81291995465, + 109043.8129199546, 553082.0701673088 ] ] @@ -218,11 +218,11 @@ "coordinates": [ [ [ - 109043.61280645092, + 109043.61280645098, 553071.2827189546 ], [ - 122481.77479068137, + 122481.77479068142, 552962.5620117659 ], [ @@ -230,11 +230,11 @@ 575219.1012624607 ], [ - 109254.66298884692, - 575327.4685912606 + 109254.66298884695, + 575327.4685912638 ], [ - 109043.61280645092, + 109043.61280645098, 553071.2827189546 ] ] @@ -266,7 +266,7 @@ "coordinates": [ [ [ - 109043.81291995465, + 109043.8129199546, 553082.0701673088 ], [ @@ -282,7 +282,7 @@ 575338.2567024586 ], [ - 109043.81291995465, + 109043.8129199546, 553082.0701673088 ] ] @@ -322,7 +322,7 @@ 552955.5399556488 ], [ - 122540.2350379685, + 122540.23503796855, 575212.0767479953 ], [ @@ -362,12 +362,12 @@ "coordinates": [ [ [ - 108970.26902163994, + 108970.26902164, 553077.8551786089 ], [ - 122408.41355513701, - 552968.9311218729 + 122408.41355513704, + 552968.931121876 ], [ 122558.07153432549, @@ -378,7 +378,7 @@ 575334.0391476178 ], [ - 108970.26902163994, + 108970.26902164, 553077.8551786089 ] ] @@ -410,12 +410,12 @@ "coordinates": [ [ [ - 108970.26902163994, + 108970.26902164, 553077.8551786089 ], [ - 122408.41355513701, - 552968.9311218729 + 122408.41355513704, + 552968.931121876 ], [ 122558.07153432549, @@ -426,7 +426,7 @@ 575334.0391476178 ], [ - 108970.26902163994, + 108970.26902164, 553077.8551786089 ] ] @@ -466,8 +466,8 @@ 552992.226492696 ], [ - 122594.41673838987, - 575248.7667374964 + 122594.41673838989, + 575248.7667374992 ], [ 109218.0664168045,