limitSearchText.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package util
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/init"
  6. "fmt"
  7. "strings"
  8. )
  9. // 标讯搜索:全文搜索和附件搜索限制
  10. func IsSearchLimit(searchItems []string) bool {
  11. for _, searchItem := range searchItems {
  12. if LimitItemsMap[searchItem] {
  13. return true
  14. }
  15. }
  16. return false
  17. }
  18. func LimitSearchInit() {
  19. if IC.C.LimitSearchText.Flag {
  20. LimitSearchClear()
  21. for i := 0; i < IC.C.LimitSearchText.Count; i++ {
  22. redis.RPUSH("other", IC.SearchLimitKey, 1)
  23. }
  24. if limitItems := IC.C.LimitSearchText.LimitItems; len(limitItems) > 0 {
  25. LimitItemsMap = map[string]bool{}
  26. for _, v := range IC.C.LimitSearchText.LimitItems {
  27. LimitItemsMap[v] = true
  28. }
  29. }
  30. }
  31. }
  32. func LimitSearchClear() {
  33. redis.Del("other", IC.SearchLimitKey)
  34. }
  35. // 限制正文、附件查询
  36. // return 1 正常
  37. // return -1 抱歉!由于系统繁忙暂时无法进行搜索,请1分钟后再试!
  38. // return -2 抱歉!由于系统繁忙暂时无法进行搜索,请稍后再试!
  39. func IsLimited(limitFlag, userid string, isPayedUser, isNew bool) int64 {
  40. if !IC.C.LimitSearchText.Flag {
  41. return 1
  42. }
  43. var llen = int(redis.LLEN("other", IC.SearchLimitKey))
  44. if IC.C.LimitSearchText.TimeOut > 0 {
  45. //limitFlag, isNew := getFlag(r, w, userid)
  46. if isNew {
  47. if llen <= IC.C.LimitSearchText.Count/2 {
  48. timeLimit, _ := redis.Exists("other", fmt.Sprintf(IC.C.LimitSearchText.LimitKey, limitFlag))
  49. if timeLimit {
  50. return -1
  51. }
  52. }
  53. }
  54. redis.Put("other", fmt.Sprintf(IC.C.LimitSearchText.LimitKey, limitFlag), 1, IC.C.LimitSearchText.TimeOut)
  55. }
  56. if IC.C.LimitSearchText.Count == -2 { //不限制
  57. return 1
  58. } else if IC.C.LimitSearchText.Count == -1 { //无条件限制
  59. return -2
  60. }
  61. //免费和付费用户 使用并发的80%(默认)通道|| 保留一条通道给付费用户使用
  62. if userid == "" { //未登录用户2个并发数限制,
  63. if IC.C.LimitSearchText.Count-IC.C.LimitSearchText.NoLogin <= 0 || llen <= IC.C.LimitSearchText.Count-IC.C.LimitSearchText.NoLogin || llen == 1 {
  64. return -2
  65. }
  66. } else if !isPayedUser {
  67. if llen <= IC.C.LimitSearchText.Count*IC.C.LimitSearchText.Percentage/100 || llen == 1 {
  68. return -1
  69. }
  70. }
  71. //
  72. pollLimit := redis.LPOP("other", IC.SearchLimitKey)
  73. if MC.IntAll(pollLimit) <= 0 {
  74. return -2
  75. }
  76. return 1
  77. }
  78. func Limit() {
  79. if !IC.C.LimitSearchText.Flag {
  80. return
  81. }
  82. if int(redis.LLEN("other", IC.SearchLimitKey)) < IC.C.LimitSearchText.Count {
  83. redis.RPUSH("other", IC.SearchLimitKey, 1)
  84. }
  85. }
  86. func IsCanLogin(userId string) bool {
  87. for _, v := range IC.C.LimitSearchText.UserIds {
  88. if v == userId {
  89. return true
  90. }
  91. }
  92. return false
  93. }
  94. var (
  95. UserIdentMap = map[string]int{
  96. "payer": 1,
  97. "free": 2,
  98. "noLogin": 3,
  99. }
  100. LimitItemsMap = map[string]bool{
  101. "detail": true,
  102. "filetext": true,
  103. }
  104. )
  105. /*
  106. 限制正文、附件查询
  107. return 1 正常
  108. return -1 抱歉!由于系统繁忙暂时无法进行搜索,请1分钟后再试!
  109. return -2 抱歉!由于系统繁忙暂时无法进行搜索,请稍后再试!
  110. userIdent 未登录用户是;cookie 中存的标识;登录用户是UserId
  111. */
  112. func CheckLimit(userType int, userIdent string, isNew bool) (state int64, msg string) {
  113. if !IC.C.LimitSearchText.Flag {
  114. state = 1
  115. return
  116. }
  117. var limitCount = int(redis.LLEN("other", IC.SearchLimitKey))
  118. switch IC.C.LimitSearchText.Count {
  119. case -2: //不限制
  120. state = 1
  121. return
  122. case -1: //无条件限制
  123. state = -2
  124. msg = fmt.Sprintf("筛选条件:%s,必限制。", strings.Join(IC.C.LimitSearchText.LimitItems, ","))
  125. return
  126. default:
  127. if limitCount <= 0 {
  128. state = -1
  129. msg = fmt.Sprintf("并发通道总数:%d,\n未使用数量:%d", IC.C.LimitSearchText.Count, limitCount)
  130. return
  131. }
  132. }
  133. //同一用户 -- 请求频次 在IC.C.LimitSearchText.TimeOut内 不允许多次请求
  134. if IC.C.LimitSearchText.TimeOut > 0 {
  135. if isNew && limitCount <= IC.C.LimitSearchText.Count/2 {
  136. timeLimit, _ := redis.Exists("other", fmt.Sprintf(IC.C.LimitSearchText.LimitKey, userIdent))
  137. if timeLimit {
  138. state = -1
  139. msg = fmt.Sprintf("当前用户:%s \n招投标筛选在%d秒内请求频次过多,\n并发通道-\n总数:%d,\n未使用数量:%d", userIdent, IC.C.LimitSearchText.TimeOut, IC.C.LimitSearchText.Count, limitCount)
  140. return
  141. }
  142. }
  143. redis.Put("other", fmt.Sprintf(IC.C.LimitSearchText.LimitKey, userIdent), 1, IC.C.LimitSearchText.TimeOut)
  144. }
  145. //userType 用户标识:1:付费用户;2:免费用户;3:未登录用户;
  146. switch userType {
  147. case 1:
  148. if limitCount > 0 {
  149. state = 1
  150. }
  151. default:
  152. state = -2
  153. userStr := MC.If(userType == 2, "免费用户", "未登录用户").(string)
  154. msg = fmt.Sprintf("%s 招投标重要字段筛选", userStr)
  155. if limitCount > IC.C.LimitSearchText.ForPayer {
  156. switch userType {
  157. case 2:
  158. quota := limitCount * IC.C.LimitSearchText.Percentage / 100
  159. if quota > 0 {
  160. state = 1
  161. msg = ""
  162. } else {
  163. state = -1
  164. msg = fmt.Sprintf("%s,\n总并发量的百分之%d", msg, IC.C.LimitSearchText.Percentage)
  165. }
  166. case 3:
  167. quota := limitCount * IC.C.LimitSearchText.NoLogin / 100
  168. if quota > 0 {
  169. state = 1
  170. msg = ""
  171. } else {
  172. state = -1
  173. msg = fmt.Sprintf("%s,\n总并发量的百分之%d", msg, IC.C.LimitSearchText.NoLogin)
  174. }
  175. }
  176. }
  177. }
  178. //两套负载 同一套并发池
  179. pollLimit := redis.LPOP("other", IC.SearchLimitKey)
  180. if MC.IntAll(pollLimit) <= 0 {
  181. state = -2
  182. }
  183. if msg != "" {
  184. msg = fmt.Sprintf("%s,\n并发通道-\n总数:%d,\n未使用数量:%d", msg, IC.C.LimitSearchText.Count, limitCount)
  185. }
  186. return
  187. }