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

Commit 8223065

Browse files
authored
Check error and use Ping in db.Open (#37)
There was a missing error check and the connection was checked before breaking out of the backoff loop.
1 parent 5182953 commit 8223065

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ require (
44
github.com/Masterminds/squirrel v1.1.0
55
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
66
github.com/bakins/net-http-recover v0.0.0-20141007104922-6cba69d01459
7-
github.com/cenkalti/backoff v2.2.1+incompatible
7+
github.com/cenkalti/backoff/v4 v4.0.0
88
github.com/contiamo/goserver v0.5.0
99
github.com/contiamo/jwt v0.2.2
1010
github.com/dgrijalva/jwt-go v3.2.0+incompatible

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
1313
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
1414
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
1515
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
16-
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
17-
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
16+
github.com/cenkalti/backoff/v4 v4.0.0 h1:6VeaLF9aI+MAUQ95106HwWzYZgJJpZ4stumjj6RFYAU=
17+
github.com/cenkalti/backoff/v4 v4.0.0/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg=
1818
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
1919
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd h1:qMd81Ts1T2OTKmB4acZcyKaMtRnY5Y44NuXGX2GFJ1w=
2020
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=

pkg/db/open.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import (
44
"context"
55
"database/sql"
66

7-
"github.com/cenkalti/backoff"
7+
"github.com/cenkalti/backoff/v4"
88
"github.com/contiamo/go-base/pkg/config"
99
)
1010

1111
// Open opens a connection to a database and retries until ctx.Done()
1212
// The users must import all the necessary drivers before calling this function.
1313
func Open(ctx context.Context, cfg config.Database) (db *sql.DB, err error) {
1414
connStr, err := cfg.GetConnectionString()
15+
if err != nil {
16+
return nil, err
17+
}
1518

1619
err = backoff.Retry(func() error {
1720
select {
@@ -22,7 +25,11 @@ func Open(ctx context.Context, cfg config.Database) (db *sql.DB, err error) {
2225
default:
2326
{
2427
db, err = sql.Open(cfg.DriverName, connStr)
25-
return err
28+
if err != nil {
29+
return err
30+
}
31+
32+
return db.Ping()
2633
}
2734
}
2835
}, backoff.NewExponentialBackOff())

0 commit comments

Comments
 (0)