-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
114 lines (99 loc) · 3.05 KB
/
database.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
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package config
import (
"time"
"github.com/webx-top/db/lib/factory"
"github.com/admpub/log"
"github.com/admpub/null"
"github.com/webx-top/db/lib/sqlbuilder"
)
var dbConnMaxDuration = 10 * time.Second
type DB struct {
Type string `json:"type"`
User string `json:"user"`
Password string `json:"password"`
Host string `json:"host"`
Database string `json:"database"`
Prefix string `json:"prefix"`
Options map[string]string `json:"options"`
Debug bool `json:"debug"`
ConnMaxLifetime string `json:"connMaxLifetime"` //example: 10s
MaxIdleConns int `json:"maxIdleConns"`
MaxOpenConns int `json:"maxOpenConns"`
connMaxDuration time.Duration
}
type DBConnSetter interface {
SetConnMaxLifetime(time.Duration)
SetMaxIdleConns(int)
SetMaxOpenConns(int)
}
func (d *DB) SetDebug(on bool) {
d.Debug = on
factory.SetDebug(on)
}
func (d *DB) Table(table string) string {
return d.Prefix + table
}
func (d *DB) ToTable(m sqlbuilder.Name_) string {
return d.Table(m.Name_())
}
func (d *DB) ConnMaxDuration() time.Duration {
if d.connMaxDuration > 0 {
return d.connMaxDuration
}
if len(d.ConnMaxLifetime) > 0 {
d.connMaxDuration, _ = time.ParseDuration(d.ConnMaxLifetime)
if d.connMaxDuration <= 0 {
d.connMaxDuration = dbConnMaxDuration
}
} else {
d.connMaxDuration = dbConnMaxDuration
}
return d.connMaxDuration
}
func (d *DB) SetConn(setter DBConnSetter) error {
setter.SetMaxIdleConns(d.MaxIdleConns)
setter.SetMaxOpenConns(d.MaxOpenConns)
database, ok := setter.(sqlbuilder.Database)
if !ok {
setter.SetConnMaxLifetime(d.ConnMaxDuration())
return nil
}
var retErr error
switch d.Type {
case `mysql`:
rows, err := database.Query(`show variables where Variable_name = 'wait_timeout'`)
if err != nil {
log.Error(err)
} else {
if rows.Next() {
name := null.String{}
timeout := null.String{}
err = rows.Scan(&name, &timeout)
if err != nil {
log.Error(err)
} else {
d.connMaxDuration = time.Duration(timeout.Int64()) * time.Second
d.connMaxDuration /= 2
}
}
}
retErr = err
default:
}
database.SetConnMaxLifetime(d.ConnMaxDuration())
return retErr
}