Skip to content

Commit

Permalink
修复db未关闭导致触发mysql并发上限
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Apr 18, 2020
1 parent a3aa3c7 commit 4d6247d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 26 deletions.
12 changes: 6 additions & 6 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func BindUser(m *tb.Message, cid, cse string) error {
}
//MS information has gotten
bot.Send(m.Chat, "MS_ID(MD5): "+u.msId+"\nuserPrincipalName: "+gjson.Get(info, "userPrincipalName").String()+"\ndisplayName: "+gjson.Get(info, "displayName").String()+"\n")
if ok, err := AddData(db, u); !ok {
if ok, err := AddData(u); !ok {
logger.Printf("%d Bind error: %s\n", m.Chat.ID, err)
return err
}
Expand All @@ -71,13 +71,13 @@ func BindUser(m *tb.Message, cid, cse string) error {

//get bind num
func GetBindNum(tgId int64) int {
data := QueryDataByTG(db, tgId)
data := QueryDataByTG(tgId)
return len(data)
}

//return true => exist
func MSAppIsExist(tgId int64, clientId string) bool {
data := QueryDataByTG(db, tgId)
data := QueryDataByTG(tgId)
var res MSData
for _, res = range data {
if res.clientId == clientId {
Expand All @@ -98,7 +98,7 @@ func SignTask() {
SignOk = make(map[int64]int)
fmt.Println("----Task Begin----")
fmt.Println("Time:" + time.Now().Format("2006-01-02 15:04:05"))
data := QueryDataAll(db)
data := QueryDataAll()
num = len(data)
fmt.Println("Start Sign")
//签到任务
Expand Down Expand Up @@ -130,7 +130,7 @@ func SignTask() {
continue
}
u.uptime = time.Now().Unix()
if ok, err := UpdateData(db, u); !ok {
if ok, err := UpdateData(u); !ok {
logger.Println(u.msId+" ", err)
bot.Send(chat, pre+err.Error(), tmpBtn)
SignErr = append(SignErr, se)
Expand All @@ -154,7 +154,7 @@ func SignTask() {
//错误上限账户清退
if ErrorTimes[u.msId] == ErrMaxTimes {
logger.Println(u.msId + " Error Limit")
if ok, err := DelData(db, u.msId); !ok {
if ok, err := DelData(u.msId); !ok {
logger.Println(err)
} else {
UnbindUser = append(UnbindUser, u.msId+" ( @"+chat.Username+" )")
Expand Down
12 changes: 6 additions & 6 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func bStart(m *tb.Message) {

func bMy(m *tb.Message) {
logger.Println(strconv.FormatInt(m.Chat.ID, 10) + " Start Manager Users")
data := QueryDataByTG(db, m.Chat.ID)
data := QueryDataByTG(m.Chat.ID)
var inlineKeys [][]tb.InlineButton
for _, u := range data {
inlineBtn := tb.InlineButton{
Expand All @@ -97,7 +97,7 @@ func bMy(m *tb.Message) {
}
func bMyInlineBtn(c *tb.Callback) {
logger.Println(strconv.FormatInt(c.Message.Chat.ID, 10) + " Get User Info")
r := QueryDataByMS(db, c.Data)
r := QueryDataByMS(c.Data)
u := r[0]
bot.Send(c.Message.Chat, "信息\n别名:"+u.alias+"\nMS_ID(MD5): "+u.msId+"\nclient_id: "+u.clientId+"\nclient_secret: "+u.clientSecret+"\n最近更新时间: "+time.Unix(u.uptime, 0).Format("2006-01-02 15:04:05"))
bot.Respond(c)
Expand Down Expand Up @@ -140,7 +140,7 @@ func bBind2(m *tb.Message) {

func bUnBind(m *tb.Message) {
logger.Println(strconv.FormatInt(m.Chat.ID, 10) + " Start Unbind")
data := QueryDataByTG(db, m.Chat.ID)
data := QueryDataByTG(m.Chat.ID)
var inlineKeys [][]tb.InlineButton
for _, u := range data {
inlineBtn := tb.InlineButton{
Expand All @@ -155,9 +155,9 @@ func bUnBind(m *tb.Message) {
}
func bUnBindInlineBtn(c *tb.Callback) {
logger.Println(strconv.FormatInt(c.Message.Chat.ID, 10) + " Unbind: " + c.Data)
r := QueryDataByMS(db, c.Data)
r := QueryDataByMS(c.Data)
u := r[0]
if ok, _ := DelData(db, u.msId); !ok {
if ok, _ := DelData(u.msId); !ok {
logger.Println(u.msId + " UnBind ERROR")
bot.Send(c.Message.Chat, "解绑失败!")
return
Expand All @@ -176,7 +176,7 @@ func bExport(m *tb.Message) {
Other string
}
var MsMini []MsMiniData
data := QueryDataByTG(db, m.Chat.ID)
data := QueryDataByTG(m.Chat.ID)
if len(data) == 0 {
bot.Send(m.Chat, "你还没有绑定过账户嗷~")
return
Expand Down
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var (
BotToken string
Socks5 string
bot *tb.Bot
db *sql.DB
logger *log.Logger
)

Expand All @@ -34,6 +33,8 @@ const (
`
)

var dbPath string

func main() {
BotStart()
}
Expand Down Expand Up @@ -84,17 +85,17 @@ func init() {
port := viper.GetString("mysql.port")
pwd := viper.GetString("mysql.password")
database := viper.GetString("mysql.database")
path := strings.Join([]string{user, ":", pwd, "@tcp(", host, ":", port, ")/", database, "?charset=utf8"}, "")
dbPath = strings.Join([]string{user, ":", pwd, "@tcp(", host, ":", port, ")/", database, "?charset=utf8"}, "")
//fmt.Println(path)
db, err = sql.Open(dbDriverName, path)
db, err := sql.Open(dbDriverName, dbPath)
if err != nil {
logger.Fatal(err)
}
logger.Println("Connect MySQL Success!")
if ok, err := CreateTB(db); !ok {
if ok, err := CreateTB(); !ok {
logger.Fatal(err)
}

defer db.Close()
BotToken = viper.GetString("bot_token")
Socks5 = viper.GetString("socks5")
//set bot
Expand Down
52 changes: 43 additions & 9 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ type MSData struct {
}

//update data by msId
func UpdateData(db *sql.DB, u MSData) (bool, error) {
func UpdateData(u MSData) (bool, error) {
db, err := sql.Open(dbDriverName, dbPath)
if err != nil {
logger.Println(err)
}
defer db.Close()
sqlString := `UPDATE users set tg_id=?,refresh_token=?,uptime=?,alias=?,client_id=?,client_secret=?,other=? where ms_id=?`
stmt, err := db.Prepare(sqlString)
if err != nil {
Expand All @@ -31,7 +36,12 @@ func UpdateData(db *sql.DB, u MSData) (bool, error) {
}

//add data
func AddData(db *sql.DB, u MSData) (bool, error) {
func AddData(u MSData) (bool, error) {
db, err := sql.Open(dbDriverName, dbPath)
if err != nil {
logger.Println(err)
}
defer db.Close()
sqlString := `
INSERT INTO users (tg_id, refresh_token,ms_id, uptime,alias,client_id,client_secret,other)
VALUES (?,?,?,?,?,?,?,?)`
Expand All @@ -47,7 +57,12 @@ func AddData(db *sql.DB, u MSData) (bool, error) {
}

//del data by ms_id
func DelData(db *sql.DB, msId string) (bool, error) {
func DelData(msId string) (bool, error) {
db, err := sql.Open(dbDriverName, dbPath)
if err != nil {
logger.Println(err)
}
defer db.Close()
sqlString := `delete from users where ms_id=?`
stmt, err := db.Prepare(sqlString)
if err != nil {
Expand Down Expand Up @@ -78,26 +93,45 @@ func QueryData(rows *sql.Rows) []MSData {
}
return result
}
func QueryDataByMS(db *sql.DB, msId string) []MSData {
func QueryDataByMS(msId string) []MSData {
db, err := sql.Open(dbDriverName, dbPath)
if err != nil {
logger.Println(err)
}
defer db.Close()
rows, err := db.Query("select * from users where ms_id = ?", msId)
CheckErr(err)
return QueryData(rows)
}

func QueryDataAll(db *sql.DB) []MSData {
func QueryDataAll() []MSData {
db, err := sql.Open(dbDriverName, dbPath)
if err != nil {
logger.Println(err)
}
defer db.Close()
rows, err := db.Query("select * from users ")
CheckErr(err)
return QueryData(rows)
}

//query data by tg_id
func QueryDataByTG(db *sql.DB, tgId int64) []MSData {
func QueryDataByTG(tgId int64) []MSData {
db, err := sql.Open(dbDriverName, dbPath)
if err != nil {
logger.Println(err)
}
defer db.Close()
rows, err := db.Query("select * from users where tg_id = ?", tgId)
CheckErr(err)
return QueryData(rows)
}
func CreateTB(db *sql.DB) (bool, error) {

func CreateTB() (bool, error) {
db, err := sql.Open(dbDriverName, dbPath)
if err != nil {
logger.Println(err)
}
defer db.Close()
sqltable := `
create table if not exists users
(
Expand All @@ -110,7 +144,7 @@ func CreateTB(db *sql.DB) (bool, error) {
client_secret VARCHAR(255),
other TEXT
);`
_, err := db.Exec(sqltable)
_, err = db.Exec(sqltable)
if err != nil {
return false, err
}
Expand Down

0 comments on commit 4d6247d

Please sign in to comment.