From 49c782809aff84b6141027f1a2634b0a0db2b18a Mon Sep 17 00:00:00 2001 From: Eno Compton Date: Mon, 4 Apr 2022 15:51:08 -0600 Subject: [PATCH] fix: prevent unnecessary allocation of conn config (#164) --- postgres/pgxv4/postgres.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/postgres/pgxv4/postgres.go b/postgres/pgxv4/postgres.go index 062f195c..4c9f4b3f 100644 --- a/postgres/pgxv4/postgres.go +++ b/postgres/pgxv4/postgres.go @@ -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 @@ -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) }