Skip to content

Commit

Permalink
mysql/gcpmysql: fix ensure the dialer is unique
Browse files Browse the repository at this point in the history
the dialerNum doesn't increment in the last implement, it always is zero.
  • Loading branch information
giautm committed Nov 25, 2023
1 parent 00165ed commit ac4076b
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions mysql/gcpmysql/gcpmysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"net/url"
"strings"
"sync"
"sync/atomic"

"contrib.go.opencensus.io/integrations/ocsql"
"github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/proxy"
Expand Down Expand Up @@ -98,11 +99,10 @@ func (uo *URLOpener) OpenMySQLURL(ctx context.Context, u *url.URL) (*sql.DB, err
return nil, fmt.Errorf("gcpmysql: URLOpener CertSource is nil")
}
// TODO(light): Avoid global registry once https://github.com/go-sql-driver/mysql/issues/771 is fixed.
dialerCounter.mu.Lock()
dialerNum := dialerCounter.n
dialerCounter.mu.Unlock()
dialerName := fmt.Sprintf("gocloud.dev/mysql/gcpmysql/%d", dialerNum)

dialerName := fmt.Sprintf(
"gocloud.dev/mysql/gcpmysql/%d",
atomic.AddUint32(&dialerCounter, 1),
)

Check warning on line 105 in mysql/gcpmysql/gcpmysql.go

View check run for this annotation

Codecov / codecov/patch

mysql/gcpmysql/gcpmysql.go#L102-L105

Added lines #L102 - L105 were not covered by tests
cfg, err := configFromURL(u, dialerName)
if err != nil {
return nil, fmt.Errorf("gcpmysql: open config %v", err)
Expand All @@ -112,7 +112,7 @@ func (uo *URLOpener) OpenMySQLURL(ctx context.Context, u *url.URL) (*sql.DB, err
Port: 3307,
Certs: uo.CertSource,
}
mysql.RegisterDial(dialerName, client.Dial)
mysql.RegisterDialContext(dialerName, client.DialContext)

Check warning on line 115 in mysql/gcpmysql/gcpmysql.go

View check run for this annotation

Codecov / codecov/patch

mysql/gcpmysql/gcpmysql.go#L115

Added line #L115 was not covered by tests

db := sql.OpenDB(connector{cfg.FormatDSN(), uo.TraceOpts})
return db, nil
Expand Down Expand Up @@ -161,10 +161,7 @@ func instanceFromURL(u *url.URL) (instance, db string, _ error) {
return parts[0] + ":" + parts[1] + ":" + parts[2], parts[3], nil
}

var dialerCounter struct {
mu sync.Mutex
n int
}
var dialerCounter uint32

type connector struct {
dsn string
Expand Down

0 comments on commit ac4076b

Please sign in to comment.