service.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package service
  2. import (
  3. "context"
  4. "sync"
  5. "go-common/app/interface/main/push/conf"
  6. "go-common/app/interface/main/push/dao"
  7. pushrpc "go-common/app/service/main/push/api/grpc/v1"
  8. pushmdl "go-common/app/service/main/push/model"
  9. "go-common/library/cache"
  10. httpx "go-common/library/net/http/blademaster"
  11. )
  12. // Service push service.
  13. type Service struct {
  14. c *conf.Config
  15. dao *dao.Dao
  16. cache *cache.Cache
  17. pushRPC pushrpc.PushClient
  18. callbackCh chan *pushmdl.Callback
  19. httpClient *httpx.Client
  20. waiter sync.WaitGroup
  21. closed bool
  22. }
  23. // New creates a push service instance.
  24. func New(c *conf.Config) *Service {
  25. s := &Service{
  26. c: c,
  27. dao: dao.New(c),
  28. cache: cache.New(1, 10240),
  29. callbackCh: make(chan *pushmdl.Callback, c.Push.CallbackChanLen),
  30. httpClient: httpx.NewClient(c.HTTPClient),
  31. }
  32. var err error
  33. if s.pushRPC, err = pushrpc.NewClient(c.PushRPC); err != nil {
  34. panic(err)
  35. }
  36. for i := 0; i < s.c.Push.CallbackGoroutines; i++ {
  37. s.waiter.Add(1)
  38. go s.callbackproc()
  39. }
  40. return s
  41. }
  42. // Close closes service.
  43. func (s *Service) Close() {
  44. s.closed = true
  45. close(s.callbackCh)
  46. s.waiter.Wait()
  47. s.dao.Close()
  48. }
  49. // Ping checks service.
  50. func (s *Service) Ping(c context.Context) (err error) {
  51. err = s.dao.Ping(c)
  52. return
  53. }