Skip to content

Commit

Permalink
fix: prevent unnecessary allocation of conn config (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
enocom authored Apr 4, 2022
1 parent 47cdf2d commit 49c7828
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions postgres/pgxv4/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func (p *pgDriver) Open(name string) (driver.Conn, error) {
return stdlib.GetDefaultDriver().Open(dbURI)
}

p.mu.Lock()
defer p.mu.Unlock()
// Recheck to ensure dbURI wasn't created between locks
dbURI, ok = p.dbURIs[name]
if ok {
return stdlib.GetDefaultDriver().Open(dbURI)
}

config, err := pgx.ParseConfig(name)
if err != nil {
return nil, err
Expand All @@ -82,12 +90,8 @@ func (p *pgDriver) Open(name string) (driver.Conn, error) {
return p.d.Dial(ctx, instConnName)
}

p.mu.Lock()
dbURI, ok = p.dbURIs[name] // check again if another goroutine already registered config
if !ok {
dbURI = stdlib.RegisterConnConfig(config)
p.dbURIs[name] = dbURI
}
p.mu.Unlock()
dbURI = stdlib.RegisterConnConfig(config)
p.dbURIs[name] = dbURI

return stdlib.GetDefaultDriver().Open(dbURI)
}

0 comments on commit 49c7828

Please sign in to comment.