Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.

Commit 752cbb7

Browse files
authored
Remove the callback library causing race issue (#489)
* Add opencensus integration * Removed trace options * increase delay for test case * Ran go mod tidy * Moved driver registration to init method and added retriable sql connection. * Changed formatting
1 parent c43e7c0 commit 752cbb7

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
cloud.google.com/go/firestore v1.3.0 // indirect
88
contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200615190824-f8c219d2d895 // indirect
99
contrib.go.opencensus.io/exporter/prometheus v0.2.1-0.20200609204449-6bcf6f8577f0 // indirect
10+
contrib.go.opencensus.io/integrations/ocsql v0.1.6
1011
firebase.google.com/go v3.13.0+incompatible
1112
github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5 // indirect
1213
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 // indirect
@@ -23,6 +24,7 @@ require (
2324
github.com/jinzhu/gorm v1.9.16
2425
github.com/jinzhu/now v1.1.1 // indirect
2526
github.com/kelseyhightower/envconfig v1.4.0 // indirect
27+
github.com/lib/pq v1.8.0
2628
github.com/mattn/go-sqlite3 v2.0.1+incompatible // indirect
2729
github.com/mikehelmick/go-chaff v0.3.0
2830
github.com/opencensus-integrations/redigo v2.0.1+incompatible

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ contrib.go.opencensus.io/exporter/prometheus v0.2.1-0.20200609204449-6bcf6f8577f
5959
contrib.go.opencensus.io/exporter/prometheus v0.2.1-0.20200609204449-6bcf6f8577f0/go.mod h1:MjHoxkI7Ny27toPeFkRbXbzVjzIGkwOAptrAy8Mxtm8=
6060
contrib.go.opencensus.io/exporter/stackdriver v0.13.4 h1:ksUxwH3OD5sxkjzEqGxNTl+Xjsmu3BnC/300MhSVTSc=
6161
contrib.go.opencensus.io/exporter/stackdriver v0.13.4/go.mod h1:aXENhDJ1Y4lIg4EUaVTwzvYETVNZk10Pu26tevFKLUc=
62+
contrib.go.opencensus.io/integrations/ocsql v0.1.6 h1:9qmZJBlnMtffShflmfhW4EZK7M+CujIDG4bEwUrg+ms=
63+
contrib.go.opencensus.io/integrations/ocsql v0.1.6/go.mod h1:8DsSdjz3F+APR+0z0WkU1aRorQCFfRxvqjUUPMbF3fE=
6264
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
6365
firebase.google.com/go v3.13.0+incompatible h1:3TdYC3DDi6aHn20qoRkxwGqNgdjtblwVAyRLQwGn/+4=
6466
firebase.google.com/go v3.13.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIwjt8toICdV5Wh9ptHs=

pkg/database/database.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package database
1717

1818
import (
1919
"context"
20+
"database/sql"
2021
"encoding/base64"
2122
"errors"
2223
"fmt"
@@ -32,7 +33,8 @@ import (
3233
"go.uber.org/zap"
3334

3435
// ensure the postgres dialiect is compiled in.
35-
_ "github.com/jinzhu/gorm/dialects/postgres"
36+
"contrib.go.opencensus.io/integrations/ocsql"
37+
postgres "github.com/lib/pq"
3638
)
3739

3840
// Database is a handle to the database layer for the Exposure Notifications
@@ -53,6 +55,20 @@ type Database struct {
5355

5456
// secretManager is used to resolve secrets.
5557
secretManager secrets.SecretManager
58+
59+
statsCloser func()
60+
}
61+
62+
// Overrides the postgresql driver with
63+
func init() {
64+
const driverName = "ocsql"
65+
for _, v := range sql.Drivers() {
66+
if v == driverName {
67+
return
68+
}
69+
}
70+
ocsql.RegisterAllViews()
71+
sql.Register(driverName, ocsql.Wrap(&postgres.Driver{}))
5672
}
5773

5874
// SupportsPerRealmSigning returns true if the configuration supports
@@ -129,19 +145,37 @@ func (db *Database) OpenWithCacher(ctx context.Context, cacher cache.Cacher) err
129145

130146
// Establish a connection to the database. We use this later to register
131147
// opencenusus stats.
148+
var rawSQL *sql.DB
149+
if err := withRetries(ctx, func(ctx context.Context) error {
150+
var err error
151+
rawSQL, err = sql.Open("ocsql", c.ConnectionString())
152+
if err != nil {
153+
return retry.RetryableError(err)
154+
}
155+
db.statsCloser = ocsql.RecordStats(rawSQL, 5*time.Second)
156+
return nil
157+
}); err != nil {
158+
return fmt.Errorf("failed to create sql connection: %w", err)
159+
}
160+
if rawSQL == nil {
161+
return fmt.Errorf("failed to create database connection")
162+
}
163+
132164
var rawDB *gorm.DB
133165
if err := withRetries(ctx, func(ctx context.Context) error {
134166
var err error
135-
rawDB, err = gorm.Open("postgres", c.ConnectionString())
167+
// Need to give postgres dialect as otherwise gorm starts running
168+
// in compatibility mode
169+
rawDB, err = gorm.Open("postgres", rawSQL)
136170
if err != nil {
137171
return retry.RetryableError(err)
138172
}
139173
return nil
140174
}); err != nil {
141-
return fmt.Errorf("failed to connect to database: %w", err)
175+
return fmt.Errorf("failed to initialize gorm: %w", err)
142176
}
143177
if rawDB == nil {
144-
return fmt.Errorf("failed to create database connection")
178+
return fmt.Errorf("failed to initialize gorm")
145179
}
146180

147181
// Set connection configuration.
@@ -194,6 +228,7 @@ func (db *Database) OpenWithCacher(ctx context.Context, cacher cache.Cacher) err
194228

195229
// Close will close the database connection. Should be deferred right after Open.
196230
func (db *Database) Close() error {
231+
db.statsCloser()
197232
return db.db.Close()
198233
}
199234

0 commit comments

Comments
 (0)