service.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/favorite/conf"
  5. musicDao "go-common/app/interface/main/favorite/dao/music"
  6. topicDao "go-common/app/interface/main/favorite/dao/topic"
  7. videoDao "go-common/app/interface/main/favorite/dao/video"
  8. arcrpc "go-common/app/service/main/archive/api/gorpc"
  9. favpb "go-common/app/service/main/favorite/api"
  10. "go-common/library/log"
  11. "go-common/library/stat/prom"
  12. "go-common/library/sync/pipeline/fanout"
  13. )
  14. // Service define fav service
  15. type Service struct {
  16. conf *conf.Config
  17. // dao
  18. videoDao *videoDao.Dao
  19. topicDao *topicDao.Dao
  20. musicDao *musicDao.Dao
  21. // cache chan
  22. cache *fanout.Fanout
  23. // prom
  24. prom *prom.Prom
  25. // rpc
  26. favClient favpb.FavoriteClient
  27. arcRPC *arcrpc.Service2
  28. }
  29. // New return fav service
  30. func New(c *conf.Config) (s *Service) {
  31. s = &Service{
  32. conf: c,
  33. // dao
  34. videoDao: videoDao.New(c),
  35. topicDao: topicDao.New(c),
  36. musicDao: musicDao.New(c),
  37. // cache
  38. cache: fanout.New("cache"),
  39. // prom
  40. prom: prom.New().WithTimer("fav_add_video", []string{"method"}),
  41. // rpc
  42. arcRPC: arcrpc.New2(c.RPCClient2.Archive),
  43. }
  44. favClient, err := favpb.New(c.RPCClient2.FavClient)
  45. if err != nil {
  46. panic(err)
  47. }
  48. s.favClient = favClient
  49. return
  50. }
  51. // Ping check service health
  52. func (s *Service) Ping(c context.Context) (err error) {
  53. return s.videoDao.Ping(c)
  54. }
  55. // Close close service
  56. func (s *Service) Close() {
  57. s.videoDao.Close()
  58. }
  59. // PromError stat and log.
  60. func (s *Service) PromError(name string, format string, args ...interface{}) {
  61. prom.BusinessErrCount.Incr(name)
  62. log.Error(format, args...)
  63. }