forked from Janulka/dat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
53 lines (42 loc) · 1.2 KB
/
init.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
package runner
import (
"database/sql"
"time"
"github.com/cenkalti/backoff"
"github.com/mgutz/logxi/v1"
"gopkg.in/mgutz/dat.v1"
"gopkg.in/mgutz/dat.v1/kvs"
"gopkg.in/mgutz/dat.v1/postgres"
)
var logger log.Logger
// LogQueriesThreshold is the threshold for logging "slow" queries
var LogQueriesThreshold time.Duration
func init() {
dat.Dialect = postgres.New()
logger = log.New("dat:sqlx")
}
// Cache caches query results.
var Cache kvs.KeyValueStore
// SetCache sets this runner's cache. The default cache is in-memory
// based. See cache.MemoryKeyValueStore.
func SetCache(store kvs.KeyValueStore) {
Cache = store
}
// MustPing pings a database with an exponential backoff. The
// function panics if the database cannot be pinged after 15 minutes
func MustPing(db *sql.DB) {
var err error
b := backoff.NewExponentialBackOff()
ticker := backoff.NewTicker(b)
// Ticks will continue to arrive when the previous operation is still running,
// so operations that take a while to fail could run in quick succession.
for _ = range ticker.C {
if err = db.Ping(); err != nil {
logger.Info("pinging database...", err.Error())
continue
}
ticker.Stop()
return
}
panic("Could not ping database!")
}