Repository navigation
Expand file tree
/
Copy pathdatabase.go
More file actions
175 lines (145 loc) · 4.73 KB
/
Copy pathdatabase.go
File metadata and controls
175 lines (145 loc) · 4.73 KB
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
165
166
167
168
169
170
171
172
173
174
175
package services
import (
"context"
"database/sql"
"database/sql/driver"
"log"
"sync"
"time"
"github.com/cenkalti/backoff"
"github.com/jinzhu/gorm"
//Framework
c "../../config"
)
var dbConnections = make(map[string]*sql.DB)
var dbMutex sync.RWMutex
func SQLDB(ctx context.Context, connectionTag ...string) (*sql.DB, error) {
var connectionString string
if len(connectionTag) == 0 {
connectionString = c.DEFAULT_CONNECTION_TAG //Use default database connection
} else {
connectionString = connectionTag[0]
}
var db *sql.DB
// Create a retryable operation
operation := func() error {
//Check if database connection already in dbConnections Map
dbMutex.RLock()
connection := dbConnections[connectionString]
dbMutex.RUnlock()
justCreated := false // Used to indicate if connection was registered for first time
if connection == nil {
//Connection does not exist - make a new one
var err error
connection, err = sql.Open(connectionOpenString(connectionString))
if err != nil {
return err //Retry attempt
}
d := c.Connections(connectionString)
connection.SetConnMaxLifetime(d.SetConnMaxLifetime)
connection.SetMaxIdleConns(d.SetMaxIdleConns)
connection.SetMaxOpenConns(d.SetMaxOpenConns)
justCreated = true
}
// Call PingContext here and test for driver.ErrBadConn. If driver.ErrBadConn, don't try again. Otherwise try again.
err := connection.PingContext(ctx)
if err != nil {
dbMutex.Lock()
connection.Close()
if !justCreated {
//Remove from dbConnections
delete(dbConnections, connectionString)
}
dbMutex.Unlock()
if err == driver.ErrBadConn {
return backoff.Permanent(err) //Don't retry
}
return err //Retry
}
if justCreated {
//Store into dbConnections Map so we can reuse again
dbMutex.Lock()
dbConnections[connectionString] = connection
dbMutex.Unlock()
}
db = connection
return nil //All good
}
backoffAlgorithm := backoff.NewExponentialBackOff()
backoffAlgorithm.MaxElapsedTime = time.Duration(10000) * time.Millisecond
err := backoff.Retry(operation, backoffAlgorithm)
if err != nil {
return nil, err
}
return db, nil
}
// DebugLogger is a custom logger for Gorm because historically it had issues logging output whilst in local development.
// This `hack` seemed to fix the problem.
type DebugLogger struct {
}
func (w DebugLogger) Print(v ...interface{}) {
log.Print(v)
}
// Return a gorm object with the relevant underlying *sql.DB object
// NOTE: Gorm internally calls Ping(). TODO: Use a fork of Gorm that removes unnecessary Ping() call
// since SqlDB() already calls Ping()
func Gorm(ctx context.Context, connectionTag ...string) (*gorm.DB, error) {
db, err := SQLDB(ctx, connectionTag...)
if err != nil {
return nil, err
}
var connectionString string
if len(connectionTag) == 0 {
connectionString = c.DEFAULT_CONNECTION_TAG //Use default database connection
} else {
connectionString = connectionTag[0]
}
dbName, _ := connectionOpenString(connectionString)
gormDB, err := gorm.Open(dbName, db)
if err != nil {
// NOTE: It is possible that SqlDB() provided us with a healthy connection.
// Internally Gorm calls Ping() too, so it is possible that the connection became stale in the meantime.
// Let's remove it from our store of connections since it's stale
if err == driver.ErrBadConn {
dbMutex.Lock()
connection := dbConnections[connectionString]
if connection != nil {
connection.Close()
}
delete(dbConnections, connectionString) //Remove from dbConnections
dbMutex.Unlock()
}
return nil, err
}
// Set for Local Development
// gormDB.LogMode(true)
// gormDB.SetLogger(DebugLogger{})
return gormDB, nil
}
func connectionOpenString(connectionTag ...string) (string, string) {
var db *c.Database
if len(connectionTag) == 0 {
db = c.Connections(c.DEFAULT_CONNECTION_TAG)
if db == nil {
panic("No valid connectionTag specified - DEFAULT_CONNECTION_TAG setting in config/database.go is invalid")
}
} else if len(connectionTag) == 1 {
db = c.Connections(connectionTag[0])
if db == nil {
panic("No valid connectionTag specified")
}
} else {
panic("Too many arguments - connectionOpenString requires 0 or 1 argument")
}
if db.Settings == "" {
if db.Password == "" {
return db.Driver, db.User + "@" + db.Protocol + "(" + db.Host + ":" + db.Port + ")/" + db.Name
}
return db.Driver, db.User + ":" + db.Password + "@" + db.Protocol + "(" + db.Host + ":" + db.Port + ")/" + db.Name
} else {
if db.Password == "" {
return db.Driver, db.User + "@" + db.Protocol + "(" + db.Host + ":" + db.Port + ")/" + db.Name + "?" + db.Settings
}
return db.Driver, db.User + ":" + db.Password + "@" + db.Protocol + "(" + db.Host + ":" + db.Port + ")/" + db.Name + "?" + db.Settings
}
}