service.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package service
  2. import (
  3. "context"
  4. conf "go-common/app/interface/main/kvo/conf"
  5. "go-common/app/interface/main/kvo/dao"
  6. "go-common/library/log"
  7. "go-common/library/stat/prom"
  8. )
  9. // Service kvo main service
  10. type Service struct {
  11. da *dao.Dao
  12. docLimit int
  13. sp *prom.Prom
  14. cacheUcCh chan *cacheUc
  15. }
  16. type cacheUc struct {
  17. mid int64
  18. moduleKeyID int
  19. }
  20. // New get a kvo service
  21. func New(c *conf.Config) *Service {
  22. da := dao.New(c)
  23. s := &Service{
  24. da: da,
  25. // limit data size
  26. docLimit: c.Rule.DocLimit,
  27. cacheUcCh: make(chan *cacheUc, 1024),
  28. sp: prom.New().WithCounter("conf_cache", []string{"method"}),
  29. }
  30. go s.cacheUcProc()
  31. return s
  32. }
  33. func (s *Service) updateUcCache(mid int64, moduleKeyID int) {
  34. select {
  35. case s.cacheUcCh <- &cacheUc{
  36. mid: mid,
  37. moduleKeyID: moduleKeyID,
  38. }:
  39. default:
  40. log.Info("s.cacheUcCh is full")
  41. }
  42. }
  43. func (s *Service) cacheUcProc() {
  44. for cuc := range s.cacheUcCh {
  45. uc, err := s.da.UserConf(context.Background(), cuc.mid, cuc.moduleKeyID)
  46. if err != nil {
  47. log.Error("service.cacheUcProc(%v,%v),err:%v", cuc.mid, cuc.moduleKeyID)
  48. continue
  49. }
  50. if uc != nil {
  51. s.da.SetUserConfCache(context.Background(), uc)
  52. }
  53. }
  54. }
  55. // Ping kvo service check
  56. func (s *Service) Ping(ctx context.Context) (err error) {
  57. return s.da.Ping(ctx)
  58. }