topic.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package server
  2. import (
  3. "heckel.io/ntfy/log"
  4. "math/rand"
  5. "sync"
  6. )
  7. // topic represents a channel to which subscribers can subscribe, and publishers
  8. // can publish a message
  9. type topic struct {
  10. ID string
  11. subscribers map[int]*topicSubscriber
  12. rateVisitor *visitor
  13. mu sync.RWMutex
  14. }
  15. type topicSubscriber struct {
  16. userID string // User ID associated with this subscription, may be empty
  17. subscriber subscriber
  18. cancel func()
  19. }
  20. // subscriber is a function that is called for every new message on a topic
  21. type subscriber func(v *visitor, msg *message) error
  22. // newTopic creates a new topic
  23. func newTopic(id string) *topic {
  24. return &topic{
  25. ID: id,
  26. subscribers: make(map[int]*topicSubscriber),
  27. }
  28. }
  29. // Subscribe subscribes to this topic
  30. func (t *topic) Subscribe(s subscriber, userID string, cancel func()) int {
  31. t.mu.Lock()
  32. defer t.mu.Unlock()
  33. subscriberID := rand.Int()
  34. t.subscribers[subscriberID] = &topicSubscriber{
  35. userID: userID, // May be empty
  36. subscriber: s,
  37. cancel: cancel,
  38. }
  39. return subscriberID
  40. }
  41. func (t *topic) Stale() bool {
  42. t.mu.Lock()
  43. defer t.mu.Unlock()
  44. if t.rateVisitor != nil && !t.rateVisitor.Stale() {
  45. return false
  46. }
  47. return len(t.subscribers) == 0
  48. }
  49. func (t *topic) SetRateVisitor(v *visitor) {
  50. t.mu.Lock()
  51. defer t.mu.Unlock()
  52. t.rateVisitor = v
  53. }
  54. func (t *topic) RateVisitor() *visitor {
  55. t.mu.Lock()
  56. defer t.mu.Unlock()
  57. if t.rateVisitor != nil && t.rateVisitor.Stale() {
  58. t.rateVisitor = nil
  59. }
  60. return t.rateVisitor
  61. }
  62. // Unsubscribe removes the subscription from the list of subscribers
  63. func (t *topic) Unsubscribe(id int) {
  64. t.mu.Lock()
  65. defer t.mu.Unlock()
  66. delete(t.subscribers, id)
  67. }
  68. // Publish asynchronously publishes to all subscribers
  69. func (t *topic) Publish(v *visitor, m *message) error {
  70. go func() {
  71. // We want to lock the topic as short as possible, so we make a shallow copy of the
  72. // subscribers map here. Actually sending out the messages then doesn't have to lock.
  73. subscribers := t.subscribersCopy()
  74. if len(subscribers) > 0 {
  75. logvm(v, m).Tag(tagPublish).Debug("Forwarding to %d subscriber(s)", len(subscribers))
  76. for _, s := range subscribers {
  77. // We call the subscriber functions in their own Go routines because they are blocking, and
  78. // we don't want individual slow subscribers to be able to block others.
  79. go func(s subscriber) {
  80. if err := s(v, m); err != nil {
  81. logvm(v, m).Tag(tagPublish).Err(err).Warn("Error forwarding to subscriber")
  82. }
  83. }(s.subscriber)
  84. }
  85. } else {
  86. logvm(v, m).Tag(tagPublish).Trace("No stream or WebSocket subscribers, not forwarding")
  87. }
  88. }()
  89. return nil
  90. }
  91. // SubscribersCount returns the number of subscribers to this topic
  92. func (t *topic) SubscribersCount() int {
  93. t.mu.RLock()
  94. defer t.mu.RUnlock()
  95. return len(t.subscribers)
  96. }
  97. // CancelSubscribers calls the cancel function for all subscribers, forcing
  98. func (t *topic) CancelSubscribers(exceptUserID string) {
  99. t.mu.Lock()
  100. defer t.mu.Unlock()
  101. for _, s := range t.subscribers {
  102. if s.userID != exceptUserID {
  103. log.
  104. Tag(tagSubscribe).
  105. Fields(log.Context{
  106. "message_topic": t.ID,
  107. "user_id": s.userID,
  108. }).
  109. Debug("Canceling subscriber %s", s.userID)
  110. s.cancel()
  111. }
  112. }
  113. }
  114. // subscribersCopy returns a shallow copy of the subscribers map
  115. func (t *topic) subscribersCopy() map[int]*topicSubscriber {
  116. t.mu.Lock()
  117. defer t.mu.Unlock()
  118. subscribers := make(map[int]*topicSubscriber)
  119. for k, sub := range t.subscribers {
  120. subscribers[k] = &topicSubscriber{
  121. userID: sub.userID,
  122. subscriber: sub.subscriber,
  123. cancel: sub.cancel,
  124. }
  125. }
  126. return subscribers
  127. }