fix: 添加超时控制,

每个用户三十分钟未完成学习自动关闭,防止内存泄漏
This commit is contained in:
johlanse 2021-12-07 10:01:01 +08:00
parent 8b9beba670
commit 466ffc2a42
1 changed files with 58 additions and 23 deletions

View File

@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"sync"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/google/uuid"
@ -220,6 +221,9 @@ func studyAll(bot *Telegram, args []string) {
},
}
timer := time.After(time.Minute * 30)
c := make(chan int, 1)
go func() {
u := uuid.New().String()
bot.SendMsg("已创建运行实例:" + u)
datas.Store(u, &core)
@ -231,8 +235,22 @@ func studyAll(bot *Telegram, args []string) {
core.RespondDaily(user.Cookies, "daily")
core.RespondDaily(user.Cookies, "weekly")
core.RespondDaily(user.Cookies, "special")
c <- 1
}()
select {
case <-timer:
{
bot.SendMsg("学习超时,请重新登录或检查日志")
log.Errorln("学习超时,已自动退出")
core.Quit()
}
case <-c:
{
}
}
score, _ := GetUserScore(user.Cookies)
bot.SendMsg(fmt.Sprintf("%v当前学习总积分%v,今日得分:%v", user.Nick, score.TotalScore, score.TodayScore))
bot.SendMsg(fmt.Sprintf("当前学习总积分:%v,今日得分:%v", score.TotalScore, score.TodayScore))
}
s()
}
@ -285,7 +303,9 @@ func study(bot *Telegram, args []string) {
}
},
}
timer := time.After(time.Minute * 1)
c := make(chan int, 1)
go func() {
u := uuid.New().String()
bot.SendMsg("已创建运行实例:" + u)
datas.Store(u, &core)
@ -297,6 +317,21 @@ func study(bot *Telegram, args []string) {
core.RespondDaily(cookies, "daily")
core.RespondDaily(cookies, "weekly")
core.RespondDaily(cookies, "special")
c <- 1
}()
select {
case <-timer:
{
log.Errorln("学习超时,已自动退出")
bot.SendMsg("学习超时,请重新登录或检查日志")
core.Quit()
}
case <-c:
{
}
}
score, _ := GetUserScore(cookies)
bot.SendMsg(fmt.Sprintf("当前学习总积分:%v,今日得分:%v", score.TotalScore, score.TodayScore))
}