logdb/logdb.go
2018-12-10 14:10:24 +08:00

78 lines
1.5 KiB
Go

package logdb
import (
"database/sql"
"fmt"
"io/ioutil"
"log"
yaml "gopkg.in/yaml.v2"
)
// LogDB 属性结构
type LogDB struct {
Charset string `yaml:"charset"`
DB string `yaml:"db"`
Hosts []string `yaml:"hosts"`
Password string `yaml:"password"`
Port string `yaml:"port"`
User string `yaml:"user"`
hostid int
driver *sql.DB
errorcount int
}
// NewLogDB 创建一个logdb的配置
func NewLogDB(filename string) *LogDB {
logdb := LogDB{}
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(data, &logdb)
if err != nil {
panic(err)
}
logdb.hostid = 0
logdb.Connect()
return &logdb
}
// Ping 是否Ping通数据库
func (logdb *LogDB) Ping() error {
return logdb.driver.Ping()
}
// Connect 重连
func (logdb *LogDB) Connect() {
defer func() {
if err := recover(); err != nil {
hostlen := len(logdb.Hosts)
for i := 0; i < hostlen; i++ {
logdb.errorcount++
if logdb.errorcount >= hostlen*2 {
panic(err)
}
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", logdb.User, logdb.Password, logdb.Hosts[logdb.hostid], logdb.Port, logdb.DB))
if err != nil {
log.Println(err, logdb.Hosts[logdb.hostid], " is connect fail")
continue
}
logdb.driver = db
logdb.hostid = i
break
}
}
}()
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", logdb.User, logdb.Password, logdb.Hosts[logdb.hostid], logdb.Port, logdb.DB))
if err != nil {
panic(err)
}
db.Ping()
logdb.driver = db
}