Skip to content

Commit

Permalink
Merge pull request #19 from PDOK/postgres_connection_pool
Browse files Browse the repository at this point in the history
Added PostgreSQL connection pool
  • Loading branch information
gerdos82 authored Jan 2, 2025
2 parents 8123787 + 31f4a39 commit 866b3fe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 12 additions & 6 deletions internal/search/datasources/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -15,7 +16,7 @@ import (
)

type Postgres struct {
db *pgx.Conn
db *pgxpool.Pool
ctx context.Context

queryTimeout time.Duration
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
575110.8422034585
],
[
109142.385825345,
575219.5200152525
109142.38582534503,
575219.520015255
],
[
108930.82374757381,
Expand Down Expand Up @@ -86,8 +86,8 @@
575110.8422034585
],
[
109142.385825345,
575219.5200152525
109142.38582534503,
575219.520015255
],
[
108930.82374757381,
Expand Down Expand Up @@ -122,7 +122,7 @@
"coordinates": [
[
[
109043.81291995465,
109043.8129199546,
553082.0701673088
],
[
Expand All @@ -138,7 +138,7 @@
575338.2567024586
],
[
109043.81291995465,
109043.8129199546,
553082.0701673088
]
]
Expand Down Expand Up @@ -170,7 +170,7 @@
"coordinates": [
[
[
109043.81291995465,
109043.8129199546,
553082.0701673088
],
[
Expand All @@ -186,7 +186,7 @@
575338.2567024586
],
[
109043.81291995465,
109043.8129199546,
553082.0701673088
]
]
Expand Down Expand Up @@ -218,23 +218,23 @@
"coordinates": [
[
[
109043.61280645092,
109043.61280645098,
553071.2827189546
],
[
122481.77479068137,
122481.77479068142,
552962.5620117659
],
[
122631.09554431966,
575219.1012624607
],
[
109254.66298884692,
575327.4685912606
109254.66298884695,
575327.4685912638
],
[
109043.61280645092,
109043.61280645098,
553071.2827189546
]
]
Expand Down Expand Up @@ -266,7 +266,7 @@
"coordinates": [
[
[
109043.81291995465,
109043.8129199546,
553082.0701673088
],
[
Expand All @@ -282,7 +282,7 @@
575338.2567024586
],
[
109043.81291995465,
109043.8129199546,
553082.0701673088
]
]
Expand Down Expand Up @@ -322,7 +322,7 @@
552955.5399556488
],
[
122540.2350379685,
122540.23503796855,
575212.0767479953
],
[
Expand Down Expand Up @@ -362,12 +362,12 @@
"coordinates": [
[
[
108970.26902163994,
108970.26902164,
553077.8551786089
],
[
122408.41355513701,
552968.9311218729
122408.41355513704,
552968.931121876
],
[
122558.07153432549,
Expand All @@ -378,7 +378,7 @@
575334.0391476178
],
[
108970.26902163994,
108970.26902164,
553077.8551786089
]
]
Expand Down Expand Up @@ -410,12 +410,12 @@
"coordinates": [
[
[
108970.26902163994,
108970.26902164,
553077.8551786089
],
[
122408.41355513701,
552968.9311218729
122408.41355513704,
552968.931121876
],
[
122558.07153432549,
Expand All @@ -426,7 +426,7 @@
575334.0391476178
],
[
108970.26902163994,
108970.26902164,
553077.8551786089
]
]
Expand Down Expand Up @@ -466,8 +466,8 @@
552992.226492696
],
[
122594.41673838987,
575248.7667374964
122594.41673838989,
575248.7667374992
],
[
109218.0664168045,
Expand Down

0 comments on commit 866b3fe

Please sign in to comment.