代码拉取完成,页面将自动刷新
/*
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。