manager.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package service
  2. import (
  3. "context"
  4. "esproxy/internal/consts"
  5. "fmt"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "github.com/gogf/gf/v2/os/glog"
  8. "net/http/httputil"
  9. "net/url"
  10. "os"
  11. "sync"
  12. "time"
  13. )
  14. var (
  15. EsProxyManager *esProxyManager = initManager(context.Background())
  16. )
  17. type esProxyManager struct {
  18. lock sync.Mutex
  19. esStatus consts.EsStatus
  20. proxyPool chan *httputil.ReverseProxy
  21. simpleQueryPool, aggsQueryPool, complexQueryPool chan struct{}
  22. }
  23. // initManager 初始化代理Manager
  24. func initManager(ctx context.Context) *esProxyManager {
  25. esAddr := g.Cfg().MustGet(ctx, "elasticSearch.address").Strings()
  26. if len(esAddr) < 1 {
  27. glog.Error(ctx, "加载出错,退出")
  28. os.Exit(0)
  29. }
  30. pool := make(chan *httputil.ReverseProxy, len(esAddr))
  31. for _, v := range esAddr {
  32. u, err := url.Parse(v)
  33. if err != nil {
  34. glog.Error(ctx, "init parse esAddr err:", err)
  35. } else {
  36. pool <- CreateDefaultProxyClient(u)
  37. }
  38. }
  39. emp := &esProxyManager{
  40. lock: sync.Mutex{},
  41. esStatus: consts.EsStatus_Free,
  42. proxyPool: pool,
  43. simpleQueryPool: makeEmptyChan(g.Cfg().MustGet(ctx, "elasticSearch.pool.simple", 10).Int()),
  44. aggsQueryPool: makeEmptyChan(g.Cfg().MustGet(ctx, "elasticSearch.pool.aggs", 10).Int()),
  45. complexQueryPool: makeEmptyChan(g.Cfg().MustGet(ctx, "elasticSearch.pool.complex", 10).Int()),
  46. }
  47. go emp.UpdateEsStatus(ctx)
  48. return emp
  49. }
  50. func makeEmptyChan(total int) chan struct{} {
  51. c := make(chan struct{}, total)
  52. for i := 0; i < total; i++ {
  53. c <- struct{}{}
  54. }
  55. return c
  56. }
  57. // UpdateEsStatus 更新es状态
  58. func (epm *esProxyManager) UpdateEsStatus(ctx context.Context) {
  59. if cron := g.Cfg().MustGet(ctx, "elasticSearch.queryState.cron").String(); cron != "" {
  60. err := EsStatusQuery.GetEsStatusRunning(cron, func(ctx context.Context) {
  61. finalStatus := EsStatusQuery.GetEsStatus(ctx)
  62. epm.esStatus = finalStatus
  63. _, _ = g.Redis().Set(ctx, "es_status", finalStatus)
  64. g.Log().Infof(ctx, "当前 epm.esStatus:%v", epm.esStatus)
  65. })
  66. if err != nil {
  67. panic(err)
  68. }
  69. }
  70. }
  71. // GetProxy 获取代理对象
  72. func (epm *esProxyManager) GetProxy(ctx context.Context, queryLevel int) (*httputil.ReverseProxy, error) {
  73. if queryLevel+int(epm.esStatus) > 2 {
  74. return nil, fmt.Errorf("server is busy")
  75. }
  76. select {
  77. case <-epm.getPool(queryLevel):
  78. c := <-epm.proxyPool
  79. epm.proxyPool <- c
  80. return c, nil
  81. case <-time.After(time.Second * time.Duration(g.Cfg().MustGet(ctx, "elasticSearch.queryState.waitTime", 5).Int())):
  82. return nil, fmt.Errorf("wait time out")
  83. }
  84. }
  85. // Release 归还链接池
  86. func (epm *esProxyManager) Release(ctx context.Context, queryLevel int) {
  87. epm.getPool(queryLevel) <- struct{}{}
  88. }
  89. // 获取对应查询复杂度的链接池
  90. func (epm *esProxyManager) getPool(queryLevel int) chan struct{} {
  91. switch queryLevel {
  92. case int(consts.QueryLevelSimple):
  93. return epm.simpleQueryPool
  94. case int(consts.QueryLevelAggs):
  95. return epm.aggsQueryPool
  96. default:
  97. return epm.complexQueryPool
  98. }
  99. }