init.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. propertyFile = flag.String("pf", "etc/property.yaml", "the logs file")
  21. Property map[string]map[string]interface{}
  22. SearchLimitKey = "jy_limitSearchText_new" // 全文或附件搜索限制
  23. Middleground *middleground.Middleground
  24. Search_Thread chan bool
  25. ReqLimitInit *ReqLimit
  26. )
  27. type ReqLimit struct {
  28. DoPool chan struct{}
  29. WaitPool chan struct{}
  30. }
  31. // -2 等待池已满
  32. // -1 超时
  33. // 1:可以执行查询
  34. func (r *ReqLimit) Limit(ctx context.Context) int {
  35. ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
  36. defer cancel()
  37. select {
  38. case <-r.WaitPool:
  39. defer func() {
  40. r.WaitPool <- struct{}{}
  41. }()
  42. select {
  43. case <-r.DoPool:
  44. return 1
  45. case <-ctx.Done(): //超时
  46. return -1
  47. }
  48. default:
  49. return -2
  50. }
  51. }
  52. func (r *ReqLimit) Release() {
  53. r.DoPool <- struct{}{}
  54. }
  55. func init() {
  56. //基本配置
  57. conf.MustLoad(*configFile, &C)
  58. conf.MustLoad(*propertyFile, &Property)
  59. //数据库配置
  60. conf.MustLoad(*dbFile, &DB)
  61. //初始mongodb
  62. MongoDBInit(&DB.Mongo)
  63. //初始化msyql
  64. MysqlInit(&DB.Mysql)
  65. //初始redis
  66. RedisInit(&DB.Redis)
  67. //初始es
  68. EsInit(&DB.Es)
  69. NoLoginEsInit(&DB.EsNoLogin)
  70. EsFreeInit(&DB.EsFree)
  71. Search_Thread = make(chan bool, C.SearchConcurrency)
  72. NoLoginPoolInit(C.NoLoginSearch.Switch, C.NoLoginSearch.ExecutionNum, C.NoLoginSearch.Wait)
  73. //初始化标签
  74. LabelInit()
  75. //
  76. SearchLimitKey = fmt.Sprintf(C.LimitSearchText.LimitKey, "jy_limitSearchText")
  77. //初始化中台相关
  78. Middleground = middleground.NewMiddleground(C.Middleground.Etcd.Hosts).
  79. RegUserCenter(C.Middleground.UserCenterKey).
  80. RegPowerCheckCenter(C.Middleground.PowerCheckCenterKey).
  81. RegResourceCenter(C.Middleground.ResourceCenterKey)
  82. }