service.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "go-common/app/admin/main/aegis/conf"
  8. daoOrm "go-common/app/admin/main/aegis/dao/gorm"
  9. daoHttp "go-common/app/admin/main/aegis/dao/http"
  10. daoMc "go-common/app/admin/main/aegis/dao/mc"
  11. daoMysql "go-common/app/admin/main/aegis/dao/mysql"
  12. daoRedis "go-common/app/admin/main/aegis/dao/redis"
  13. daoRpc "go-common/app/admin/main/aegis/dao/rpc"
  14. "go-common/app/admin/main/aegis/model/common"
  15. "go-common/app/admin/main/aegis/model/middleware"
  16. "go-common/app/admin/main/aegis/model/net"
  17. "go-common/library/queue/databus"
  18. "go-common/library/sync/pipeline/fanout"
  19. )
  20. // Service struct
  21. type Service struct {
  22. c *conf.Config
  23. gorm *daoOrm.Dao
  24. http *daoHttp.Dao
  25. mc *daoMc.Dao
  26. mysql *daoMysql.Dao
  27. redis *daoRedis.Dao
  28. rpc *daoRpc.Dao
  29. aegisPub *databus.Databus
  30. // cache
  31. groupCache map[int64]*common.Group
  32. bizCfgCache map[string]string
  33. bizRoleCache map[int64]map[string]int64 //business:cfg_name:role_id/manager_id映射
  34. taskRoleCache map[int64]map[int64][]int64 //business:manager_id:flow_id映射
  35. netCacheCh chan map[string]string
  36. bizMiddlewareCache map[int64][]*middleware.Aggregate
  37. netCache map[int64]*net.Net
  38. tokenCache map[int64]*net.Token
  39. flowCache map[int64]*net.Flow
  40. transitionCache map[int64]*net.Transition
  41. bindCache map[int64]*net.TokenBind
  42. //async
  43. async *fanout.Fanout
  44. //gray
  45. gray map[int64][][]common.GrayField
  46. }
  47. // Cache .
  48. func (s *Service) Cache() map[string]interface{} {
  49. return map[string]interface{}{
  50. "groupCache": s.groupCache,
  51. "bizCfgCache": s.bizCfgCache,
  52. "bizRoleCache": s.bizRoleCache,
  53. "taskRoleCache": s.taskRoleCache,
  54. "bizMiddlewareCache": s.bizMiddlewareCache,
  55. }
  56. }
  57. // New init
  58. func New(c *conf.Config) (s *Service) {
  59. s = &Service{
  60. c: c,
  61. // dao
  62. gorm: daoOrm.New(c),
  63. http: daoHttp.New(c),
  64. mc: daoMc.New(c),
  65. mysql: daoMysql.New(c),
  66. redis: daoRedis.New(c),
  67. rpc: daoRpc.New(c),
  68. aegisPub: databus.New(c.AegisPub),
  69. // cache
  70. netCache: map[int64]*net.Net{},
  71. tokenCache: map[int64]*net.Token{},
  72. flowCache: map[int64]*net.Flow{},
  73. transitionCache: map[int64]*net.Transition{},
  74. bindCache: map[int64]*net.TokenBind{},
  75. //gray
  76. gray: loadGray(c),
  77. netCacheCh: make(chan map[string]string, 10240),
  78. //async
  79. async: fanout.New("async", fanout.Worker(10), fanout.Buffer(10240)),
  80. }
  81. go s.cacheProc()
  82. go s.setNetCache()
  83. return s
  84. }
  85. // Debug .
  86. func (s *Service) Debug() string {
  87. return s.c.Debug
  88. }
  89. // Ping Service
  90. func (s *Service) Ping(c context.Context) (err error) {
  91. return s.mysql.Ping(c)
  92. }
  93. // Close Service
  94. func (s *Service) Close() {
  95. s.async.Close()
  96. s.gorm.Close()
  97. s.mysql.Close()
  98. s.mc.Close()
  99. s.redis.Close()
  100. s.aegisPub.Close()
  101. }
  102. func (s *Service) cacheProc() {
  103. for {
  104. s.syncUpCache(context.Background())
  105. s.syncBizCache(context.Background())
  106. time.Sleep(10 * time.Minute)
  107. }
  108. }
  109. //IsAdmin uid是否为管理员
  110. func (s *Service) IsAdmin(uid int64) bool {
  111. return len(s.c.Admin) > 0 && strings.Contains(","+s.c.Admin+",", fmt.Sprintf(",%d,", uid))
  112. }
  113. func loadGray(c *conf.Config) (gray map[int64][][]common.GrayField) {
  114. if c.Gray == nil {
  115. gray = make(map[int64][][]common.GrayField)
  116. return
  117. }
  118. gray = make(map[int64][][]common.GrayField)
  119. for _, biz := range c.Gray.Biz {
  120. ones := [][]common.GrayField{}
  121. for _, opts := range biz.Options {
  122. options := []common.GrayField{}
  123. for _, field := range opts.Fields {
  124. field.Name = strings.TrimSpace(field.Name)
  125. field.Value = strings.TrimSpace(field.Value)
  126. if field.Value == "" || field.Name == "" {
  127. continue
  128. }
  129. options = append(options, common.GrayField{
  130. Name: field.Name,
  131. Value: fmt.Sprintf(",%s,", field.Value),
  132. })
  133. }
  134. ones = append(ones, options)
  135. }
  136. gray[biz.BusinessID] = ones
  137. }
  138. return
  139. }
  140. //GetMiddlewareCache get cache by bizid
  141. func (s *Service) GetMiddlewareCache(bizid int64) []*middleware.Aggregate {
  142. return s.bizMiddlewareCache[bizid]
  143. }