-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.go
164 lines (142 loc) · 4 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package coresql
import (
"context"
"database/sql"
"fmt"
"log"
"time"
"github.com/golang-migrate/migrate/v4"
// Since we are most likely going to be only retrieving migrations from file source,
// it's prudent that we include this side effect inside of this package and not
// having to import it inside each and every project.
_ "github.com/golang-migrate/migrate/v4/source/file"
)
const (
waitMaxTries = 60
waitTimeout = 5 * time.Second
waitCooldown = 10 * time.Millisecond
)
var errTimeout = fmt.Errorf("could not connect to database: timed out")
// DB represents a wrapper for SQL DB providing extra methods.
type DB struct {
*sql.DB
}
// Check will attempt to ping the database to see if the connection is still alive.
func (db *DB) Check() ([]string, bool) {
if err := db.Ping(); err != nil {
return []string{err.Error()}, false
}
return []string{"database connection ok"}, true
}
// MustWait will call the Wait method and crash if it cant be performed.
func (db *DB) MustWait() {
if err := db.Wait(); err != nil {
log.Fatal(err)
}
}
// Wait will attempt to connect to a database and block until it connects.
func (db *DB) Wait() error {
ctx, cancel := context.WithTimeout(context.Background(), waitTimeout)
defer cancel()
tries := 0
doneC := make(chan struct{}, 1)
sem := make(chan struct{}, 1)
ping := func(ctx context.Context) {
err := db.PingContext(ctx)
if err == nil {
doneC <- struct{}{}
return
}
time.Sleep(waitCooldown)
tries++
<-sem
}
for {
select {
case sem <- struct{}{}:
if tries >= waitMaxTries {
return fmt.Errorf("could not connect to database: attempt limit (%d) exceeded", waitMaxTries)
}
go ping(ctx)
case <-ctx.Done():
return fmt.Errorf("could not connect to database: timeout")
case <-doneC:
return nil
}
}
}
// Open will attempt to open a new database connection.
func Open(driverName, dsn string) (*DB, error) {
switch driverName {
case "mysql":
default:
dsn = fmt.Sprintf("%s://%s", driverName, dsn)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
dbC := make(chan *sql.DB, 1)
errC := make(chan error, 1)
go func(driverName, dsn string) {
db, err := sql.Open(driverName, dsn)
if err != nil {
errC <- err
return
}
dbC <- db
}(driverName, dsn)
select {
case db := <-dbC:
// see: https://github.com/go-sql-driver/mysql/issues/674
db.SetMaxIdleConns(0)
return &DB{db}, nil
case err := <-errC:
return nil, err
case <-ctx.Done():
return nil, errTimeout
}
}
// MustOpen will crash your program unless a database could be retrieved.
func MustOpen(driverName, dsn string) *DB {
db, err := Open(driverName, dsn)
if err != nil {
log.Fatalln(err)
}
return db
}
// OpenMigration will open a migration instance.
func OpenMigration(driverName, dsn, sourceURL string) (*migrate.Migrate, error) {
dsn = fmt.Sprintf("%s://%s", driverName, dsn)
migration, err := migrate.New(sourceURL, dsn)
if err != nil {
return nil, err
}
return migration, nil
}
// MustOpenMigration will open a migration instance with and crashes if unsuccessful.
func MustOpenMigration(driverName, dsn, sourceURL string) *migrate.Migrate {
migration, err := OpenMigration(driverName, dsn, sourceURL)
if err != nil {
log.Fatalln(err)
}
return migration
}
// OpenWithMigrations opens a database connection with an associated migration instance.
func OpenWithMigrations(driverName, dsn, sourceURL string) (*DB, *migrate.Migrate, error) {
migration, err := OpenMigration(driverName, dsn, sourceURL)
if err != nil {
return nil, nil, err
}
database, err := Open(driverName, dsn)
if err != nil {
return nil, nil, err
}
return database, migration, nil
}
// MustOpenWithMigrations opens a database connection with an associated migration instance and crashes if unsuccessful.
func MustOpenWithMigrations(driverName, dsn, sourceURL string) (*DB, *migrate.Migrate) {
database, migrations, err := OpenWithMigrations(driverName, dsn, sourceURL)
if err != nil {
log.Fatalln(err)
}
return database, migrations
}