logdb/logdb.go

72 lines
1.4 KiB
Go
Raw Normal View History

2018-12-10 06:06:37 +00:00
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
}
// 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)
}
logdb.driver = db
}