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