diff --git a/controller/shout.go b/controller/shout.go index 318cda1..d780d28 100644 --- a/controller/shout.go +++ b/controller/shout.go @@ -25,7 +25,11 @@ func SendServerJiang(title string, content string, domain string) string { res := tools.Get(url) return res } -func SendVisitorLoginNotice(kefuName, visitorName, avator, content string) { +func SendVisitorLoginNotice(kefuName, visitorName, avator, content, visitorId string) { + if !tools.LimitFreqSingle("sendnotice:"+visitorId, 1, 120) { + log.Println("SendVisitorLoginNotice limit") + return + } userInfo := make(map[string]string) userInfo["username"] = visitorName userInfo["avator"] = avator diff --git a/controller/visitor.go b/controller/visitor.go index 5244d39..afd269e 100644 --- a/controller/visitor.go +++ b/controller/visitor.go @@ -117,7 +117,7 @@ func PostVisitorLogin(c *gin.Context) { //各种通知 go SendNoticeEmail(visitor.Name, "来了") go SendAppGetuiPush(kefuInfo.Name, visitor.Name, visitor.Name+"来了") - go SendVisitorLoginNotice(kefuInfo.Name, visitor.Name, visitor.Avator, visitor.Name+"来了") + go SendVisitorLoginNotice(kefuInfo.Name, visitor.Name, visitor.Avator, visitor.Name+"来了", visitor.VisitorId) go ws.VisitorOnline(kefuInfo.Name, visitor) go SendServerJiang(visitor.Name, "来了", c.Request.Host) diff --git a/tools/limits.go b/tools/limits.go index 76aa34e..af4f9db 100644 --- a/tools/limits.go +++ b/tools/limits.go @@ -1,10 +1,31 @@ package tools -import "time" +import ( + "log" + "time" +) var LimitQueue map[string][]int64 var ok bool +func init() { + cleanLimitQueue() +} +func cleanLimitQueue() { + go func() { + for { + LimitQueue = nil + log.Println("cleanLimitQueue finshed") + now := time.Now() + // 计算下一个零点 + next := now.Add(time.Hour * 24) + next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location()) + t := time.NewTimer(next.Sub(now)) + <-t.C + } + }() +} + //单机时间滑动窗口限流法 func LimitFreqSingle(queueName string, count uint, timeWindow int64) bool { currTime := time.Now().Unix()