limitSearchText.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package util
  2. import (
  3. MC "app.yhyue.com/moapp/jybase/common"
  4. "app.yhyue.com/moapp/jybase/redis"
  5. "fmt"
  6. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/init"
  7. )
  8. // 标讯搜索:全文搜索和附件搜索限制
  9. func IsSearchLimit(searchItems []string) bool {
  10. for _, searchItem := range searchItems {
  11. if searchItem == "detail" || searchItem == "filetext" {
  12. return true
  13. }
  14. }
  15. return false
  16. }
  17. func LimitSearchInit() {
  18. if IC.C.LimitSearchText.Flag {
  19. LimitSearchClear()
  20. for i := 0; i < IC.C.LimitSearchText.Count; i++ {
  21. redis.RPUSH("other", IC.SearchLimitKey, 1)
  22. }
  23. }
  24. }
  25. func LimitSearchClear() {
  26. redis.Del("other", IC.SearchLimitKey)
  27. }
  28. // 限制正文、附件查询
  29. // return 1 正常
  30. // return -1 抱歉!由于系统繁忙暂时无法进行搜索,请1分钟后再试!
  31. // return -2 抱歉!由于系统繁忙暂时无法进行搜索,请稍后再试!
  32. func IsLimited(limitFlag, userid string, isPayedUser, isNew bool) int64 {
  33. if !IC.C.LimitSearchText.Flag {
  34. return 1
  35. }
  36. var llen = int(redis.LLEN("other", IC.SearchLimitKey))
  37. if IC.C.LimitSearchText.TimeOut > 0 {
  38. //limitFlag, isNew := getFlag(r, w, userid)
  39. if isNew {
  40. if llen <= IC.C.LimitSearchText.Count/2 {
  41. timeLimit, _ := redis.Exists("other", fmt.Sprintf(IC.C.LimitSearchText.LimitKey, limitFlag))
  42. if timeLimit {
  43. return -1
  44. }
  45. }
  46. }
  47. redis.Put("other", fmt.Sprintf(IC.C.LimitSearchText.LimitKey, limitFlag), 1, IC.C.LimitSearchText.TimeOut)
  48. }
  49. if IC.C.LimitSearchText.Count == -2 { //不限制
  50. return 1
  51. } else if IC.C.LimitSearchText.Count == -1 { //无条件限制
  52. return -2
  53. }
  54. //免费和付费用户 使用并发的80%(默认)通道|| 保留一条通道给付费用户使用
  55. if userid == "" { //未登录用户2个并发数限制,
  56. if IC.C.LimitSearchText.Count-IC.C.LimitSearchText.NoLogin <= 0 || llen <= IC.C.LimitSearchText.Count-IC.C.LimitSearchText.NoLogin || llen == 1 {
  57. return -2
  58. }
  59. } else if !isPayedUser {
  60. if llen <= IC.C.LimitSearchText.Count*IC.C.LimitSearchText.Percentage/100 || llen == 1 {
  61. return -1
  62. }
  63. }
  64. //
  65. pollLimit := redis.LPOP("other", IC.SearchLimitKey)
  66. if MC.IntAll(pollLimit) <= 0 {
  67. return -2
  68. }
  69. return 1
  70. }
  71. func Limit() {
  72. if !IC.C.LimitSearchText.Flag {
  73. return
  74. }
  75. if int(redis.LLEN("other", IC.SearchLimitKey)) < IC.C.LimitSearchText.Count {
  76. redis.RPUSH("other", IC.SearchLimitKey, 1)
  77. }
  78. }
  79. func IsCanLogin(userId string) bool {
  80. for _, v := range IC.C.LimitSearchText.UserIds {
  81. if v == userId {
  82. return true
  83. }
  84. }
  85. return false
  86. }