service.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/main/block/conf"
  5. "go-common/app/admin/main/block/dao"
  6. "go-common/library/cache"
  7. "go-common/library/log"
  8. "go-common/library/queue/databus"
  9. )
  10. // Service struct
  11. type Service struct {
  12. dao *dao.Dao
  13. cache *cache.Cache
  14. missch chan func()
  15. accountNotifyPub *databus.Databus
  16. }
  17. // New init
  18. func New() (s *Service) {
  19. s = &Service{
  20. dao: dao.New(),
  21. cache: cache.New(1, 10240),
  22. missch: make(chan func(), 10240),
  23. accountNotifyPub: databus.New(conf.Conf.AccountNotify),
  24. }
  25. go s.missproc()
  26. return s
  27. }
  28. func (s *Service) missproc() {
  29. defer func() {
  30. if x := recover(); x != nil {
  31. log.Error("service.missproc panic(%v)", x)
  32. go s.missproc()
  33. log.Info("service.missproc recover")
  34. }
  35. }()
  36. for {
  37. f := <-s.missch
  38. f()
  39. }
  40. }
  41. func (s *Service) mission(f func()) {
  42. select {
  43. case s.missch <- f:
  44. default:
  45. log.Error("s.missch full")
  46. }
  47. }
  48. // Ping Service
  49. func (s *Service) Ping(c context.Context) (err error) {
  50. return s.dao.Ping(c)
  51. }
  52. // Close Service
  53. func (s *Service) Close() {
  54. s.dao.Close()
  55. }