Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazily connect to PostGIS #106

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions emissions/aep/srgspec_osm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/json"
"fmt"
"io"
"sync"

backoff "github.com/cenkalti/backoff/v4"
"github.com/ctessum/geom"
Expand Down Expand Up @@ -57,6 +58,8 @@ type SrgSpecOSM struct {
// in MergeNames.
MergeMultipliers []float64 `json:"merge_multipliers"`

connectPostGISOnce sync.Once
postGISURL string
conn *pgxpool.Pool
}

Expand All @@ -74,39 +77,43 @@ type SrgSpecOSM struct {
// and the PostGIS database should have the "hstore" extension installed before
// loading the data.
func ReadSrgSpecOSM(ctx context.Context, r io.Reader, postGISURL string) (*SrgSpecs, error) {
if postGISURL == "" {
return nil, fmt.Errorf("PostGIS URL is required")
}
// Connect to database.
var conn *pgxpool.Pool
var err error
err = backoff.Retry(func() error {
conn, err = pgxpool.Connect(ctx, postGISURL)
if err != nil {
return err
}
return nil
}, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 10))
if err != nil {
return nil, fmt.Errorf("Unable to connect to PostGIS database %s after 10 tries: %w\n", postGISURL, err)
}

// Read the surrogate specification.
d := json.NewDecoder(r)
var o []*SrgSpecOSM
if err = d.Decode(&o); err != nil {
if err := d.Decode(&o); err != nil {
return nil, err
}

// Add the db connection to each surrogate.
srgs := NewSrgSpecs()
for _, s := range o {
s.conn = conn
s.postGISURL = postGISURL
srgs.Add(s)
}
return srgs, nil
}

func (s *SrgSpecOSM) connectPostGIS() {
if s.postGISURL == "" {
panic(fmt.Errorf("PostGIS URL is required"))
}

// Connect to database.
var conn *pgxpool.Pool
var err error
err = backoff.Retry(func() error {
conn, err = pgxpool.Connect(context.Background(), s.postGISURL)
if err != nil {
return err
}
return nil
}, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 10))
if err != nil {
panic(fmt.Errorf("unable to connect to PostGIS database %s after 10 tries: %w", s.postGISURL, err))
}
s.conn = conn
}

func (srg *SrgSpecOSM) backupSurrogateNames() []string { return srg.BackupSurrogateNames }
func (srg *SrgSpecOSM) region() Country { return srg.Region }
func (srg *SrgSpecOSM) code() string { return srg.Code }
Expand Down Expand Up @@ -164,6 +171,8 @@ func (srg *SrgSpecOSM) getSrgData(gridData *GridDef, inputLoc *Location, tol flo
}
tagKeys = tagKeys[:len(tagKeys)-1] // Remove trailing comma.

srg.connectPostGISOnce.Do(srg.connectPostGIS)

rows, err := srg.conn.Query(ctx, `
SELECT
hstore_to_array(tags) tags, ST_AsBinary(way)
Expand Down