service.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package service
  2. import (
  3. "context"
  4. "sync/atomic"
  5. "time"
  6. "go-common/library/log"
  7. "go-common/library/net/rpc/liverpc"
  8. "go-common/library/net/rpc/warden"
  9. xtime "go-common/library/time"
  10. "go-common/app/job/live/xroom-feed/internal/dao"
  11. daoAnchor "go-common/app/service/live/dao-anchor/api/grpc/v1"
  12. roomClient "go-common/app/service/live/room/api/liverpc"
  13. "go-common/library/conf/paladin"
  14. )
  15. // Service service.
  16. type Service struct {
  17. ac *paladin.Map
  18. dao *dao.Dao
  19. daoAnchor daoAnchor.DaoAnchorClient
  20. roomService *roomClient.Client
  21. ruleConf atomic.Value
  22. indexBlackList atomic.Value
  23. }
  24. // New new a service and return.
  25. func New() (s *Service) {
  26. var ac = new(paladin.TOML)
  27. if err := paladin.Watch("application.toml", ac); err != nil {
  28. panic(err)
  29. }
  30. s = &Service{
  31. ac: ac,
  32. dao: dao.New(),
  33. }
  34. wdConf := new(warden.ClientConfig)
  35. wdConf.Timeout = xtime.Duration(time.Second * 10)
  36. err := s.ac.Get("daoAnchorClient").UnmarshalTOML(wdConf)
  37. if err != nil {
  38. log.Error("[service]get daoAnchorClient warden error:%+v", err)
  39. wdConf.Dial = xtime.Duration(time.Millisecond * 100)
  40. wdConf.Timeout = xtime.Duration(time.Second * 10)
  41. }
  42. conn, err := daoAnchor.NewClient(wdConf) // 目前传空,如果需要配置
  43. if err != nil {
  44. panic(err)
  45. }
  46. s.daoAnchor = conn
  47. roomClientConf := new(liverpc.ClientConfig)
  48. rerr := s.ac.Get("roomClient").UnmarshalTOML(roomClientConf)
  49. if rerr != nil {
  50. log.Error("[service]get roomClient conf error:%+v", rerr)
  51. roomClientConf.ConnTimeout = xtime.Duration(time.Millisecond * 50)
  52. roomClientConf.AppID = "live.room"
  53. }
  54. s.roomService = roomClient.New(roomClientConf)
  55. s.loadConfFromDb()
  56. s.loadBlackList()
  57. go s.reloadConfFromDb()
  58. go s.reloadRecList()
  59. go s.blackListProc()
  60. return s
  61. }
  62. // Ping ping the resource.
  63. func (s *Service) Ping(ctx context.Context) (err error) {
  64. return s.dao.Ping(ctx)
  65. }
  66. // Close close the resource.
  67. func (s *Service) Close() {
  68. s.dao.Close()
  69. }