pub.go 704 B

123456789101112131415161718192021222324252627282930313233343536
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/infra/notify/model"
  5. "go-common/app/infra/notify/notify"
  6. "go-common/library/ecode"
  7. )
  8. // Pub pub message.
  9. func (s *Service) Pub(c context.Context, arg *model.ArgPub) (err error) {
  10. pc, ok := s.pubConfs[key(arg.Group, arg.Topic)]
  11. if !ok {
  12. err = ecode.AccessDenied
  13. return
  14. }
  15. s.plock.RLock()
  16. pub, ok := s.pubs[key(arg.Group, arg.Topic)]
  17. s.plock.RUnlock()
  18. if !ok {
  19. pub, err = notify.NewPub(pc, s.c)
  20. if err != nil {
  21. return
  22. }
  23. s.plock.Lock()
  24. s.pubs[key(arg.Group, arg.Topic)] = pub
  25. s.plock.Unlock()
  26. }
  27. if !pub.Auth(arg.AppSecret) {
  28. err = ecode.AccessDenied
  29. return
  30. }
  31. err = pub.Send([]byte(arg.Key), []byte(arg.Msg))
  32. return
  33. }