limitSearchText.go 2.1 KB

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