init.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package init
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "jyBXCore/entity"
  7. "jyBXCore/rpc/internal/config"
  8. "time"
  9. "app.yhyue.com/moapp/jypkg/middleground"
  10. _ "github.com/go-sql-driver/mysql"
  11. "github.com/zeromicro/go-zero/core/conf"
  12. )
  13. var (
  14. configFile = flag.String("cf", "etc/bxcore.yaml", "the config file")
  15. C config.Config
  16. dbFile = flag.String("df", "etc/db.yaml", "the db file")
  17. DB config.Db
  18. logFile = flag.String("lf", "etc/logs.yaml", "the logs file")
  19. logc entity.Logc
  20. SearchLimitKey = "jy_limitSearchText_new" // 全文或附件搜索限制
  21. Middleground *middleground.Middleground
  22. Search_Thread chan bool
  23. ReqLimitInit *ReqLimit
  24. )
  25. type ReqLimit struct {
  26. DoPool chan struct{}
  27. WaitPool chan struct{}
  28. }
  29. // -2 等待池已满
  30. // -1 超时
  31. // 1:可以执行查询
  32. func (r *ReqLimit) Limit(ctx context.Context) int {
  33. ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
  34. defer cancel()
  35. select {
  36. case <-r.WaitPool:
  37. defer func() {
  38. r.WaitPool <- struct{}{}
  39. }()
  40. select {
  41. case <-r.DoPool:
  42. return 1
  43. case <-ctx.Done(): //超时
  44. return -1
  45. }
  46. default:
  47. return -2
  48. }
  49. }
  50. func (r *ReqLimit) Release() {
  51. r.DoPool <- struct{}{}
  52. }
  53. func init() {
  54. //基本配置
  55. conf.MustLoad(*configFile, &C)
  56. //数据库配置
  57. conf.MustLoad(*dbFile, &DB)
  58. //初始mongodb
  59. MongoDBInit(&DB.Mongo)
  60. //初始化msyql
  61. MysqlInit(&DB.Mysql)
  62. //初始redis
  63. RedisInit(&DB.Redis)
  64. //初始es
  65. EsInit(&DB.Es)
  66. NoLoginEsInit(&DB.EsNoLogin)
  67. EsFreeInit(&DB.EsFree)
  68. Search_Thread = make(chan bool, C.SearchConcurrency)
  69. NoLoginPoolInit(C.NoLoginSearch.Switch, C.NoLoginSearch.ExecutionNum, C.NoLoginSearch.Wait)
  70. //初始化标签
  71. LabelInit()
  72. //
  73. SearchLimitKey = fmt.Sprintf(C.LimitSearchText.LimitKey, "jy_limitSearchText")
  74. //初始化中台相关
  75. Middleground = middleground.NewMiddleground(C.Middleground.Etcd.Hosts).
  76. RegUserCenter(C.Middleground.UserCenterKey).
  77. RegPowerCheckCenter(C.Middleground.PowerCheckCenterKey).
  78. RegResourceCenter(C.Middleground.ResourceCenterKey)
  79. }