limitSearchText.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package init
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/go-xweb/httpsession"
  5. "app.yhyue.com/moapp/jybase/redis"
  6. "fmt"
  7. "net/http"
  8. "time"
  9. )
  10. var Lst *LimitSearchText
  11. type LimitSearchText struct {
  12. TimeOut int
  13. Count int
  14. UserIds []string
  15. Flag bool
  16. TotalPage int
  17. Msg string
  18. Percentage int
  19. }
  20. func IsSearchLimit(searchItems []string) bool {
  21. for _, searchItem := range searchItems {
  22. if searchItem == "detail" || searchItem == "filetext" {
  23. return true
  24. }
  25. }
  26. return false
  27. }
  28. //
  29. func InitLimitSearchText(flag bool) {
  30. Lst = &C.LimitSearchText
  31. if !Lst.Flag {
  32. return
  33. }
  34. if flag {
  35. Lst.Init()
  36. }
  37. }
  38. func (l *LimitSearchText) Init() {
  39. l.Clear()
  40. for i := 0; i < Lst.Count; i++ {
  41. redis.RPUSH("other", "jy_limitSearchText", 1)
  42. }
  43. }
  44. func (l *LimitSearchText) Clear() {
  45. redis.Del("other", "jy_limitSearchText")
  46. }
  47. //限制正文查询
  48. //return 1 正常
  49. //return -1 抱歉!由于系统繁忙暂时无法进行搜索,请1分钟后再试!
  50. //return -2 抱歉!由于系统繁忙暂时无法进行搜索,请稍后再试!
  51. func (l *LimitSearchText) IsLimited(r *http.Request, w http.ResponseWriter, s *httpsession.Session, isPayedUser bool) int {
  52. if !l.Flag {
  53. return 1
  54. }
  55. var llen = int(redis.LLEN("other", "jy_limitSearchText"))
  56. if l.TimeOut > 0 {
  57. limitFlag, isNew := l.getFlag(r, w, s)
  58. if isNew {
  59. if llen <= l.Count/2 {
  60. timeLimit, _ := redis.Exists("other", "jy_limitSearchText_"+limitFlag)
  61. if timeLimit {
  62. return -1
  63. }
  64. }
  65. }
  66. redis.Put("other", "jy_limitSearchText_"+limitFlag, 1, l.TimeOut)
  67. }
  68. if l.Count == -2 {
  69. return 1
  70. } else if l.Count == -1 {
  71. return -2
  72. }
  73. //非VIP&大会员 用户 使用并发的80%(默认)通道|| 保留一条通道给非普通用户使用
  74. if !isPayedUser {
  75. if llen <= l.Count*l.Percentage/100 || llen == 1 {
  76. return -1
  77. }
  78. }
  79. //
  80. pollLimit := redis.LPOP("other", "jy_limitSearchText")
  81. if MC.IntAll(pollLimit) <= 0 {
  82. return -2
  83. }
  84. return 1
  85. }
  86. func (l *LimitSearchText) Limit() {
  87. if !l.Flag {
  88. return
  89. }
  90. if int(redis.LLEN("other", "jy_limitSearchText")) < l.Count {
  91. redis.RPUSH("other", "jy_limitSearchText", 1)
  92. }
  93. }
  94. func (l *LimitSearchText) getFlag(r *http.Request, w http.ResponseWriter, s *httpsession.Session) (string, bool) {
  95. limitFlag, _ := s.Get("openid").(string)
  96. if limitFlag == "" {
  97. c, _ := r.Cookie("limitSearchTextFlag")
  98. if c != nil {
  99. limitFlag = c.Value
  100. if limitFlag == "" {
  101. limitFlag, _ = s.Get("limitSearchTextFlag").(string)
  102. }
  103. }
  104. }
  105. if limitFlag != "" {
  106. return limitFlag, true
  107. }
  108. limitFlag = MC.GetLetterRandom(5) + fmt.Sprint(time.Now().UnixNano())
  109. s.Set("limitSearchTextFlag", limitFlag)
  110. c := &http.Cookie{
  111. Name: "limitSearchTextFlag",
  112. Value: limitFlag,
  113. Path: "/",
  114. HttpOnly: false,
  115. MaxAge: 2592000, //一个月
  116. }
  117. http.SetCookie(w, c)
  118. return limitFlag, false
  119. }
  120. //
  121. func (l *LimitSearchText) IsCanLogin(userId string) bool {
  122. for _, v := range l.UserIds {
  123. if v == userId {
  124. return true
  125. }
  126. }
  127. return false
  128. }