123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package init
- import (
- MC "app.yhyue.com/moapp/jybase/common"
- "app.yhyue.com/moapp/jybase/go-xweb/httpsession"
- "app.yhyue.com/moapp/jybase/redis"
- "fmt"
- "net/http"
- "time"
- )
- var Lst *LimitSearchText
- type LimitSearchText struct {
- TimeOut int
- Count int
- UserIds []string
- Flag bool
- TotalPage int
- Msg string
- Percentage int
- }
- func IsSearchLimit(searchItems []string) bool {
- for _, searchItem := range searchItems {
- if searchItem == "detail" || searchItem == "filetext" {
- return true
- }
- }
- return false
- }
- //
- func InitLimitSearchText(flag bool) {
- Lst = &C.LimitSearchText
- if !Lst.Flag {
- return
- }
- if flag {
- Lst.Init()
- }
- }
- func (l *LimitSearchText) Init() {
- l.Clear()
- for i := 0; i < Lst.Count; i++ {
- redis.RPUSH("other", "jy_limitSearchText", 1)
- }
- }
- func (l *LimitSearchText) Clear() {
- redis.Del("other", "jy_limitSearchText")
- }
- //限制正文查询
- //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", "jy_limitSearchText"))
- if l.TimeOut > 0 {
- limitFlag, isNew := l.getFlag(r, w, s)
- if isNew {
- if llen <= l.Count/2 {
- timeLimit, _ := redis.Exists("other", "jy_limitSearchText_"+limitFlag)
- if timeLimit {
- return -1
- }
- }
- }
- redis.Put("other", "jy_limitSearchText_"+limitFlag, 1, l.TimeOut)
- }
- if l.Count == -2 {
- return 1
- } else if l.Count == -1 {
- return -2
- }
- //非VIP&大会员 用户 使用并发的80%(默认)通道|| 保留一条通道给非普通用户使用
- if !isPayedUser {
- if llen <= l.Count*l.Percentage/100 || llen == 1 {
- return -1
- }
- }
- //
- pollLimit := redis.LPOP("other", "jy_limitSearchText")
- if MC.IntAll(pollLimit) <= 0 {
- return -2
- }
- return 1
- }
- func (l *LimitSearchText) Limit() {
- if !l.Flag {
- return
- }
- if int(redis.LLEN("other", "jy_limitSearchText")) < l.Count {
- redis.RPUSH("other", "jy_limitSearchText", 1)
- }
- }
- func (l *LimitSearchText) getFlag(r *http.Request, w http.ResponseWriter, s *httpsession.Session) (string, bool) {
- limitFlag, _ := s.Get("openid").(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 = MC.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
- }
|