package public import ( "fmt" "net/http" "time" util "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/redis" "app.yhyue.com/moapp/jybase/go-xweb/httpsession" ) var Lst *LimitSearchText type LimitSearchText struct { TimeOut int Count int UserIds []string Flag bool TotalPage int Msg string Percentage int LimitKey string LimitKeyUser string NoLogin int } func IsSearchLimit(searchItems []string) bool { for _, searchItem := range searchItems { if searchItem == "detail" || searchItem == "filetext" { return true } } return false } // func InitLimitSearchText(flag bool, Sysconfig map[string]interface{}) { limitSearchConfig, _ := Sysconfig["limitSearchText"].(map[string]interface{}) Lst = &LimitSearchText{ TimeOut: util.IntAllDef(limitSearchConfig["timeout"], 60), UserIds: util.ObjArrToStringArr(limitSearchConfig["userIds"].([]interface{})), Count: util.IntAllDef(limitSearchConfig["count"], 3), TotalPage: util.IntAllDef(limitSearchConfig["totalPage"], 3), Flag: limitSearchConfig["flag"].(bool), Msg: limitSearchConfig["msg"].(string), Percentage: util.IntAllDef(limitSearchConfig["percentage"], 80), LimitKey: fmt.Sprintf(util.ObjToString(limitSearchConfig["limitKey"]), "jy_limitSearchText"), LimitKeyUser: fmt.Sprintf(util.ObjToString(limitSearchConfig["limitKey"]), "%s"), NoLogin: util.IntAllDef(limitSearchConfig["noLogin"], 3), } if !Lst.Flag { return } if flag { Lst.Init() } } func (l *LimitSearchText) Init() { l.Clear() for i := 0; i < Lst.Count; i++ { redis.RPUSH("other", l.LimitKey, 1) } } func (l *LimitSearchText) Clear() { redis.Del("other", l.LimitKey) } //限制正文查询 //return 1 正常 //return -1 抱歉!由于系统繁忙暂时无法进行搜索,请1分钟后再试! //return -2 抱歉!由于系统繁忙暂时无法进行搜索,请稍后再试! func (l *LimitSearchText) IsLimited(r *http.Request, w http.ResponseWriter, s *httpsession.Session, isPayedUser bool) int { if !l.Flag { return 1 } var llen = int(redis.LLEN("other", l.LimitKey)) if l.TimeOut > 0 { limitFlag, isNew := l.getFlag(r, w, s) if isNew { if llen <= l.Count/2 { timeLimit, _ := redis.Exists("other", fmt.Sprintf(l.LimitKeyUser, limitFlag)) if timeLimit { return -1 } } } redis.Put("other", fmt.Sprintf(l.LimitKeyUser, limitFlag), 1, l.TimeOut) } if l.Count == -2 { return 1 } else if l.Count == -1 { return -2 } if s.Get("userId") == nil { //未登录用户,2个并发数限制 if l.Count-l.NoLogin <= 0 || llen <= l.Count-l.NoLogin || llen == 1 { return -2 } } else if !isPayedUser { //非VIP&大会员 用户 使用并发的80%(默认)通道|| 保留一条通道给非普通用户使用 if llen <= l.Count*l.Percentage/100 || llen == 1 { return -2 } } // pollLimit := redis.LPOP("other", l.LimitKey) if util.IntAll(pollLimit) <= 0 { return -2 } return 1 } func (l *LimitSearchText) Limit() { if !l.Flag { return } if int(redis.LLEN("other", l.LimitKey)) < l.Count { redis.RPUSH("other", l.LimitKey, 1) } } func (l *LimitSearchText) getFlag(r *http.Request, w http.ResponseWriter, s *httpsession.Session) (string, bool) { limitFlag, _ := s.Get("userId").(string) if limitFlag == "" { c, _ := r.Cookie("limitSearchTextFlag") if c != nil { limitFlag = c.Value if limitFlag == "" { limitFlag, _ = s.Get("limitSearchTextFlag").(string) } } } if limitFlag != "" { return limitFlag, true } limitFlag = util.GetLetterRandom(5) + fmt.Sprint(time.Now().UnixNano()) s.Set("limitSearchTextFlag", limitFlag) c := &http.Cookie{ Name: "limitSearchTextFlag", Value: limitFlag, Path: "/", HttpOnly: false, MaxAge: 2592000, //一个月 } http.SetCookie(w, c) return limitFlag, false } // func (l *LimitSearchText) IsCanLogin(userId string) bool { for _, v := range l.UserIds { if v == userId { return true } } return false }