forked from zerodha/dungbeetle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
101 lines (83 loc) · 2.01 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
package main
import (
"database/sql"
"fmt"
"math/rand"
"strings"
"time"
)
// DBs represents a map of *sql.DB connections.
type DBs map[string]*sql.DB
// Get returns an *sql.DB from the DBs map by name.
func (d DBs) Get(dbName string) *sql.DB {
return d[dbName]
}
// GetRandom returns a random *sql.DB from the DBs map.
func (d DBs) GetRandom() (string, *sql.DB) {
stop := 0
if len(d) > 1 {
stop = rand.Intn(len(d))
}
i := 0
for name, v := range d {
if i == stop {
return name, v
}
i++
}
// This'll never happen.
return "", nil
}
// GetRandomFromSet returns a random *sql.DB from the DBs map
// from a given set of names.
func (d DBs) GetRandomFromSet(dbNames []string) (string, *sql.DB) {
stop := rand.Intn(len(dbNames))
for i, n := range dbNames {
if i == stop {
return n, d[n]
}
}
// This'll never happen.
return "", nil
}
// GetNames returns the list of available DB names.
func (d DBs) GetNames() []string {
var names []string
for n := range d {
names = append(names, n)
}
return names
}
// DBsFromTag splits comma separated DB names from the SQL queries file,
// validates them against the DB map, and returns a list of valid names.
func DBsFromTag(names string, dbs DBs) (DBs, error) {
var (
chunks = strings.Split(names, ",")
newDBs = make(DBs)
)
for _, c := range chunks {
if c := strings.TrimSpace(c); c != "" {
if db, ok := dbs[c]; ok {
newDBs[c] = db
} else {
return nil, fmt.Errorf("unknown db %s", c)
}
}
}
return newDBs, nil
}
// connectDB creates and returns a database connection.
func connectDB(cfg DBConfig) (*sql.DB, error) {
db, err := sql.Open(cfg.Type, cfg.DSN)
if err != nil {
return nil, fmt.Errorf("error connecting to db : %v", err)
}
db.SetMaxIdleConns(cfg.MaxIdleConns)
db.SetMaxOpenConns(cfg.MaxActiveConns)
db.SetConnMaxLifetime(time.Second * cfg.ConnectTimeout)
// Ping database to check for connection issues.
if err = db.Ping(); err != nil {
return nil, fmt.Errorf("error connecting to db : %v", err)
}
return db, nil
}